Function call rules (_ cdecl ,__ stdcall ,__ fastcall ,__ Pascal ).

Source: Internet
Author: User
Tags repetition

You do not need to know about the Function calling rules (call conventions) most of the time. However, if you need cross-language programming, such as calling a DLL written by VC using Delphi, you need to know.

Microsoft's VC adopts the _ cdecl method by default, while Windows API adopts the _ stdcall method. If you use VC to develop DLL for other languages, you should specify the _ stdcall method. It is very important to know who clears the stack. If you want to write an assembly function to call C, you must be careful with Stack clearing. If it is a function in the _ cdecl mode, then the function itself (if you do not need to compile the Code) does not need to care about clearing the stack for storing parameters. However, if it is a _ stdcall rule, you must exit the function (RET) stack.
1. _ cdecl
The so-called C call rule. The parameter is pushed from right to left to the stack. The caller pushes the parameter to the stack. Remember: The Memory stack of the transfer parameter is maintained by the caller. The returned value is in eax. Therefore, this rule must be used for functions that change parameters like printf. During compilation, the compiler only adds an underline prefix to the name of the function that calls the rule in the format of _ functionname.
2. _ stdcall
The parameter is pushed from right to left to the stack. The parameter is pushed to the stack by the caller. _ Stdcall is the default calling method of the PASCAL program. It is usually used in Win32 APIs. Remember: The function clears the stack when it exits, and the returned value is in eax. The _ stdcall call Convention adds an underline prefix before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is _ functionname @ number. For example, the int func (int,
Double B) the modifier is_ FUNC @ 12.
3. _ fastcall
_ Fastcall is fast because it transmits parameters through registers (in fact, it uses ECx and EDX to transmit the first two DWORD or smaller parameters, the remaining parameters are still transmitted from the right to the left pressure stack, and the called function clears the memory stack of the transfer parameter before returning ). _ Fastcall: add the "@" symbol before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is @ functionname @ number. This is very similar to _ stdcall. The only difference is that the first two parameters are transmitted through registers. Note that the two parameters transmitted through the Register are left-to-right, that is, the first parameter is added to ECx, and the third parameter is imported to EDX. Other parameters are transmitted from right to left into the stack. The returned results still pass through eax.
4. _ Pascal
This rule transmits parameters from left to right and is returned through eax. the stack is cleared by the caller.

5. _ thiscall

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

Call conventions can be selected through the Project Settings: setting.../C ++/code generation item. The default status is _ cdecl.

 

Naming Conventions:

1. decoration name: "C" or "C ++" functions are internally (compiled and linked) identified by modifier names.
2. Agreed rules for function name modification during C Compilation:
_ Stdcall indicates that an underline prefix is added before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is_ Functionname @ numberFor example: function (int A, int B), whose name is:_ FUNCTION @ 8
The _ cdecl call Convention only adds an underline prefix before the output function name in the format of _ functionname.
_ Fastcall: Add a "@" symbol before the output function name, followed by a "@" symbol and the number of bytes of the parameter. The format is @ functionname @ number.

3. Agreed rules for function name modification during C ++ Compilation:
_ Stdcall call conventions:
1) "? "Mark the start of the function name, followed by the function name;
2) The function name starts with "@ YG", followed by the parameter table;
3) The parameter table is represented in code:
X -- void,
D -- char,
E -- unsigned char,
F -- short,
H -- int,
I -- unsigned int,
J -- long,
K -- unsigned long,
M -- float,
N -- double,
_ N -- bool,
Pa -- indicates the pointer. The code behind the pointer indicates the pointer type. If a pointer of the same type appears consecutively, it is replaced by "0". A "0" indicates a repetition;
4) the first item of the parameter table is the type of the return value of the function, followed by the Data Type of the parameter, and the pointer ID is before the data type referred to by the function;
5) The end of the name is marked with "@ Z" after the parameter table. If this function has no parameter, it ends with "Z.
The format is"? Functionname @ YG ***** @ Z"Or"? Functionname @ YG * xz", Such
Int test1 (char * var1, unsigned long) ----- "? Test1 @ yghpadk @ Z"
Void Test2 ()----- "? Test2 @ ygxxz"

 

_ Cdecl:
The rules are the same as the _ stdcall call Convention above, except that the start mark of the parameter table is changed from "@ YG" to "@ ya ".
_ Fastcall:
The rules are the same as the _ stdcall call Convention above, except that the start identifier of the parameter table changes from "@ YG" to "@ Yi ".
The "_ cedcl" Declaration for the function can only be called by C/C ++.

Note:
1. _ beginthread requires the thread function address of _ cdecl, and _ stdcall for _ beginthreadex and createthread.

2. Generally, Win32 functions are _ stdcall. Windef. H has the following definitions:

#define CALLBACK __stdcall#define WINAPI  __stdcall

3,

extern "C" _declspec(dllexport) int __cdecl Add(int a, int b);   typedef int (__cdecl*FunPointer)(int a, int b);

The modifier is written in the above Order.

