What happens in this WinMain () declaration?

I am learning C ++ and programming in windows api. My first "Hello Windows API" program simply displays a MessageBox (). But I have questions that the book I am reading does not explain.

Firstly, here is the program:

// HelloWin32 Program #include<Windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { MessageBox(NULL, "This gets displayed in the message.", "This is the title bar of the message dialog.", MB_OK | MB_ICONEXCLAMATION); } 

This compiles and works fine. My question is related to the WinMain () declaration, which says int WINAPI WinMain(...) . When I read it, the WinMain function (method?) Returns an integer. But what does WINAPI say?

Obviously, I am writing in the Windows API. Does WINAPI function flag, so does the program use the Windows API to execute it, or something like that?

+4
source share
1 answer

WINAPI β€” A preprocessor definition defined as __stdcall calling the convention; when functions have __stdcall before their name, it is a directive for the compiler to force this function to use this calling convention. This means that both your function and the function that calls your function agree to use the stdcall calling convention, and the call is executed correctly.

This is necessary because the default calling convention of your compiler may or may not be stdcall, so you need to explicitly tell the compiler to do this for this function. The Windows API designers have decided, mainly for reasons of compatibility and universality of the stdcall calling convention, that all function calls use the stdcall calling convention.

In addition, you can have functions with different calling conventions that are used in the same program. So, for example, WinMain should be stdcall, but other functions of your program are not executed; they can use the default compiler.

A calling convention is a method for performing actions such as the order in which the parameters should go on the stack, who should remove them from the stack when returning the function, where to put the return values ​​and other things. Different calling conventions do this differently. First of all, it is extremely important that both the caller and the callee invoke the same calling convention. For more information about calls, see the Wikipedia article .

+8
source

Source: https://habr.com/ru/post/1394801/


All Articles