EXE文件: FMakeDll.cpp
//---------------------------------------------------------------------------
//使用静态调用,别忘了Project->Add to project...添加MakeDll.lib
#include <vcl.h>
#pragma hdrstop
#include "FTestDll.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
hMyDll = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TestA(10);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(hMyDll != NULL)
{
ShowMessage("已经为对象分配资源!");
return;
}
hMyDll = InitMakeDll();
if(hMyDll != NULL)
{
ShowMessage("获得对象句柄成功!");
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
int irtn;
irtn = TestB(hMyDll,20);
if(errOk == irtn)
{
ShowMessage("使用句柄执行测试函数B成功!");
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
int irtn;
//这种情况下需要使用者在程序中自己控制
if(hMyDll == NULL)
{
ShowMessage("对象还没有被创建或已经释放");
return;
}
irtn = ReleaseMakeDll(hMyDll);
if(errOk == irtn)
{
ShowMessage("使用句柄释放对象成功!");
}
hMyDll = NULL;
}
//---------------------------------------------------------------------------
EXE文件: FTestDll.h
//---------------------------------------------------------------------------
#ifndef FTestDllH
#define FTestDllH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
//DLL的导出函数文件(上面已经介绍过)
#include "MakeDll.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
private: // User declarations
//因为在整个窗体中需要使用hTestDll,定义在这里好了
//从这里的声明根本不会看到有关原来的类的影子
HANDLE hMyDll;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//--------------------------------------------------------------------------
#endif