4. Function of extern "C": If add (int A, int B) is compiled in the C language compiler and used in the C ++ file, you need to declare in the C ++ file: extern "C" add (int A, int B ), because the C compiler and C ++ compiler have different interpretations of function names (function parameters must be considered when the C ++ compiler interprets a function name, which facilitates function overloading, in C language, there is no function overload problem). The essence of using extern "C" is to tell the C ++ compiler that this function is a function in the C library. If you do not use extern "C", a link error occurs.
It is generally used as follows:

#ifdef _cplusplus #define ETERN_C extern "C"#else#define EXTERN_C extern#endif#ifdef _cplusplus extern "C"{#endif EXTERN_C int func(int a, int b); #ifdef _cplusplus } #endif

5. MFC provides some macros. You can use afx_ext_class to replace _ declspec (dllexport) and modify the class name to export the class, afx_api_export to modify the function, and afx_data_export to modify the variable.

AFX_CLASS_IMPORT:__declspec(DLLexport)AFX_API_IMPORT:__declspec(DLLexport)AFX_DATA_IMPORT:__declspec(DLLexport)AFX_CLASS_EXPORT:__declspec(DLLexport)AFX_API_EXPORT:__declspec(DLLexport)AFX_DATA_EXPORT:__declspec(DLLexport)AFX_EXT_CLASS:#ifdef _AFXEXT     AFX_CLASS_EXPORT        #else    AFX_CLASS_IMPORT

6. dllmain is responsible for initialization and termination. Whenever a new process or a new thread of the Process accesses the DLL, or when every process or thread accessing the DLL no longer uses the DLL or ends, it will call dllmain. However, using terminateprocess or terminatethread to end a process or thread does not call dllmain.

7. One dll has only one instance in the memory.
The relationship between the DLL program and the program that calls its output function:
1) Relationship between DLL and process and thread
The DLL module is mapped to the virtual address space of the process that calls it.
The dll Memory is allocated from the virtual address space of the calling process and can only be accessed by the thread of the process.
The DLL handle can be used by the calling process, and the call Process Handle can be used by the DLL.
Dlldll can have its own data segment but does not have its own stack. It uses the stack of the calling process, which is the same as the stack mode of the application that calls it.

2) about shared data segments
The global variables defined by dll can be accessed by the calling process; The dll can access the global data of the calling process. Each process using the same dll has its own DLL global variable instance. If multiple threads concurrently access the same variable, you need to use the synchronization mechanism. For a DLL variable, If you want each thread that uses the DLL to have its own value, the local thread storage should be used (TLS, Thread Local strorage ).

You do not need to know about the Function calling rules (call conventions) most of the time. However, if you need cross-language programming, such as calling a DLL written by VC using Delphi, you need to know.

Microsoft's VC adopts the _ cdecl method by default, while Windows API adopts the _ stdcall method. If you use VC to develop DLL for other languages, you should specify the _ stdcall method. It is very important to know who clears the stack. If you want to write an assembly function to call C, you must be careful with Stack clearing. If it is a function in the _ cdecl mode, then the function itself (if you do not need to compile the Code) does not need to care about clearing the stack for storing parameters. However, if it is a _ stdcall rule, you must exit the function (RET) stack.
1. _ cdecl
The so-called C call rule. The parameter is pushed from right to left to the stack. The caller pushes the parameter to the stack. Remember: The Memory stack of the transfer parameter is maintained by the caller. The returned value is in eax. Therefore, this rule must be used for functions that change parameters like printf. During compilation, the compiler only adds an underline prefix to the name of the function that calls the rule in the format of _ functionname.
2. _ stdcall
The parameter is pushed from right to left to the stack. The parameter is pushed to the stack by the caller. _ Stdcall is the default calling method of the PASCAL program. It is usually used in Win32 APIs. Remember: The function clears the stack when it exits, and the returned value is in eax. The _ stdcall call Convention adds an underline prefix before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is _ functionname @ number. For example, the int func (int,
Double B) the modifier is_ FUNC @ 12.
3. _ fastcall
_ Fastcall is fast because it transmits parameters through registers (in fact, it uses ECx and EDX to transmit the first two DWORD or smaller parameters, the remaining parameters are still transmitted from the right to the left pressure stack, and the called function clears the memory stack of the transfer parameter before returning ). _ Fastcall: add the "@" symbol before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is @ functionname @ number. This is very similar to _ stdcall. The only difference is that the first two parameters are transmitted through registers. Note that the two parameters transmitted through the Register are left-to-right, that is, the first parameter is added to ECx, and the third parameter is imported to EDX. Other parameters are transmitted from right to left into the stack. The returned results still pass through eax.
4. _ Pascal
This rule transmits parameters from left to right and is returned through eax. the stack is cleared by the caller.

5. _ thiscall

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

Call conventions can be selected through the Project Settings: setting.../C ++/code generation item. The default status is _ cdecl.

 

Naming Conventions:

