What is the difference between winapi and callback?
What are the meanings and differences between the keywords _ stdcall _ cdecl _ Pascal _ fastcall?
First, let's look at the explanations provided in msdn, but some of them are not detailed.
Winapi
· Use in place of far Pascal in API declarations. If you are writing a DLL with exported API entry points, you can use this for your own APIs.
Callback
· Use in place of far Pascal in application callback routines such as window procedures and dialog procedures.
Let's see what these two macros are.
VC: windef. h
# Define callback Pascal // = _ Pascal. VC does not support direct use of _ Pascal.
# Define winapi cdecl // = _ cdecl
BCB: windef. h
# Define callback _ stdcall
# Define winapi _ stdcall
Some Rare keywords such as cdecl stdcall are introduced.
So what do cdecl, Pascal, stdcall, fastcall and other modifiers mean?
It is very simple, that is, some descriptions of the stack. The first is the function parameter pressure stack sequence, and the second is
Who clears the content pushed into the stack, the caller or the function itself?
These switches are used to tell the compiler what kind of assembly code is generated.
The differences are listed below:
Directive Parameter order Clean-up Passes parameters in registers?
Register left-to-right routine Yes
Pascal left-to-right routine no
Cdecl right-to-left caller No
Stdcall right-to-left routine no
Safecall right-to-left routine no
Simple Description:
_ Cdecl is the call Convention used by C/C ++ and MFC programs by default. You can also add the _ cdecl keyword when declaring a function. When _ cdecl is used, function parameters are pushed to the stack in the order from right to left, and the caller pops up the parameter stack to clear the stack. Therefore, a function that implements variable parameters can only use this call convention. Every function using the _ cdecl Convention must contain the code for clearing the stack, so the size of the executable file generated will be relatively large. _ Cdecl can be written as _ cdecl.
_ Stdcall is used to call Win32 API functions. When _ stdcal is used, function parameters are written to the stack in the order from right to left. The called function clears the stack of the transfer parameter before returning, and the number of function parameters is fixed. Because the function body knows the number of passed parameters, the called function can directly clear the stack of the passed parameters with a RET n command before returning the result. _ Stdcall can be written as _ stdcall.
_ Fastcall conventions are used in scenarios with high performance requirements. _ Fastcall: The two DWORD parameters starting from the left of the function are placed in the ECX and EDX registers respectively, the other parameters are still transmitted from the right to the left pressure stack. The called function clears the stack of the transfer parameter before returning. _ Fastcall can be written as _ fastcall.
· Special instructions
1. The _ cdecl method is used by default, so it can be omitted.
2. winapi is generally used to modify the export function in the dynamic link library.
3. Callback is only used to modify callback functions.
4. You may have discovered that the winapi definition in VC is different from that in BCB, So you at least understand
One reason why vc dll cannot be called directly from BCB is that.
Attached to the csdn expert's answer, I think it makes sense:
The following definitions are available in the windef. h header file:
# Define winapi _ stdcall
# Define apientry winapi
There are two function calling methods for VC: _ stdcall and _ cdecl.
There are two ways to call a function: Pascal call and C call.
The Pascal call method is used. The function deletes the parameter from the stack before returning it to the caller.
Using the C call method, the parameter is deleted by the caller.
The winmain function is called by the system. Windows system requires that all functions called by the system follow the Pascal call method.
However, in VC, the default function call method is _ cdecl, that is, the C call method.
So the Declaration displayed before winmain.
In Windows programming, many declarative modifiers such as Callback, winapi, and Pascal are displayed on intelcpu computers as _ stdcall.
For detailed declaration details, see windef. h.