Why do I have __stdcall?

I am starting to do directX programming. I am using this tutorial that I found from the internet.

I'm just wondering why CALLBACK was defined as _stdcall and why WINAPI too.

I thought __stdcall was used when exporting functions to be compiled as dll.

However, since WindowProc and WINAPI will never be exported, why are these functions declared as __stdcall?

Thanks a lot for any suggestions,

// WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, 
                            UINT message, 
                            WPARAM wParam, 
                            LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{

}

      

+2


source to share


3 answers


__ stdcall refers to the calling convention and is not necessarily related to exporting functions. See the Wikipedia article on calling conventions if you want to know more. In short, the compiler needs to know where to pass parameters to your function, onto the stack or into registers, etc.



+8


source


__stdcall

also helps to reduce code size in general on X86 architecture. Instead of each caller restoring the stack, the stack is restored only once at the end of the function before it returns.



An export from a DLL can also be __cdecl

as long as it is declared that way. For example: wsprintf()

not exported as WINAPI

.

+6


source


Think about it. Reason functions exported from a DLL must have a fixed known calling convention in order for dll users to call them. Likewise, with your WindowProc, it must have a known calling convention for Windows to call it. The same goes for WinMain. It should be called by the OS when your program starts. :)

+4


source







All Articles