n 方法3:(自己写的)
我想法是先声明个抽象基类,DLL继承这个基类将类给实例化(代码化),并在DLL中导出这个类,EXE只要这个基类,去调用DLL的导出类函数! virtual void sum(int) = 0; 声明一个纯虚函数!这个方法只要一个动态库(DLL)和一个基类文件就可以了!!达到了我预期的要求!
基类文件:Test.h
//基类
//将这个文件提供给EXE调用
class Test
{
private:
virtual int sum(int, int) = 0;
public:
virtual void ShowMess(char* ) = 0;
virtual void Count(int) = 0;
};
DLL文件: DllMain.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
//注意加上这个基类的文件
#include "TTest.h"
#pragma hdrstop
#pragma argsused
//---------------------------------------------------------------------------
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return 1;
}
//---------------------------------------------------------------------------
//派生出一个新类Me,来自基类Test
class Me : public Test
{
private:
virtual int sum(int, int);
int sum1(int, int, int); //只能自己调用(基类中没有的)
public:
virtual void ShowMess(char* );
virtual void Count(int);
void Count1(int); //只能自己调用(基类中没有的)
};
//---------------------------------------------------------------------------
int Me::sum(int a, int b)
{
return a+b;
}
//---------------------------------------------------------------------------
void Me::ShowMess(char* buf)
{
ShowMessage(buf);
}
//---------------------------------------------------------------------------
void Me::Count(int i)
{
int j = sum(i, i*2);
ShowMessage(j);
j = sum1(i, i*2, i*3);
ShowMessage(j);
}
//---------------------------------------------------------------------------
int Me::sum1(int a, int b, int c)
{
return a+b+c;
}
//---------------------------------------------------------------------------
void Me::Count1(int i)
{
int j = sum1(i, i*2, i*3);
ShowMessage(j);
}
//---------------------------------------------------------------------------
//导出类函数(得到EXE传来基类的指针地址,所以这里是指针的指针)
extern "C"
{
__declspec(dllexport) void __stdcall OutClass(Test** MeTest, bool New);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __stdcall OutClass(Test** MeTest, bool New)
{
//在这里实现地址的分配与释放
if (New == true)
{
if (*MeTest != NULL)
{
delete *MeTest;
*MeTest = NULL;
}
*MeTest = new Me();
ShowMessage("动态库分配成功");
}
else
{
delete *MeTest;
*MeTest = NULL;
ShowMessage("动态库释放成功");
}
}