What does a “CALLBACK” declaration in C do?

后端 未结 4 1947
Happy的楠姐
Happy的楠姐 2020-12-01 18:47

I was looking through some code from the SDL library and came across a function declared like this:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wPar         


        
相关标签:
4条回答
  • 2020-12-01 19:32

    The "CALLBACK" is a calling convention. There are other kinds of calling conventions. CALLBACK is the same as __stdcall.

    http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557

    Some more information at Raymond Chen's blog:

    http://blogs.msdn.com/oldnewthing/archive/2004/01/08/48616.aspx

    0 讨论(0)
  • 2020-12-01 19:37

    It's the calling convention. It's required when you pass a pointer to this function to a Windows API which later calls that function. The Windows calling convention is different from the C calling convention, therefore you need to specify to the compiler that WndProc() is special, and that it needs different startup and cleanup code.

    0 讨论(0)
  • 2020-12-01 19:42

    Short roundup from Raymond Chen's Blog:

    The great thing about calling conventions on the x86 platform is that there are so many to choose from!

    C calling convention (__cdecl)

    The C calling convention is constrained because it allows the use of functions with a variable number of parameters. It pretty much requires that the stack be caller-cleaned and that the parameters be pushed right to left, so that the first parameter is at a fixed position relative to the top of the stack. In summary: Caller cleans the stack, parameters pushed right to left.

    Pascal calling convention (__pascal)

    Pascal does not support functions with a variable number of parameters, so it can use the callee-clean convention. Parameters are pushed from left to right. Nearly all Win16 functions are exported as Pascal calling convention. The callee-clean convention saves three bytes at each call point, with a fixed overhead of two bytes per function. It was also fractionally faster. On Win16, saving a few hundred bytes and a few cycles was a big deal. Note: The Fortran calling convention (__fortran) is the same as the Pascal calling convention

    0 讨论(0)
  • 2020-12-01 19:47

    It's a calling convention, and Delphi has them too. Try looking up 'cdecl' in the Delphi Help. In Delphi (or Object Pascal as we oldies like to call it) calling conventions come after the function declaration, like this;

    function MyFunction(X, Y: Real): Real; cdecl;
    
    0 讨论(0)
提交回复
热议问题