C++ DLL

创建dll
1.win32 console application,dll,输入dll的名称,此例为dlltest
2.为dll写方法,例如 为dll增加一个相加的方法
  extern   "C" _declspec(dllexport)  int Add(int,int);  //注意了 
int _declspec(dllexport) Add(int a, int b) 

     return a+b; 

3.Build,生成dll
调用dll
1.创建一个win32 console application,名称为helloc,右键选择header files,添加已存在项目,浏览找到windows.h并添加到项目的头文件中
2.引入dll,将生成的dll复制到helloc.cpp所在的文件
3.引入window 
#include <windows.h>
typedef int (* MYPROC)(int,int);
4.使用dll
int main()
{
   int error; 
MYPROC pFunc; 
HMODULE hdll_lib =::LoadLibraryEx(TEXT("dlltest.dll"),NULL,0); 
//  
if(hdll_lib == NULL) 

error = GetLastError(); 
cout<<error<<endl; 
FreeLibrary(hdll_lib); return -1; 

pFunc=(MYPROC)GetProcAddress(hdll_lib, "Add"); //  
if(!pFunc) 

error = GetLastError(); 
cout<<error<<endl;
FreeLibrary(hdll_lib); return -1; 

int a=pFunc(1,2);// 
cout<<a<<endl;
FreeLibrary(hdll_lib); 
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值