_ Cdecl and _ stdcall

Source: Internet
Author: User

1. If the function func is _ cdecl (the default call method), the call process is as follows:

Int main ()
...{
// Stack pressure from right to left for parameters
Push 4
Push 3
Push 2
Push 1
Call func
Add ESP 0x10 // The caller restores the stack pointer esp. The size of the four parameters is 0x10 (4x4)
}

2. If the function func is _ stdcall, the calling process is as follows:
Int main ()
...{
// Stack pressure from right to left for parameters
Push 4
Push 3
Push 2
Push 1
Call func
// The call func is responsible for recovering the stack pointer. The method is "RET 0x10"
}

3. If the function func is _ Pascal, the call is as follows:
Int main ()
...{
// Press the stack from left to right for parameters
Push 1
Push 2
Push 3
Push 4
Call func
// The call func is responsible for recovering the stack pointer. The method is "RET 0x10"
}

3. If the function func is _ fastcall, the call is as follows:
Int main ()
...{
// Parameters are first transmitted using ECx, EDX, and eax, and then pushed to the stack.
// Do not enter the stack
// (For some reason, the help is written from left to right,
// Is it wrong or bcb6 is different from bcb5)
Push 4
MoV ECx 3
MoV edX 2
MoV eax 1
Call func
// The call func is responsible for recovering the stack pointer. The method is "RET 0x04 ",
// Resume with RET 0x04 because only one parameter is added to the stack and the rest are passed in registers.
}

Sender: huang_reporter

# Define callback _ stdcall

# Define winapi _ stdcall

Different names are defined only for "Hope-informed", just as hwnd and hcursor are of the same type.
They are all window functions (procedures )......

Sender: sinman
I have collected all the information.

Left passed through the stack, the called function clears the memory stack of the transfer parameter before returning, but the difference is the modified part of the function name.

_ 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. The C call Convention puts pressure parameters in the stack in the order from right to left. The caller pushes the parameters to the stack. The memory stack of the transfer parameter is maintained by the caller. 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 number of letters is pushed from right to left. After compiling a function, VC adds an underline prefix to the function name. 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 parameter or smaller parameter transmitted through registers, 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). 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. 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 environment... 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.

2) Name modification conventions

1. decoration name)

The "C" or "C ++" functions are internally identified by modifier names. 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 a and c Compilation:

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.

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.

B. Conventions for function name modification during C ++ Compilation:

_ Stdcall call conventions:
1. Take "?" Start of the function name, followed by the function name;
2. The parameter table starts with "@ YG" after the function name, 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. 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 ----- "? 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, but 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 four modifiers for output function declaration.
// _ Cdecl
The default value of CB. It will add _ before the output function name and keep this function name unchanged. parameters are passed to the stack in sequence from right to left, it can also be written as _ cdecl and cdecl.
// _ Fastcall
The parameters of the function modified by her will be processed by registers with the intention of adding @ before the function name, and the parameters will be pushed to the stack in the 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 pressed from left to right;
// _ Stdcall
Use 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;

Sender: echoher
Far is an ancient thing.

In the 16-Bit mode, the pointer is a 16-bit
The pointer's addressing space is only 64 KB.
If far is specified, the base address must be added to the address pointed to by this pointer.
That is to say, you can use far to specify a region other than 64 KB.

It's useless now
No use at all

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/kudoo/archive/2008/03/21/2201966.aspx

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.