_ Stdcall and _ cdel

Source: Internet
Author: User

1. _ cdecl

(1) c Declaration stands for c Declaration, indicating the default function call method of C language. It is actually the default function call method of c ++.

(2). All parameters are pushed to the stack from right to left. These parameters are cleared by the caller, which is called manual stack clearing. Specifically, call the function of the caller-> execute the called function-> return the result of the called function-> clear the adjustment stack of the caller.

(3) The called function does not require the caller to pass many parameters. Too many or too few parameters are passed by the caller, and even different parameters do not produce errors in the compilation phase. In general, the number of parameters of a function is variable (just like that of a printf function), because only the caller knows how many parameters it passes to the called function, and the stack can be adjusted properly at the end of the call.

(4). Because a piece of code to adjust the stack needs to be generated for each call, the final file generated is large.

 

2. _ stdcall (callback/winapi)

(1). It is short for standard call. To use this call method, you must add _ stdcall to the function name. Generally, _ Win32 API
It should be the _ stdcall call rule. If a DLL written in VC ++ is to be called by a program written in other languages, the function call method should be declared as _ stdcall. winapi adopts this method.

(2). All parameters are pushed to the stack from right to left. If it is a member of the call class, the last one is the this pointer. Details: Call the function of the caller-> execute the called function->
The called party clears the adjustment stack and returns the result of the called function.

(3 ). the parameters in the stack are cleared after the called function returns. The command is retn X, and X indicates the number of bytes occupied by the parameter, the CPU automatically pops up X bytes of stack space after ret. It is called automatic stack clearing.

(4) the number of parameters must be determined when the function is compiled, and the caller must strictly control the generation of parameters. There must be no more or fewer parameters. Otherwise, an error will occur after the return. In general, the number of parameters of a function cannot be variable. It is modified from _ cdecl, _ stdcall does not support variable parameters, and stack clearing is the responsibility of the caller. Others are the same.

(5). Because you only need to generate a code to adjust the stack at the place where the function is called, the final file generated is small.

 

3. Pascal is a function call method in PASCAL language and can also be used in C/C ++. The parameter pressure stack sequence is opposite to that of the first two. The method of clearing the stack when the return result is missing...

 

4. _ fastcall is a quick call method specified by the compiler. Because most function parameters are few, it is time-consuming to use stack transfer. Therefore, _ fastcall usually specifies that the first two (or several) parameters are passed by the register, and the other parameters are passed through the stack. The registers defined by programs compiled by different compilers are different. The return method is equivalent to _ stdcall.

 

5. _ thiscall is specified to solve this pointer passing in class member calls. _ Thiscall requires that this pointer be placed in a specific register, which is determined by the compiler. VC uses ECx and Borland's c ++ compiler uses eax. The return method is equivalent to _ stdcall.

 

6. The registers involved in _ fastcall and _ thiscall are determined by the compiler. Therefore, they cannot be used as cross-compiler interfaces. Therefore, the COM object interface on Windows is defined as the _ stdcall call method.

 

7. C does not indicate that the default function is the _ cdecl method (this method can only be used in c). c ++ is the same, but the default call method can be set in the IDE environment.

 

8. Functions with variable parameters must and can only use the _ cdecl method. For example, the following function:

Intprintf (char * fmtstr ,...);

Int scanf (char * fmtstr ,...);

9. function name Modification

(1 ). _ cdecl: For _ cdecl, if it is defined in the C program file (the compiler will use the extension name. c), the function name remains unchanged. For the output function defined in the C ++ program file, the function name is modified (see 10 ). To prevent the function name from being modified, there are two methods:. you can add the extern "C" to remove the function name modification; B. you can use. def file removes function name modification.

(2). _ stdcall: whether it is the output function in the C program file or the output function in the C ++ program file, the function name will be modified. The output functions defined in the C ++ program file seem more complex, similar to the case of _ cdecl. Method for removing the function name: You can only remove the function name through the. Def file.

10. function name modification rules:

(1). Why should I modify the function name:

