Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the CALLBACK keyword mean in a Win-32 C++ application? [duplicate]

Some function calls in Win-32 C++ applications have the CALLBACK keyword applied to them, as in this example (taken from this MSDN page):

BOOL CALLBACK DeleteItemProc(HWND hwndDlg, UINT message,
                             WPARAM wParam, LPARAM lParam) 
{
    // ... code here ...
}

I see via Visual Studio that the CALLBACK keyword is defined (using #define) as __stdcall. The __stdcall documentation doesn't make it much clearer (to me at least) what it does.

In short, what does using CALLBACK do for me? Is it absolutely required, or can I leave that declaration off?

like image 650
Jonah Bishop Avatar asked Feb 28 '13 02:02

Jonah Bishop


1 Answers

It is required, __stdcall is not the default calling convention on most compilers. __cdecl is the normal default. Having the calling convention wrong will make your program misbehave in a hard to diagnose way. The argument values are wrong and the stack gets imbalanced.

It is only an issue in 32-bit code, 64-bit code has only one calling convention. Whomever adds another one is going to be summarily banned to a small island in the South Atlantic for life. __stdcall is an historic accident that started with the pascal calling convention in DOS and 16-bit Windows, back when shaving an instruction off the call was important when so little memory was available.

like image 144
Hans Passant Avatar answered Sep 25 '22 16:09

Hans Passant