I. Writing a DLL
File/new/dll The wizard that generates the DLL, and then you can add export functions and export classes
Export function: extern "C" __declspec (dllexport) exporttype functionname (Parameter)
Export class: Class __declspec (dllexport) exporttype classname{...}
Example: (Note: Only a DLL.dll was generated)
#include "DllForm.h"//TDLLFRM definition
useres ("Dll.res");
Useform ("DllForm.cpp", dllfrm);
Class __declspec (dllexport) __stdcall Mydllclass {//Export class
Public:
Mydllclass ();
void Createaform ();
tdllfrm* Dllmyform;
};
tdllfrm* DllMyForm2;
extern "C" __declspec (dllexport) __stdcall void createfromfunct ();//Export function
//--------------------------------- ------------------------------------------
int WINAPI DllEntryPoint (hinstance hinst, unsigned long reason, void*)
{
return 1;
}
//---------------------------------------------------------------------------
Mydllclass::mydllclass ()
{
}
void Mydllclass::createaform ()
{
Dllmyform = new Tdllfrm (application);
Dllmyform->show ();
}
//---------------------------------------------------------------------------
void __stdcall Createfromfunct ()
{
DllMyForm2 = new Tdllfrm (application);
Dllmyform2->show ();
}
Two. Static Call DLL
Use $BCB path\bin\implib.exe to generate Lib files and add them to the project file
Copy the file to the current directory, using the Implib MyDll.lib MyDll.dll build
// Unit1.h // TForm1 定义
#include "DllForm.h" // TDllFrm 定义
//---------------------------------------------------------------------------
__declspec(dllimport) class __stdcall MyDllClass {
public:
MyDllClass();
void CreateAForm();
TDllFrm* DllMyForm;
};
extern "C" __declspec(dllimport) __stdcall void CreateFromFunct();
class TForm1 : public TForm{...}
// Unit1.cpp // TForm1 实现
void __fastcall TForm1::Button1Click(TObject *Sender)
{ // 导出类实现,导出类只能使用静态方式调用
DllClass = new MyDllClass();
DllClass->CreateAForm();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{ // 导出函数实现
CreateFromFunct();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete DllClass;
}