开发者

Why do I have __stdcall?

开发者 https://www.devze.com 2022-12-08 09:24 出处:网络
I am starting doing some directX programming. I am using this tutorial that I have found from the Internet.

I am starting doing some directX programming. I am using this tutorial that I have found from the Internet.

I am just wondering why the CALLBACK has been defined as _stdcal开发者_C百科l and why WINAPI is as well.

I thought __stdcall was used when exporting functions that will be compiled as a dll.

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

Many thanks 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)
{

}


__stdcall refers to the calling convention, and doesn't necessarily have to do with exporting functions. Take a look at Wikipedia's article on calling conventions if you want to know more. In brief, the compiler needs to know where to pass the parameters to your function, on the stack or in registers etc.


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

Export from a DLL can also be __cdecl just as long as it is declared that way. For example: wsprintf() is not exported as WINAPI.


Think about it. The reason functions exported from a DLL has to have a fixed, known calling convention is so that users of the dll can call them. And likewise, with your WindowProc, it has to have a known calling convention so that Windows can call it. The same for WinMain. It has to be callable by the OS when it starts your program. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消