The function name modifier is a string created by the compiler during compilation to specify the definition and prototype of the function. Link programs or other tools sometimes need to specify the name of the function to locate the correct position of the function. In many cases, programmers do not need to know the function name modification. The link program or other tools will automatically distinguish them. Of course, in some cases, you need to specify the function name modification. For example, in a C ++ Program, to allow the link program or other tools to match the correct function name, you must specify the name of some special functions (such as constructors and destructor) after the overload function. Another case where you need to specify the function name modification is to call C or C ++ functions in the assembler.

(2). C language:

For the _ stdcall call convention, the compiler and the linker add an underline prefix before the output function name, and add the "@" symbol after the function name and the number of bytes of its parameters, for example, _ functionname @ number. The _ cdecl call Convention only adds an underline prefix before the output function name, for example, _ functionname. _ Fastcall: Add a "@" symbol before the output function name, followed by a "@" symbol and the number of bytes of the parameter, for example, @ functionname @ number.

(3). c ++ language:

The function name modification rules of C ++ are complicated, but the information is more adequate. By analyzing and modifying the function name, we can not only know the function call method, return value type, number of parameters, or even parameter type. Whether _ cdecl ,__ fastcall or _ stdcall is called, the function modifier is a "?" Start, followed by the name of the function, followed by the START identifier of the parameter table and the parameter table spelled out according to the parameter type code. For the _ stdcall method, the start identifier of the parameter table is "@ YG". For the _ cdecl method, it is "@ ya ", the _ fastcall method is "@ Yi ". The spelling code of the parameter table is as follows:

X -- void

D -- char

E -- unsignedchar

F -- short

H -- int

I -- unsignedint

J -- long

K -- unsignedlong (DWORD)

M -- float

N -- double

_ N-bool

U-struct

....

The pointer method is special. Pa is used to represent the pointer and Pb is used to represent the const type pointer. The following code indicates the pointer type. If a pointer of the same type appears consecutively, it is replaced by "0". A "0" indicates a repetition. The u table shows the structure type, which is usually followed by the type name of the struct. "@" is used to indicate the end Of the structure type name. The return value of a function is not specially processed. It is described in the same way as a function parameter, followed by the START sign of the parameter table. That is to say, the first item of the function parameter table actually represents the type of the return value of the function. The end of the name is marked with "@ Z" after the parameter table. If this function does not have a parameter, it ends with "Z. The following two examples show the following function declaration:

Intfunction1 (char * var1, unsigned long );

Its function modifier is "? Function1 @ yghpadk @ Z ", and for function declaration:

Oidfunction2 ();

The function modifier is "? Function2 @ ygxxz ".

For C ++ class member functions (whose call method is thiscall), the name modification of the function is slightly different from that of the non-member c ++ functions, first, insert the class name guided by the "@" character between the function name and the parameter table. Second, the start ID of the parameter table is different. Public) the member function identifier is "@ Qae", the Protection (protected) member function identifier is "@ IAE", private) the member function identifier is "@ AAE". If the function declaration uses the const keyword, the corresponding identifier should be "@ QBE ", "@ IBE" and "@ Abe ". If the parameter type is a reference of a class instance, "aav1" is used. For a reference of the const type, "abv1" is used ".

11. view the function name Modification

There are two ways to check the name modification of the function in your program: Compile the output list or use the dumpbin tool. Use the/FAC,/Fas, or/FACS command line parameters to allow the compiler to output a list of function or variable names. You can also use the dumpbin.exe/Symbols command to obtain the list of function or variable names in the OBJ file or lib file. In addition, you can use undname.exe to convert the modifier name to an unmodified form.

12. _ beginthread requires the thread function address of _ cdecl, and _ beginthreadex and _ createthread require the thread function address of _ stdcall.

13. # define callback _ stdcall // This is the legendary callback function.
# Define winapi _ stdcall // This is the legendary winapi
# Define winapiv _ cdecl
# Define apientry winapi // The dllmain entry is here
# Define apiprivate _ stdcall
# Define Pascal _ 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.