BCB6主应用调用dll中的模块作为子窗口范例

直接上代码:

主应用动态调用DLL的代码

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   if( NULL == DLLInst ) DLLInst = LoadLibrary("Project1.dll"); //上面的 Dll
    if (DLLInst) { 
        Open = (void (__stdcall*)(TWinControl* parent,int GNID)) GetProcAddress(DLLInst,
                                                    "Open");
        if (Open)
        Open(Panel1,123);
        else
        ShowMessage("Could not obtain function pointer");
    }
    else ShowMessage("Could not load DLL.dll");      
}

dll中的头文件定义

Unit1.h

//---------------------------------------------------------------------------

#include 
#include 
#pragma hdrstop
#include 
//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
        return 1;
}

//---------------------------------------------------------------------------
//作者:PMS动态库生成工具
//日期:2014年05月12日
//内容:生成标准的PMS接入DLL的接口函数
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) __stdcall void Open (TWinControl* parent)
{//打开动态库,在本函数中传递主程序接口类,并显示窗体
    TfrmMain = new TTfrmMain(parent);
    SetParent(TfrmMain->Handle, parent->Handle);
    ShowWindow ( TfrmMain->Handle, SW_SHOWMAXIMIZED );
    TfrmMain->Align = alClient;
    TfrmMain->Show();
}
dll中的子窗口。h文件

//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include 
//---------------------------------------------------------------------------
class TTfrmMain : public TForm
{
__published:	// IDE-managed Components
        TCheckBox *CheckBox1;
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TTfrmMain(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TTfrmMain *TfrmMain;
extern "C" __declspec(dllexport) __stdcall void Open (TWinControl* parent) ;
//---------------------------------------------------------------------------
#endif

下面的图片另存为之后改成rar后缀,解压后就得到源码:)

BCB6主应用调用dll中的模块作为子窗口范例_第1张图片


你可能感兴趣的:(C++,Builder,c/c++)