n 方法2:
它是将类导出,但必须要有LIB文件,这种方法是静态调用!简单
DLL文件:MainDll.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
#include "MainDllTest.h"
//---------------------------------------------------------------------------
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return 1;
}
//---------------------------------------------------------------------------
//下面是类的代码
TStaticClass::TStaticClass()
{
FTimesCalled = 0;
m_AlreadySaidHi = false;
}
//---------------------------------------------------------------------------
TStaticClass::~TStaticClass()
{
}
//---------------------------------------------------------------------------
void __fastcall TStaticClass::SayHi(void)
{
ShowMessage("调用DLL中类成功!");
FTimesCalled++;
m_AlreadySaidHi = true;
ShowMessage("再试一次");
}
//下面是导出函数
//---------------------------------------------------------------------------
void __stdcall Say(char *WhatToSay)
{
ShowMessage("来自EXE调用DLL中的函数/n" + (String)WhatToSay);
}
//---------------------------------------------------------------------------
DLL文件:MainDll.h
//---------------------------------------------------------------------------
#ifndef MainDllTestH
#define MainDllTestH
//---------------------------------------------------------------------------
//写个导出类
__declspec(dllexport) class TStaticClass
{
private:
int FTimesCalled;
public:
TStaticClass();
~TStaticClass();
bool m_AlreadySaidHi;
void __fastcall SayHi(void);
__property int TimesCalled = {read = FTimesCalled};
};
//写个导出函数
extern "C"
{
__declspec(dllexport) void __stdcall Say(char *WhatToSay);
}
//---------------------------------------------------------------------------
#endif
EXE文件:CallingForm.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "CallingForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//新建个类,此类在DLL中定义
StaticClass = new TStaticClass;
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
//释放
delete StaticClass;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//调用DLL的函数
Say("静态加载函数成功");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//加载DLL,得到DLL文件句柄
HINSTANCE hInst = LoadLibrary("SimpleDll.dll");
//加载成功?
if (hInst)
{
//获得DLL函数的指针
(FARPROC &)Say = GetProcAddress(hInst,"Say");
//获得成功?
if (Say)
{
Say("动态加载函数成功");
}
else
{
ShowMessage(SysErrorMessage(GetLastError()));
}
//释放DLL句柄
FreeLibrary(hInst);
}
else
{
ShowMessage(SysErrorMessage(GetLastError()));
ShowMessage("加载DLL失败");
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if (!StaticClass->m_AlreadySaidHi)
{
StaticClass->SayHi();
}
else
{
ShowMessage("你已经调用DLL中类第 " +
(AnsiString)StaticClass->TimesCalled + " 次.");
StaticClass->SayHi();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
StaticClass->SayHi() ;
}
//---------------------------------------------------------------------------
EXE文件:CallingForm.h
//---------------------------------------------------------------------------
#ifndef CallingFormH
#define CallingFormH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//注意加上DLL导出文件
#include "MainDllTest.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
private: // User declarations
//函数指针,指向DLL的导出函数
void __stdcall (*Say)(char *WhatToSay);
//类指针,此类在DLL中定义
TStaticClass *StaticClass;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
__fastcall TForm1::~TForm1();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif