Getting Started with Windows API programming

Source: Internet
Author: User

The central idea of how Windows works is the concept of "dynamic linking." Windows itself comes with a large set of functions that the application calls by calling these functions

To implement its user interface and display text and graphics on the screen. These functions are implemented in a dynamic-link library.

The names of these files have suffix. dll, or sometimes with an. exe.

3 main subsystems of Windows: Kernel (kernel), user, GDI.

The kernel is responsible for the traditional work of the operating system: memory management, file input/output, and task management.

Users refer to the user interface and are responsible for all window management.

GDI is the graphics device interface that is responsible for displaying text and graphics on a screen or printer.

In Windows programs, calling a Windows function is no different than calling a library function that calls C.

The main difference is that the machine code of the C library function is directly linked to your program code, while the Windows function is placed in a DLL outside your program.

When the Windows program runs, it passes through a process called "dynamic linking" with the Windows interface.

Each Windows EXE file contains the various dynamic-link libraries it uses and the reference addresses of the functions in the library.

When a Windows program is loaded into memory, the function calls in the program are parsed by a pointer to the DLL function entry, and the called functions are loaded into memory.

When linking Windows programs to survive executables, be sure to link to the special "import library" provided by your programming environment.

These import libraries contain the names and reference information of the dynamic-link libraries that are encountered by all Windows function calls. The linker uses this information to build the tables in the EXE file,

Windows relies on these tables to parse Windows function calls when loading a program.

It is also worth reminding that MFC is the encapsulation of the API, hiding a lot of complex scenarios.

The Hello world! program for Windows:

#include <windows.h>intint  icmdshow) {     MessageBox (NULL, TEXT ("  Hello, world!. "), TEXT ("hellomsg"), mb_okcancel);          return 0  ;}

The #include <windows.h> of the program is a header file that is as important as the C language #include <stdio.h>.

and int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szcmdline, int icmdshow)

Just like the C-language int main (void), it is the entrance to the program. The entrance to the C program is the entrance to the Main,windows program is WinMain.

The vast majority of Windows programs follow the "Hungarian notation":
There is a short prefix in front of the variable name to indicate the data type of the variable.

Prefix Data type
C char or WCHAR or TCHAR
By BYTE (unsigned character)
N Short (shorter integer type)
I int (integral type)
X, y int, which represents the x-coordinate and y-coordinate
Cx,cy int, which represents the length of x or Y, and C represents "count" (count)
B or F BOOL (int); F means "flag"
W WORD (unsigned short integer)
L Long (integer)
Dw DWORD (unsigned long integer)
Fn Function
S String
Sz A zero-terminated string
H Handle
P Pointer

The first parameter of the WinMain is called an instance handle (Instance Handle). A handle is a numeric value that is used to identify something.

The handle is a 4byte value that can be used to identify the window, button, icon, scroll bar, output device, control or file, and so on.

The second parameter of the WinMain is usually NULL.

The third parameter of WinMain is the command line (CommandLine) used to run the program.

The fourth parameter of WinMain is used to indicate how the program is initially displayed. (Maximized to full screen, normal display, minimized to taskbar).

MessageBox function:

function Prototypes: int WINAPI MessageBox (HWND hwnd, LPCTSTR Lptext, LPCTSTR lpcaption, UINT utype);

The first parameter is usually a handle and can be NULL.

The second parameter is a text string that appears in the message box.

The third parameter is the text string that appears on the title bar.

The fourth parameter is a combination of constants at the beginning of the prefix mb_. Used for buttons, icons, and so on in dialog boxes.

Call to function: MessageBox (NULL, Text ("Content in message box"), Text ("Title in Title box"), MB_OK);

This function is generally called, and the fourth parameter is mb_***, which starts with a constant of MB.

Getting Started with Windows API programming

Related Article

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.