Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "CALLBACK" in a Windows API function declaration mean? [duplicate]

Tags:

windows

winapi

Possible Duplicate:
What does a “CALLBACK” declaration in C do?

The prototype of a WindowProc() function is this

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message , WPARAM wParan, LPARAM lParam);

But I've never come across the CALLBACK keyword, I only know that a callback function is one you pass a pointer to another function to, so what exactly is this CALLBACK over here? and what does it do?

like image 926
Ghost Avatar asked Jun 16 '12 18:06

Ghost


1 Answers

It's #defined as __stdcall, because that's what the Win32 API assumes about callback functions. It's a calling convention - it describes the way a function call is arranged on the low level - how are parameters arranged on the CPU stack, and suchlike. The assumptions about the expected stack layout (i. e. the calling convention) must match between the caller and the callee, otherwise all kinds of fun consequences might ensue.

Historically, on the Intel CPUs there were multiple calling conventions; more if you count non-Microsoft compilers. Even more if you count languages other than C[++] that are capable of calling and getting called by Windows API. Making sure your Windows API callbacks are explicitly marked as __stdcall to match the WinAPI expectation is a good practice. In some cases, depending on compiler and settings, __stdcall is the default calling convention (i. e. you can safely omit CALLBACK), but not always.

Back in Win16, CALLBACK was defined as far pascal. That's even less likely to be the default for user functions, especially in a C program. The fact that callbacks were assumed to have a Pascal calling convention was an historical artifact. It also worked as a microoptimization.

like image 168
Seva Alekseyev Avatar answered Oct 15 '22 09:10

Seva Alekseyev