DLL入门到深入(2)

来源:互联网 发布:mac怎么下载youtube 编辑:程序博客网 时间:2024/05/16 04:05

由(1)http://blog.csdn.net/xiadasong007/archive/2009/10/13/4665932.aspx,库还可以动态载入,只修改Test.cpp的内容,代码如下:

 

#include <Windows.h>

#include <iostream>
using namespace std;

 

typedef int (*AddFunc)(int,int);

 

int main()
{
 AddFunc Add;
 HINSTANCE hInst=LoadLibrary(L"dll_01.dll");

 if(!hInst)
  FreeLibrary(hInst);

 

 Add=AddFunc(GetProcAddress(hInst,"add"));//这个函数有点像前面讲过的python库的载入哈~

 

 if(!Add)
  FreeLibrary(hInst);

 

 cout<<Add(1,2)<<endl;

 

 FreeLibrary(hInst); //一定要记住Free!

 return 0;
}

原创粉丝点击