首先需要强调,当使用某个类时一般目的有二:实例化成对象或者继承它产生新类。
对于前者,我们可以构造一个抽象类(java里的接口)来连接调用方和DLL。
抽象类:
// Interface.h 公共文件/
#pragma once
class Interface
{
public:
virtual void ShowMsg() = 0; // 将调用方需要调用的成员函数声明成纯虚函数
virtual ~Interface(){};// 抽象类的虚析构函数
};
// Interface.cpp 被调用方文件
// 注意下面的代码并不是实现 Interface 类,而是因为联系紧密才写在这。
#include "stdafx.h"
#include "Test.h"
#include "Interface.h"
// 通过导出函数形式向调用方提供指向派生类对象的基类指针
extern "C" __declspec(dllexport) Interface* Export(void)
{
return (Interface*)new Test();
}
将真正要调用的类声明成抽象类 Interface 的派生类:
// Test.h 被调用方文件//
// 类的声明
#pragma once
#include "Interface.h"
class Test:public Interface
{
public:
Test()
virtual ~Test();
virtual void ShowMsg(void);
private:
CString s;
};
// Test.cpp 被调用方文件
// 类的实现
#include"stdafx.h"
#include"Test.h"
Test::Test()
{
s = "hello form dll";
}
Test::~Test()
{
AfxMessageBox(_T("destructor"));
}
void Test::ShowMsg()
{
AfxMessageBox(s);
}
在调用方调用DLL时动态加载:
// 调用方文件 /
#include "stdafx.h"
#include "Interface.h" // 包含抽象类从而使用接口
// 在调用处添加如下代码
HINSTANCE hDll;
hDll = LoadLibrary(_T("module1.dll"));// 加载DLL库文件,DLL名称和路径用自己的
if(hDll == NULL)
{
TRACE("/n/nload dll fail/n/n");
return;
}
typedef Interface*(*pExport)(void); // 定义指向导出函数的指针类型
pExport Get;
Get = (pExport)GetProcAddress(hDll,"Export");// 将指针指向函数首地址
if(Get == NULL)
{
TRACE("/n/nload address fail/n/n");
return;
}
Interface *t = Get();// 调用导出函数获得抽象类指针
t->ShowMsg();// 通过该指针调用类成员函数
delete t; // 释放DLL中生成的对象
FreeLibrary(hDll); //释放库句柄
出处:http://blog.csdn.net/maxttyl/article/details/1537023