参照了”梦在天涯“的Blog做了下面这个Test例子:
-------------------------------------------------------------------------------------
用Win32 Dynamic-Link Library
创建
Non-MFC DLL
动态链接库
测试用例->
方法一:
Test_NonMFCDLL.h:
extern "C" _declspec(dllexport) double Add(double a, double b);
extern "C" _declspec(dllexport) double Mult(double a, double b);
Test_NonMFCDLL.cpp:
#include"Test_NonMFCDLL.h"
double Add(double a, double b)
{
return (a+b);
}
double Mult(double a, double b)
{
return (a*b);
}
编译成功后生成Test_NonMFCDLL.dll,Test_NonMFCDLL.lib
方法二:
删除上个例子工程中的Test_NonMFCDLL.h文件,新建文本文件Test_NonMFCDLL.def
LIBRARY Test_NonMFCDLL
EXPORTS
Add @1 //中间要有个空格
Mult @2
现在就测试一下你所写的dll,先将Test_NonMFCDLL.dll,Test_NonMFCDLL.lib拷贝到当前工程所在的目录下面
1.隐式链接
UseDLL.h:
#pragma comment(lib,"Test_NonMFCDLL.lib")
extern "C"_declspec(dllimport) double Add(double a,double b);
extern "C"_declspec(dllimport) double Mult(double a,double b);
UseDLL.cpp:
#include"TestUseDLL.h"
#include<stdio.h>
void main()
{
double res=Add(1.0,2.0);
printf("%f\n",res);
}
2 .显式链接
UseDLL.cpp:
#include<stdio.h>
#include<windows.h>
void main(void)
{
typedef double (*p) (double a,double b);
p Add;
HINSTANCE hDLL;
hDLL=LoadLibrary("Test_NonMFCDLL.dll");// 加载动态链接库dll 文件;
Add=(p)GetProcAddress(hDLL,"Add");
/*或使用GetProcAddress(hDLL,MAKEINTRESOURCE(1)) ,看上面的Add @1*/
double res=Add(1.0,2.0);
printf("%f\n",res);
FreeLibrary(hDLL);// 卸载dll 文件;
}