Windows API Programming

Source: Internet
Author: User

Library specifies the library file to be linked when the program is linked. You can set the project or use # pragma comment (Lib, "XXX. lib ") pre-compiled commands to implement links to the library files.

Microsoft has also improved GDI and provided a class-based Application Programming Interface (GDI. This interface is intended for C/C ++ users.

1. Precautions for calling windowsapi

As mentioned above, windowsapi is implemented through the dynamic link library output function, when an application calls these functions, it searches for the current directory of the dynamically linked library file application in the following order: Windows directory -- Windows System directory -- directory indicated by system environment variables, if these directories cannot be found, the program runs abnormally. Therefore,You 'd better put these dynamic link libraries in the directory where the program is located..The simplest way to call an API function is to use C and C ++ encoding..

If an error occurs during execution, you can call the getlasterror function to return the error code. If you need to understand the description of the error code, you must use the errorlookup tool provided by Visual C ++ and enter the returned error code,

        DWORD code = GetLastError();if (code ==0) return;...SetLastError(code);

Tools --> error lookup can find the following dialog box

2. program debugging information output

To debug a running-State program, you can use the log tracing method to format the intermediate results of the program running and output them to a log text file using a string. Encapsulate these tracing functions into a public function, which can be freely called as required by the program. If there is no proper function, writeprivateprofilestring is a good choice. This is a relatively independent function that can output text content by itself. The output file is in ini format, searching is easier than searching for plain text files. This function is much more convenient than the registry function. All the four parameters are strings and called independently. The registry function needs to enable the Registry, set the registry key, disable the registry, and create a registry key if necessary.

Writeprivateprofilestring is defined as follows:

BOOL WritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);

For details, see msdn.

Another way to record intermediate information is to use debugging statements. The most important thing is the outputdebugstring function. The declaration is as follows:

void OutputDebugString(LPCTSTR lpOutputString)

When running the program, you can use the debugview tool provided by www.sysinternals.com to view debugging information. This is a free green tool. It has only one executable file and can be directly run in any directory. For example, it traces the information generated by an Internet Explorer browser plug-in. The production of this information calls the atltrace macro, but this macro is bound to eventually call the outputdebugstring function.

C and C ++ users can also use ansic or compiler extensions to provide predefined macros to enrich debugging information output. The following describes several commonly used macros.

_ File _ name of the current source file. This macro will be expanded to a string during compilation and marked with double quotation marks.

_ Line _ the row number of the macro used in the current Code. This is a 128-bit unsigned long integer constant and can be changed using the # Line command.

...

Examples of using debugging functions and predefined macros

# Include <stdio. h> # include <windows. h> int main () {char sztemp [1024]; wsprintf (sztemp, "current file name: % s \ n", _ file _); outputdebugstring (sztemp ); wsprintf (sztemp, "% d row content is printf (sztemp); \ n", _ LINE_-1); outputdebugstring (sztemp); wsprintf (sztemp, "Compilation Time: % s \ n ", _ date _" "_ time _); outputdebugstring (sztemp); wsprintf (sztemp," Last file update time: % s \ n ", _ timestamp _); outputdebugstring (sztemp); Return 0 ;}

The definition of _ file _ is not found in the above code compilation. How should I define it?

Note: precompiled macros replace macro definitions with compilation information during compilation. After compilation, the information is hard-coded. Its content will not change with the running environment unless you recompile it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.