C + + calling conventions and name conventions

Source: Internet
Author: User
Tags modifiers

A calling convention is the method by which a program passes parameters and gets return values when a function is called: by register, or by a stack, or by a mixture of the two. The modifiers used to specify calling convention mainly include: __cdecl,__stdcall,__fastcall, and so on. The calling convention can be selected from the project setup: Setting...\c/c++ \advanced\callingconvention item, with the default state of __cdecl. They determine the following:

1), the stacking order of function parameters,

2), by the caller or by the caller, the parameters pop-up stack,

3), and the method that produces the function decorated name.

Their respective characteristics are as follows (take the VS2005 tool as an example):

1. _CDECL is the default calling method for C and C + + programs:

1.1. Parameters are passed from right to left and placed in the stack

1.2. The stack's emptying is done by the keynote function

1.3. In the generated assembly code, the function name begins with the underscore _

Compile options:/gd, for a variable parameter function, such as printf, can only be used this way

2, the standard calling convention __stdcall:

2.1, function parameters from right to left through the stack pass

2.2. The called function cleans the memory stack of the transfer parameters before returning

2.3. In the generated assembly code, the function name begins with an underscore _, ending with the number of bytes occupied by @ and all parameters. such as call [email protected]

Compile options:/gz, the WINAPI in theWin32 program is __stdcall: #define WINAPI __stdcall, since the stack is emptied by the function itself, the resulting execution code is less than __ The code generated by the Cdecl method.

3, __fastcall:

3.1, the first two parameters are not more than 32bits, respectively into ECX and edx, the remaining parameters are transferred from right to left, placed in the stack

3.2, parameters by the function popup stack

3.3. In the generated assembly code, the function name begins with @ and ends with the number of bytes occupied by @ and all parameters

Compilation options:/GR

In addition to these three kinds, there are thiscall, but it is only used for member functions of classes in C + +:

1. Parameters are passed from right to left and placed on the stack. This is placed in the ECX

2, the stack of empty has been adjusted function to complete

This is the default calling convention for class member functions in C + +. But if the member function of the class contains mutable arguments, then the calling convention of the function is __cdecl.

C and C + + correspond to different calling conventions, and the resulting modifiers vary, for the function test (void) is as follows:

calling convention

extern "C" or. c files

. cpp,. cxx, or/TP

__cdecl

_test

[Email protected] @ZAXXZ

__fastcall

@[email protected]

[Email protected] @YIXXZ

__stdcall

[Email protected]

[Email protected] @YGXXZ

C + + compile-time function name adornment Convention rules:

__stdcall calling convention:

1), with "?" Identifies the beginning of the function name followed by the name of the letter;

2), the function name after the "@ @YG" to identify the beginning of the parameter table, followed by the parameter table;

3), the parameter list is indicated by the code name:

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--represents the pointer, followed by the code indicates the pointer type, if the same type of pointer appears consecutively, with "0" instead of a "0" represents a repetition;

4), the first item of the parameter table is the return value type of the function, followed by the data type of the parameter, and the pointer is identified before the data type it refers to;

5), the parameter table after the "@z" to identify the end of the entire name, if the function has no parameters, the "Z" Mark to end.

The format is "[email protected] @YG *****@z" or "[email protected] @YG *xz", for example

IntTest1 (char *var1,unsigned long)----"[email protected]@[email protected]"

VoidTest2 ()-----"[Email protected] @YGXXZ"

__cdecl calling convention:

The rule is the same as the _stdcall calling convention above, except that the start identifier of the parameter table is changed from "@ @YG" to "@ @YA".

__fastcall calling convention:

The rule is the same as the _stdcall calling convention above, except that the start identifier of the parameter table is changed from "@ @YG" to "@ @YI".

VC + + On the function of the default declaration is "__CEDCL", will only be called by C + +.

Attached: Only one instance of a DLL in memory


The relationship between a DLL program and a program that calls its output function:

1), the relationship between DLL and process, thread

The DLL module is mapped to the virtual address space of the process that called it.

The memory used by the DLL is allocated from the virtual address space of the calling process and can only be accessed by the thread of the process.

The handle to the DLL can be used by the calling process, and the handle to the calling process can be used by the DLL.

Dlldll can have its own data segment, but not its own stack, using the same stack mode as the application calling it's stack, with the calling process.

2), about shared data segments

The global variables defined by the DLL can be accessed by the calling process, and the DLL can access the global data of the calling process. Each process that uses the same DLL has its own DLL global variable instance. If multiple threads access the same variable concurrently, you need to use the synchronization mechanism; For a DLL variable, you should use thread-local storage if you want each thread that uses a DLL to have its own value (Tls,thread localstrorage)

C + + calling conventions and name conventions

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.