BCB编写DLL终极手册

一. 编写 DLL
  File/New/Dll 生成 Dll 的向导,然后可以添加导出函数和导出类
  导出函数:extern "C" __declspec(dllexport) ExportType FunctionName(Parameter)
  导出类:class __declspec(dllexport) ExportType ClassName{...}
  例子:(说明:只是生成了一个 DLL.dll )

#include "DllForm.h"  // TDllFrm 定义

USERES("Dll.res");
USEFORM("DllForm.cpp", DllFrm);

class __declspec(dllexport) __stdcall MyDllClass { //导出类
    public:
       MyDllClass();
       void CreateAForm();
       TDllFrm* DllMyForm;
};

TDllFrm* DllMyForm2;
extern "C" __declspec(dllexport) __stdcall void CreateFromFunct();//导出函数

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


二. 静态调用 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);
}
  
四. DLL 作为 MDIChild (子窗体) 【只编写动态调用的例子】
    实际上,调用子窗体的 DLL 时,系统只是检查应用程序的 MainForm 是否为 fsMDIForm 的窗体,这样

  只要把调用程序的 Application 的 Handle 传递给 DLL 的 Application 即可;同时退出 DLL 时也要恢

  复

Application

// MDIChildPro.cpp // Dll 实现 CPP
#include "unit1.h" // TForm1 定义
TApplication *SaveApp = NULL;
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
    if ( (reason==DLL_PROCESS_DETACH) && SaveApp )
        Application = SaveApp ; // 恢复 Application
    return 1;
}

extern "C" __declspec(dllexport) __stdcall void TestMDIChild(    //1024X768
    TApplication* mainApp,LPSTR lpCaption)
{
    if ( NULL == SaveApp ) // 保存 Application,传递 Application
    {
        SaveApp = Application;
        Application = mainApp;
    }
    // lpCaption 为子窗体的 Caption
    TForm1 *Form1 = new TForm1 ( Application, lpCaption );
    Form1->Show();
}
注:上面的程序使用 BCB 3.0 编译成功

五. BCB 调用 VC 编写的 DLL
  1. 名字分解:
    没有名字分解的函数
        TestFunction1 // __cdecl calling convention
        @TestFunction2 // __fastcall calling convention
        TESTFUNCTION3 // __pascal calling convention
        TestFunction4 // __stdcall calling convention
    有名字分解的函数
        @TestFunction1$QV // __cdecl calling convention
        @TestFunction2$qv // __fastcall calling convention

你可能感兴趣的:(BCB编写DLL终极手册)