Function call and name Modification

Source: Internet
Author: User

I. Concepts of function calls

_ Stdcall, _ cdecl ,...... Left passed through the stack. The called function clears the memory stack of the transfer parameter before returning, but the difference is the function name modifier section (the modification section of the function name will be described in detail later ).
1. _ stdcall is the default calling method of the PASCAL program. It is usually used in Win32 API. The function uses the stack pressure method from right to left and clears the stack when it exits. After compiling a function, VC adds an underline prefix to the function name, and adds "@" and the number of bytes of the parameter to the function name.
2. c call conventions (which are described by the _ cdecl keyword) are pushed to the stack in sequence from right to left. The caller pushes the parameters to the stack. The memory stack of the transfer parameter is maintained by the caller (because of this, the function that implements the variable parameter 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 the code to clear the stack. Therefore, the size of the executable file generated is larger than that of the call to the _ stdcall function. The function uses the stack pressure mode from right to left. After compiling a function, VC adds an underline prefix to the function name. Is the default MFC call convention.
3. _ fastcall: The call convention is "person" as its name. Its main feature is fast because it transmits parameters through registers (in fact, it uses ECx and EDX to transmit the first two DWORD or smaller parameters, and the remaining parameters are still transmitted from the right to the left pressure stack, the called function clears the memory stack of the transfer parameter before returning). In terms of the function name modification conventions, it is different from the previous two. _ Fastcall functions use registers to pass parameters. After compiling a function, VC adds the "@" prefix to the function name, and adds "@" and the number of parameters 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 pressed from right to left. Thiscall is not a keyword and cannot be specified by programmers.
5. the naked call uses 1-4 call timing. If necessary, the compiler will generate code to save the ESI, EDI, EBX, and EBP registers when entering the function, when you exit the function, the code is generated to restore the content 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, or in the setting... Select/C ++/code generation. When the keywords added before the output function are different from those selected in the compiling environment, the keywords directly added before the output function are valid. 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. Another thing worth mentioning is winapi macro, windows. h supports this macro. It can translate the function into an appropriate call convention. In Win32, it is defined as _ stdcall. You can use the winapi macro to create your own APIs.

Ii. Name modification conventions
1. The decoration name "C" or "C ++" function is identified by the modifier internally (Compilation and link. Modifier is a string generated by the compiler when compiling a function definition or prototype. In some cases, modifying the name of a function is necessary, for example, specifying the output "C ++" overload function, constructor, and destructor in the module definition file, for example, call the "C" or "C ++" function in the assembly code. Modifier names are jointly determined by function names, class names, call conventions, return types, parameters, and so on.
2. The name Modification Convention varies with the call convention and the compilation type (C or C ++. The function name modification conventions vary with the compilation type and call conventions.
Rules for modifying function names during C Compilation:
_ Stdcall: prefix an underline before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is _ functionname @ number.
The _ cdecl call Convention only adds an underline prefix before the output function name in the format of _ functionname.
_ 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. They do not change the case sensitivity of the output function name. This is different from Pascal's call conventions. Pascal's output function names are not modified and all are capitalized.
Rules for modifying function names during C ++ Compilation:
_ Stdcall: 1. Use "?" Start of the identification function name, followed by the function name; 2. After the function name, start with "@ YG" to identify the parameter table, followed by the parameter table; 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. the pointer ID is prior to the Data Type indicated by the parameter; 5. mark the end of the entire name 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 ", for example, int test1 (char * var1, unsigned long) --"? Test1 @ yghpadk @ Z "Void Test2 () --"? Test2 @ ygxxz"
_ Cdecl call Convention: the rule is the same as the _ stdcall call Convention above, but the start mark of the parameter table is changed from "above" to "@ ya ".
_ Fastcall call Convention: the rule is the same as the _ stdcall call Convention above, except that the start mark of the parameter table is changed from "@ YG" to "@ Yi ". The "_ cedcl" Declaration for the function can only be called by C/C ++. CB uses the default values of the four modifiers/_ cdecl CB in the output function declaration. It will add _ before the output function name and keep this function name unchanged, the parameters are passed to the stack in sequence from right to left, and can also be written as _ cdecl and cdecl. // _ Fastcall the parameters of the function modified by her will be processed with registers. The function name is preceded by @, and the parameters are pushed to the stack in order from left to right; // _ Pascal it indicates that the function name uses the naming convention in Pascal format. In this case, all function names are capitalized. The parameters are pushed from left to right. // _ stdcall uses the standard function name. The function name will not change. When _ stdcall is used. The parameter can be set to _ stdcall in the order from right to left;
Iii. Others
-----------------------
1. Call conventions:
_ Cdecl is the default C format naming convention of Borland C ++ by default. It underlines before the identifier to keep all its original full identifiers. The parameter is passed to the stack based on the rightmost parameter priority principle, and then the stack is cleared. Extern "C" bool _ cdecl testfunction (); testfunction @ 1 is displayed in the def file. Note: @ 1 indicates the number of functions in sequence, which will be used when an alias is used.
In _ Pascal format, all function names are written in uppercase. The first parameter is used to press the stack and then clear the stack. Testfunction @ 1 // def file _ stdcall standard call the last parameter to first press the stack and then clear the stack. Testfunction @ 1 // def file _ fastcall: Pass the parameter to the first parameter of the Register, first press the stack, and then clear the stack. @ Testfunction @ 1 // def File
2. Solution call conventions:
The difference between Microsoft and Borland's _ stdcall is the naming method. Borland uses the _ stdcall method to remove the underline before the name. Microsoft is prefixed with an underscore, followed by @, followed by the number of bytes reserved for the stack. The number of bytes depends on the space occupied by the parameter in the stack. Every parameter is rounded to a multiple of 4. The Miocrosoft DLL is different from the system DLL.
Parameters are used in different stack-to-stack modes. For more details, see the following.
---------------------
When you declare a procedure or function, you can specify a calling convention using one of the Directives register, Pascal, cdecl, stdcall, and safecall. for example, function myfunction (X, Y: Real): real; cdecl ;... Calling conventions determine the order in which parameters are passed to the routine. they also affect the removal of parameters from the stack, the use of registers for passing parameters, and error and exception handling. the default calling convention is register. the register and Pascal conventions PASS Parameters from left to right; that is, the left most parameter is evaluated and passed fi RST and the rightmost parameter is evaluated and passed last. the cdecl, stdcall, and safecall conventions PASS Parameters from right to left. for all conventions except T cdecl, the procedure or function removes parameters from the stack upon returning. with the cdecl convention, the caller removes parameters from the stack when the call returns. the register Convention uses up to three CPU registe RS to pass parameters, while the other conventions pass all parameters on the stack. the safecall Convention implements exception "firewils. "On Windows, This implements interprocess com Error Notification. the table below summarizes calling conventions. calling conventions Directive Parameter order Clean-up Passes parameters in registers? Register left-to-right routine Yes Pascal left-to-right routine no cdecl right-to-left caller no stdcall right-to-left routine no safecall right-to-left routine no the default register Convention is the most efficient, since it usually avoids creation of a stack frame. (Access methods for published properties must use register .) the cdecl convention is useful when you call functions from shared libraries written in C or C ++, while stdcall and safecall are recommended, in general, for callto external code. on Windows, the operating system APIs are stdcall and safecall. other operating systems generally use cdecl. (Note that stdcall is more efficient than cdecl .) the safecall Convention must be used for declaring dual-interface methods. the Pascal convention is maintained for backward compatibility. for more information on calling conventions, see program control. the directives near, far, and export refer to calling conventions in 16-bit windows programming. they have no effect in 32-bit applications and are maintained for backward compatibility only.

 

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.