BCB 编写 DLL 终极手册

http://tech.ddvip.com   2006年03月31日    社区交流

本文详细介绍BCB 编写 DLL 终极手册

  二. 静态调用 DLL

  使用 $BCB path\Bin\implib.exe 生成 Lib 文件,加入到工程文件中

  将该文件拷贝到当前目录,使用 implib MyDll.lib MyDll.dll 生成

  // 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;
}

  三. 动态调用 DLL

  // Unit1.h
class TForm1 : public TForm
{
...
private: // User declarations
void (__stdcall *CreateFromFunct)();
...
}
// Unit1.cpp // TForm1
HINSTANCE DLLInst = NULL;
void __fastcall TForm1::Button2Click(TObject *Sender)
{
   if( NULL == DLLInst ) DLLInst = LoadLibrary("DLL.dll"); //上面的 Dll
   if (DLLInst) {
     CreateFromFunct = (void (__stdcall*)()) GetProcAddress(DLLInst,
                           "CreateFromFunct");
     if (CreateFromFunct) CreateFromFunct();
     else ShowMessage("Could not obtain function pointer");
   }
   else ShowMessage("Could not load DLL.dll");
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   if ( DLLInst ) FreeLibrary (DLLInst);
}

责编:豆豆技术应用

正在加载评论...