1. decoration name: "C" or "C ++" functions are internally (compiled and linked) identified by modifier names.
2. Agreed rules for function name modification during C Compilation:
_ Stdcall indicates that an underline prefix is added before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is_ Functionname @ numberFor example: function (int A, int B), whose name is:_ FUNCTION @ 8
The _ cdecl call Convention only adds an underline prefix before the output function name in the format of _ functionname.
_ Fastcall: Add a "@" symbol before the output function name, followed by a "@" symbol and the number of bytes of the parameter. The format is @ functionname @ number.

3. Agreed rules for function name modification during C ++ Compilation:
_ Stdcall call conventions:
1) "? "Mark the start of the function name, followed by the function name;
2) The function name starts with "@ YG", followed by the parameter table;
3) The parameter table is represented in code:
X -- void,
D -- char,
E -- unsigned char,
F -- short,
H -- int,
I -- unsigned int,
J -- long,
K -- unsigned long,
M -- float,
N -- double,
_ N -- bool,
Pa -- indicates the pointer. The code behind the pointer indicates the pointer type. If a pointer of the same type appears consecutively, it is replaced by "0". A "0" indicates a repetition;
4) the first item of the parameter table is the type of the return value of the function, followed by the Data Type of the parameter, and the pointer ID is before the data type referred to by the function;
5) The end of the name is marked with "@ Z" after the parameter table. If this function has no parameter, it ends with "Z.
The format is"? Functionname @ YG ***** @ Z"Or"? Functionname @ YG * xz", Such
Int test1 (char * var1, unsigned long) ----- "? Test1 @ yghpadk @ Z"
Void Test2 ()----- "? Test2 @ ygxxz"

 

_ Cdecl:
The rules are the same as the _ stdcall call Convention above, except that the start mark of the parameter table is changed from "@ YG" to "@ ya ".
_ Fastcall:
The rules are the same as the _ stdcall call Convention above, except that the start identifier of the parameter table changes from "@ YG" to "@ Yi ".
The "_ cedcl" Declaration for the function can only be called by C/C ++.

Note:
1. _ beginthread requires the thread function address of _ cdecl, and _ stdcall for _ beginthreadex and createthread.

2. Generally, Win32 functions are _ stdcall. Windef. H has the following definitions:

#define CALLBACK __stdcall#define WINAPI  __stdcall

3,

extern "C" _declspec(dllexport) int __cdecl Add(int a, int b);   typedef int (__cdecl*FunPointer)(int a, int b);

The modifier is written in the above Order.

4. Function of extern "C": If add (int A, int B) is compiled in the C language compiler and used in the C ++ file, you need to declare in the C ++ file: extern "C" add (int A, int B ), because the C compiler and C ++ compiler have different interpretations of function names (function parameters must be considered when the C ++ compiler interprets a function name, which facilitates function overloading, in C language, there is no function overload problem). The essence of using extern "C" is to tell the C ++ compiler that this function is a function in the C library. If you do not use extern "C", a link error occurs.
It is generally used as follows:

#ifdef _cplusplus #define ETERN_C extern "C"#else#define EXTERN_C extern#endif#ifdef _cplusplus extern "C"{#endif EXTERN_C int func(int a, int b); #ifdef _cplusplus } #endif

5. MFC provides some macros. You can use afx_ext_class to replace _ declspec (dllexport) and modify the class name to export the class, afx_api_export to modify the function, and afx_data_export to modify the variable.

AFX_CLASS_IMPORT:__declspec(DLLexport)AFX_API_IMPORT:__declspec(DLLexport)AFX_DATA_IMPORT:__declspec(DLLexport)AFX_CLASS_EXPORT:__declspec(DLLexport)AFX_API_EXPORT:__declspec(DLLexport)AFX_DATA_EXPORT:__declspec(DLLexport)AFX_EXT_CLASS:#ifdef _AFXEXT     AFX_CLASS_EXPORT        #else    AFX_CLASS_IMPORT

6. dllmain is responsible for initialization and termination. Whenever a new process or a new thread of the Process accesses the DLL, or when every process or thread accessing the DLL no longer uses the DLL or ends, it will call dllmain. However, using terminateprocess or terminatethread to end a process or thread does not call dllmain.

7. One dll has only one instance in the memory.
The relationship between the DLL program and the program that calls its output function:
1) Relationship between DLL and process and thread
The DLL module is mapped to the virtual address space of the process that calls it.
The dll Memory is allocated from the virtual address space of the calling process and can only be accessed by the thread of the process.
The DLL handle can be used by the calling process, and the call Process Handle can be used by the DLL.
Dlldll can have its own data segment but does not have its own stack. It uses the stack of the calling process, which is the same as the stack mode of the application that calls it.

2) about shared data segments
The global variables defined by dll can be accessed by the calling process; The dll can access the global data of the calling process. Each process using the same dll has its own DLL global variable instance. If multiple threads concurrently access the same variable, you need to use the synchronization mechanism. For a DLL variable, If you want each thread that uses the DLL to have its own value, the local thread storage should be used (TLS, Thread Local strorage ).

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.