__stdcall,__cdecl,__fastcall,__thiscall

本文详细介绍了C++中各种调用约定的实现细节,包括__stdcall、__cdecl、__fastcall和__thiscall等,涵盖了参数传递顺序、堆栈维护责任、名称修饰约定等内容。

从msdn上摘录,__*call的解释

__stdcall

return-type __stdcall function-name[(argument-list)]

Element

Implementation

Argument-passing order

Right to left.

Argument-passing convention

By value, unless a pointer or reference type is passed.

Stack-maintenance responsibility

Called function pops its own arguments from the stack.

Name-decoration convention in C

An underscore (_) is prefixed to the name. The name is

followed by the at sign (@) followed by the number of bytes

 (in decimal) in the argument list. Therefore, the function

declared as int func( int a, double b ) is decorated as follows:

_func@12

Case-translation convention

None

The __stdcall calling convention is used to call Win32 API functions.

The /Gz compiler option specifies __stdcall for all functions not explicitly declared with a different calling convention.

__cdecl

Element

Implementation

Argument-passing order

Right to left

Stack-maintenance responsibility

Calling function pops the arguments from the stack

Name-decoration convention in C

Underscore character (_) is prefixed to names, except when

exporting __cdecl functions that use C linkage.

Case-translation convention

No case translation performed

This is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code.

The /Gd compiler option forces the __cdecl calling convention.

vararg member functions use the __cdecl calling convention. All function arguments are pushed on the stack, with the this pointer placed on the stack last

__fastcall

Element

Implementation

Argument-passing order

The first two DWORD or smaller arguments are passed in

ECX and EDX registers; all other arguments are passed right

to left.

Stack-maintenance responsibility

Called function pops the arguments from the stack.

Name-decoration convention in C

At sign (@) is prefixed to names; an at sign followed by the

number of bytes (in decimal) in the parameter list is suffixed

 to names.

Case-translation convention

No case translation performed.

The __fastcall calling convention specifies that arguments to functions are to be passed in registers, when possible.

Using the /Gr compiler option causes each function in the module to compile as fastcall.

__thiscall

Element

Implementation

Argument-passing order

Arguments are pushed on the stack from right to left, with

the this pointer being passed via register ECX, and not on the

stack, on the x86 architecture.

Stack-maintenance responsibility

the callee cleans the stack

Name-decoration convention

Case-translation convention

The __thiscall calling convention is used on member functions and is the default calling convention used by C++ member functions that do not use variable arguments.

One reason to use __thiscall is in classes whose member functions use __clrcall by default. In that case, you can use __thiscall to make individual member functions callable from native code.

__clrcall

Specifies that a function can only be called from managed code.

Format of a C++ Decorated Name

A decorated name for a C++ function contains the following information:

l  The function name.

l  The class that the function is a member of, if it is a member function. This may include the class that encloses the function's class, and so on.

l  The namespace the function belongs to (if it is part of a namespace).

l  The types of the function's parameters.

l  The calling convention.

l  The return type of the function.

The function and class names are encoded in the decorated name. The rest of the decorated name is a code that has internal meaning only for the compiler and the linker. The following are examples of undecorated and decorated C++ names.

Undecorated name

Decorated name

int a(char){int i=3;return i;};

?a@@YAHD@Z

void __stdcall b::c(float){};

?c@b@@AAGXM@Z

C:/>undname ?func1@a@@AAEXH@Z

Microsoft (R) C++ Name Undecorator

Copyright (C) Microsoft Corporation 1981-2000. All rights reserved.Undecoration

of :- "?func1@a@@AAEXH@Z"

is :- "private: void __thiscall a::func1(int)"

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值