vs2010环境下生成dll文件
1,新建win32工程,选中DLL项目,如下图:
![技术分享](http://www.cppblog.com/images/cppblog_com/mycblog/vs2010%E7%8E%AF%E5%A2%83%E4%B8%8B%E7%94%9F%E6%88%90dll%E6%96%87%E4%BB%B61.jpg)
2,分别添加头文件和cpp文件
![技术分享](http://www.cppblog.com/images/cppblog_com/mycblog/vs2010%E7%8E%AF%E5%A2%83%E4%B8%8B%E7%94%9F%E6%88%90dll%E6%96%87%E4%BB%B62.jpg)
![技术分享](http://www.cppblog.com/images/cppblog_com/mycblog/vs2010%E7%8E%AF%E5%A2%83%E4%B8%8B%E7%94%9F%E6%88%90dll%E6%96%87%E4%BB%B61.jpg)
2,分别添加头文件和cpp文件
#ifndef LIB_H
#define LIB_H
extern "C" int _declspec(dllexport)add(int x,int y); // 声明为C编译、链接方式的外部函数
#endif
#define LIB_H
extern "C" int _declspec(dllexport)add(int x,int y); // 声明为C编译、链接方式的外部函数
#endif
#include "stdafx.h"
int add(int x,int y)
{
return x+y;
}
3,新建win32控制台工程,添加头文件后,然后在main函数中写下如下的代码:int add(int x,int y)
{
return x+y;
}
#include "stdafx.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
typedef int(*lpAddFun)(int,int);//宏定义函数指针类型
HINSTANCE hInst=NULL;
lpAddFun addFun; //函数指针
hInst=::LoadLibraryA("C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\Win32Study\\Debug\\test_1.dll");
if(hInst==NULL)
{
printf("Load mydll.DLL fail!\n");
return 0;
}
else
{
addFun=(lpAddFun)GetProcAddress(hInst,"add");
if (addFun!=NULL)
{
int result=addFun(2,3);
cout<<result<<endl;
getchar();
}
}
FreeLibrary(hInst);
return 0;
}
4,运行结果如下图:using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
typedef int(*lpAddFun)(int,int);//宏定义函数指针类型
HINSTANCE hInst=NULL;
lpAddFun addFun; //函数指针
hInst=::LoadLibraryA("C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\Win32Study\\Debug\\test_1.dll");
if(hInst==NULL)
{
printf("Load mydll.DLL fail!\n");
return 0;
}
else
{
addFun=(lpAddFun)GetProcAddress(hInst,"add");
if (addFun!=NULL)
{
int result=addFun(2,3);
cout<<result<<endl;
getchar();
}
}
FreeLibrary(hInst);
return 0;
}
![技术分享](http://www.cppblog.com/images/cppblog_com/mycblog/vs2010%E7%8E%AF%E5%A2%83%E4%B8%8B%E7%94%9F%E6%88%90dll%E6%96%87%E4%BB%B62.jpg)
posted on 2012-02-03 21:21 加文 阅读(3777) 评论(0) 编辑 收藏 引用 所属分类: Compile