C#
and C The reason for the CCP in P/Invoke cdecl
__stdcall
In the P/Invoke interoperability between C# and C, the C# function often appears to use the
signature situation. This is not matched with the choice of calls that have continued to this day. cdecl
__stdcall
__stdcall
Inherited the default calling agreement between the 16 -bit Pascal calling agreement, the default calling agreement between the Windows API function and COM. It stipulates that the call (function that is being called) is cleaned up after executing, making it more compact in an environment with limited resources. However, if the number of parameters expected by the caller and the required person does not match, this advantage will bring the risk of unbalanced stack.
: The caller of the variable parameter function cleans __stdcall
__cdecl
. This agreement puts the responsibility of cleaning the stack on the caller, because the caller knows the number of parameters passed. Forgetting in the statement of C# is a common trap.
__cdecl
: The hidden this pointer of the C method printf()
scanf()
[DllImport]
Special refers to C, which is used to process the hidden CallingConvention.Cdecl
pointer in the instance method of the class. It is similar to , but the .NET P/Invoke sealing processing program does not support it, so it is not suitable for Pinvoke C code.
Summary __thiscall
C#
signature coexistence in C is the result of these historical calls and specific requirements for each language. Correctly understand these agreements, and the __thiscall
and this
properties of __cdecl
, which is important for ensuring the correct P/Invoke function and avoiding potential problems (such as stack imbalances).
The above is the detailed content of Why Do C# `Cdecl` Calls and C `__stdcall` Signatures Coexist in P/Invoke?. For more information, please follow other related articles on the PHP Chinese website!