dll中函数定义的关键词

1、extern "C" 是用来告诉编译器,以C的方式来命名,而不要以C++ 的命名法,因为C++ 的命名法会在函式名称後加上参数型态等装饰字,如此会造成其他程式如VC++,VB等无法使用的困扰。

2、__declspec关键字,它可以传入dllimport及dllexport两个参数,用来分别代表输入函数及输出函数。输出函数,必须使用 __declspec(dllexport) 来定义该函数,反之若你要使用输入函数,则你必须使用 __declspec(dllimport) 来定义该函数。

3、Calling Convention

The calling convention options determine the order in which arguments passed to functions are pushed onto the stack; which function, calling or called, removes the arguments from the stack; and the name-decorating convention that the compiler uses to identify individual functions。

/Gd__cdeclSpecifies the C calling convention for all functions that are not C++ member functions or are not marked as __stdcall or __fastcall. This is the default setting.
/Gr__fastcallSpecifies the __fastcall calling convention for all functions that are not C++ member functions or are not marked as __cdecl or __stdcall. All __fastcall functions must have prototypes.
/Gz__stdcallSpecifies the __stdcall calling convention for all prototyped C functions that do not take a variable number of arguments and are not marked as __cdecl or __fastcall. All __stdcall functions must have prototypes.

_stdcall将参数压栈是按C语言的顺序(从右到左),但与C 语言不同的是它是由被调用者将参数从栈中清除的,所以它的编译文件比_cdecl小。_stdcall是Windows API函数中默认的调用约定,VB、VFP等也采用这个约定。
    _cdecl是C语言采用的默认调用方法,它的优点是支持printf这样的可变参数调用。
    另外,VC++对于两种调用方法的名称转换方法也不同

例子:

声明函数

 extern "C" __declspec(dllexport) 函数类型 __cdecl 函数名(函数参数);

实现函数:

  __declspec(dllexport) 函数类型 __cdecl 函数名(函数参数)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值