C # How to Set callingconvention when importing DLL

Source: Internet
Author: User

C # The method for calling an unmanaged. dll file is as follows:

 [DllImport("XORDll.dll",            EntryPoint = "OutEncrypt",            CharSet = CharSet.Ansi,            CallingConvention = CallingConvention.StdCall)           ]        public static extern int OutEncrypt(string FilePath, string SecretWord); 

Among them, callingconvention. There are five ways:

CallingConvention = CallingConvention.StdCallCallingConvention = CallingConvention.CdeclCallingConvention = CallingConvention.FastCallCallingConvention = CallingConvention.ThisCallCallingConvention = CallingConvention.Winapi

"By default, C and C ++ use cdecl-but using alling uses stdcall to match the Windows API ."

By default, C and C ++ use cdecl calls, but the group uses stdcall to call windows
API

It is not clear how to call fastcall, thiscall, and winapi.

Reference: Baidu Library http://wenku.baidu.com/view/70f06e48cf84b9d528ea7ae9.html

#define   CALLBACK         __stdcall    #define   WINAPI           __stdcall    #define   WINAPIV          __cdecl    #define   APIENTRY          WINAPI    #define   APIPRIVATE       __stdcall    #define   PASCAL           __stdcall

Calling convention: determines the order of the incoming and outgoing stacks when the function parameter is transferred. The caller or the caller pushes the parameter to the stack and the modifier Convention used by the compiler to identify the function name.


There are many function call conventions. Here is a simple introduction:
1. The _ stdcall call convention is equivalent to the Pascal call convention that is frequently used in 16-bit dynamic libraries.
In 32-bit VC ++ 5.0, Pascal's call Convention is no longer supported (in fact, it has been defined
It is _ stdcall. Except for _ Pascal, __fortran and _ syscall are not supported .)
It is the _ stdcall call convention. The two are essentially the same, that is, the function parameter is in the right direction.
Left passed through the stack, the called function clears the memory stack of the transfer parameter before returning, but different
Is the modifier part of the function name (the modifier part of the function name will be described in detail later ).
_ Stdcall is the default calling method of PASCAL program, which is usually used in Win32 API.
Use the stack pressure method from right to left to clear the stack when you exit. After compiling the function
Prefix with an underscore before the function name, and add "@" and the number of bytes of the parameter after the function name.


2. c call conventions (which are described by the _ cdecl keyword)
Stack. The caller pops up the parameter stack. The memory stack of the transfer parameter is maintained by the caller.
(Because of this, the function that implements variable parameters can only use this call Convention ). In addition,
The function name modification conventions are also different.
_ Cdecl is the default call Method for C and C ++ programs. Every function that calls it contains clear
Empty stack code. Therefore, the size of the executable file generated is larger than that of the _ stdcall function. Letter
The number is pushed from right to left. After compiling the function, VC will underline the function name.
Prefix. Is the default MFC call convention.


3. The _ fastcall call convention is "person" as its name. Its main feature is fast because it is a communication
Pass through the Register to transmit parameters (in fact, it uses ECx and EDX to transmit the first two double characters
(DWORD) or smaller parameters, the remaining parameters are still transferred from the right to the left pressure stack, called
The function clears the memory stack of the transfer parameter before returning.) In terms of the function name modification conventions, it and
The first two are different. _ Fastcall functions use registers to pass parameters. VC transfers the functions
After compilation, the "@" prefix is added before the function name, and the "@" and Parameter bytes are added after the function name.


4. thiscall is only applied to "C ++" member functions. This pointer is stored in the Cx register and the parameter is from the right
To the left. Thiscall is not a keyword and cannot be specified by programmers.


5. The Naked call uses the 1-4 call timing. If necessary, the compiler
Code is generated to save the ESI, EDI, EBX, and EBP registers. Code recovery is generated when the function is exited.
Repeat the contents of these registers. Naked call does not generate such code. The naked call is not a type.
Modifier, so it must be used together with _ declspec.


The keywords _ stdcall, _ cdecl, and _ fastcall can be directly added before the function to be output,
You can also select the setting.../C ++/code generation item in the compilation environment. When added to the input
The keywords before the function is output are not the same as those in the compiling environment.
Valid characters. Their corresponding command line parameters are/GZ,/GD, And/GR. The default status is/GD, Which is _ cdecl.


To fully imitate Pascal's call convention, you must first use the _ stdcall call Convention. As for the function name Modification Convention, you can use other methods to imitate it. The winapi macro is also worth mentioning,
Windows. h supports this macro. It can translate functions into appropriate call conventions. In Win32,
It is defined as _ stdcall. You can use the winapi macro to create your own APIs.

Summary:
The call conventions such as Pascal are all called by the function itself to clear the stack, and the functions of _ cdecl are
The caller clears the stack. In actual use, I think the biggest difference between the two is: __cdecl.
The number of parameters can be declared as uncertain, such as printf and scanf, while Pascal's function is not
You can do this. In this case, it does not know the number of arguments.
In VC: Pascal = callback = winapi ==__ stdcall

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.