请问WINDOWS平台中函数调用的两种方式:__stdcall和__cdecl有什么区别?

jeemy 2001-03-11 09:43:00
谢了!
...全文
222 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
招RD和QA 2001-03-11
  • 打赏
  • 举报
回复
不好意思,说错了。
最初应该是5种。
还有__fortran 和 __syscall

但是
The __pascal, __fortran, and __syscall calling conventions are no longer supported. You can emulate their functionality by using one of the supported calling conventions and appropriate linker options.
In355Hz 2001-03-11
  • 打赏
  • 举报
回复
__cdecl调用快,但是在每次调用函数都要链上清理栈的代码,所以调用次数越多,生成的代码量越大。
__stdcall较慢,但是清理栈的代码只需在函数体中链入一次,所以生成的代码量小。
招RD和QA 2001-03-11
  • 打赏
  • 举报
回复
其实应该是3种吧,__stdcall, __cdecl, __pascal。
有些书上说__stdcall 就是 __pascal,这是错误的说法。
ddddh 2001-03-11
  • 打赏
  • 举报
回复
就是参数压栈的顺序不同而已:)
Edward 2001-03-11
  • 打赏
  • 举报
回复
__stdcall
Home | Overview | How Do I

Microsoft Specific —>

The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl. Functions that use this calling convention require a function prototype. The following list shows the implementation of this calling convention.

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 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 /Gz compiler option specifies __stdcall for all functions not explicitly declared with a different calling convention.

Functions declared using the __stdcall modifier return values the same way as functions declared using __cdecl.

END Microsoft Specific

Example
In the following example, use of __stdcall results in all WINAPI function types being handled as a standard call:

// Example of the __stdcall keyword
#define WINAPI __stdcall


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.







__cdecl
Home | Overview | How Do I

Microsoft Specific —>

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 following list shows the implementation of this calling convention.

Element Implementation
Argument-passing order Right to left
Stack-maintenance responsibility Calling function pops the arguments from the stack
Name-decoration convention Underscore character (_) is prefixed to names
Case-translation convention No case translation performed


Note For related information, see Decorated Names.

Place the __cdecl modifier before a variable or a function name. Because the C naming and calling conventions are the default, the only time you need to use __cdecl is when you have specified the /Gz (stdcall) or /Gr (fastcall) compiler option. The /Gd compiler option forces the __cdecl calling convention.

Example
In the following example, the compiler is instructed to use C naming and calling conventions for the system function:

// Example of the __cdecl keyword
_CRTIMP int __cdecl system(const char *);

END Microsoft Specific


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
joke100 2001-03-11
  • 打赏
  • 举报
回复
C/C++默认的是__cdecl方式,即调用者负责清理.
但是调用Win32 API functions时使用__stdcall方式,由被调用者(API)自行清理.
joke100 2001-03-11
  • 打赏
  • 举报
回复
应该知道函数调用的过程需要首先将参数入栈.
在函数调用结束有两种方法清理栈数据.
其一是__cdecl标准,它指定由调用者清理.
另一种是__stdcall,它指定被调用者自己负责清理.

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