Home > Backend Development > C++ > Why Do C# `Cdecl` Calls and C `__stdcall` Signatures Coexist in P/Invoke?

Why Do C# `Cdecl` Calls and C `__stdcall` Signatures Coexist in P/Invoke?

Mary-Kate Olsen
Release: 2025-01-25 23:41:10
Original
348 people have browsed it

Why Do C# `Cdecl` Calls and C   `__stdcall` Signatures Coexist in P/Invoke?

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

call agreement, and the C corresponding function uses the

signature situation. This is not matched with the choice of calls that have continued to this day. cdecl __stdcall

: The caller of the Windows function cleans

__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

is the standard calling agreement in C, which is used to have a function with variable quantitative parameters, such as and __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#

call and

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template