一、生成.dll和.lib文件
(1)在VC2003Studio 创建Win32 控制台 应用程序设置 选DLL选项
(2)在工程的属性的配置属性-〉常规-〉配置类型 选 动态库(。dll);
头文件:
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
// Windows 头文件:
#include <windows.h>
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif
MYLIBAPI int g_nResult;
MYLIBAPI int Add(int nLeft, int nRight);
CPP文件
#include "stdafx.h"
//#define MYLIBAPI extern"C" __declspec(dllexport)
//#include "MyDll.h"
int g_nResult;
int Add(int nLeft, int nRight)
{
g_nResult = nLeft + nRight;
return g_nResult;
}
运行生成相应的.dll和.lib文件
二、调用自己的.dll中的函数
(1)在VC2003Studio 创建Win32 控制台;
(2)将.lib和.dll文件copy到本工程目录中
(3)在工程的属性的链接器-〉输入-〉附加依赖项 添加.lib
调用函数的CPP
#include "stdafx.h"
#include <windows.h>
extern "C" int Add(int nLeft, int nRight);
int _tmain(int argc, _TCHAR* argv[])
{
int nLeft;
int nRight;
int nResult;
nLeft = 10;
nRight = 25;
nResult = Add(nLeft, nRight);
return 0;
}