Search code examples
c++winapivisual-c++callbackcalling-convention

What is difference between callback function and regular function?


Ok I'll give two examples of function is using CALLBACK and regular function.
Note: I didn't write these examples.

Regular Function

int sumExample (int a, int b)
{
    return a + b;
}
int main()
{
     int = sumExample(1, 3);
     cout  >> int;
     return 0;
}

Function using _stdcall

int __stdcall sumExample (int a, int b);

what is the difference?
Note: I'm not sure how Calling Conventions works, an example would help!


Solution

  • Basically, a calling convention specifies implementation details of how the function will be called. Most libraries use the Standard C calling convention - __cdecl. WinAPI however expects __stdcall. You only need to know two things about calling conventions: that they have to match, e.g., you can't convert a void(*)(int, int), which is implicitly a void(__cdecl *)(int, int), to a void(__stdcall *)(int, int), and that the default is __cdecl. CALLBACK is just a WinAPI #define so that they can change if they want to.