EXE文件: Unit1.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("动态库释放成功");
}
}
EXE文件: Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//注意加入基类文件
#include "TTest.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
HINSTANCE hInst;
Test* MyTest; //请注意基类不能实例化,必须用new出来(在DLL中实现了)
void __fastcall LoadFreeDLL(bool blf=true);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
AnsiString sTemp;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Ø 小结:
以上三种方法全部在C++ Builder6.0编译通过!具体使用哪种方法请大家自己测试!
这是我第一次写文章,如有写得不对得地方请多多指教!我会及时更正错误!
如需要源代码的朋友可以发EM给我!
最后祝CSND越办越红火,大家猴年吉祥!!
我的EM:jxwbo@sohu.com
魏 波
写自深圳
2004年1月15日