VC++ 创建及调用Dll

一、_stdcall 

被这个关键字修饰的函数,其参数都是从右向左通过堆栈传递的(__fastcall 的前面部分由ecx,edx传), 函数调用在返回前要由被调用者清理堆栈。

这个关键字主要见于Microsoft Visual C、C++。GNU的C、C++是另外一种修饰方式:__attribute__((stdcall))

1.

MathFunsStd.cpp:

 int _stdcall add(int a, int b)
{
return a+b;
}

int _stdcall subtract(int a, int b)
{
return a-b;
}

int _stdcall multiple(int a, int b)
{
return a*b;
}

MathFunsStd.def:

LIBRARY MathFunsStd

EXPORTS
add
subtract
multiple

2.MathFuns.cpp

int add(int a, int b)
{
return a+b;
}

int subtract(int a, int b)
{
return a-b;
}

int multiple(int a, int b)
{
return a*b;
}

MathFuns.def

LIBRARY MathFuns

EXPORTS
add
subtract
multiple

3.UseHeaderAPI

MathFunsUseHeader.h

#ifdef MathFunsUseHeaderAPI 
#else
#define MathFunsUseHeaderAPI _declspec(dllimport)
#endif

MathFunsUseHeaderAPI int add(int a,int b);
MathFunsUseHeaderAPI int subtract(int a,int b);
MathFunsUseHeaderAPI int multiple(int a, int b);


#define MathFunsUseHeaderAPI _declspec(dllexport)
#include "MathFunsUseHeader.h"

MathFunsUseHeader.cpp

int add(int a, int b)
{
return a+b;
}

int subtract(int a, int b)
{
return a-b;
}

int multiple(int a, int b)
{
return a*b;
}

三、调用

 
/*加载dll函数调用方式为默认调用方式*/
HINSTANCE hInst = LoadLibrary(L"MathFuns.dll");
if(!hInst)
{
printf("加载MathFuns.dll失败!\n");
}
typedef int (*MathFunsAPI)(int a, int b);//定义函数指针变量类型
MathFunsAPI Add = (MathFunsAPI)::GetProcAddress(hInst,"add");
printf("5+3=%d\n",Add(5,3));
::FreeLibrary(hInst);

       //调用dll函数调用方式为_stdcall
HINSTANCE hInstStd = ::LoadLibrary(L"MathFunsStd.dll");
if(!hInstStd)
{
printf("加载MathFunsStd.dll失败!\n");
}
typedef int (_stdcall *MathFunsStdAPI)(int a, int b);//定义函数指针变量类型
MathFunsStdAPI AddStd = (MathFunsStdAPI)::GetProcAddress(hInstStd,"add");
printf("5+3=%d\n",AddStd(5,3));
::FreeLibrary(hInst);

return 0;

 

转载于:https://www.cnblogs.com/blogpro/p/11339412.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值