BCB如何调用DLL中的窗体

1、DLL程序的写法(myFormDLL.dll)
先建立一个DLL程序,再在里面加入一个新窗体Form1,这个DLL程序为:
---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#include "Unit1.h"
#pragma hdrstop
---------------------------------------------------------------------------
   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
---------------------------------------------------------------------------
extern "C" __declspec(dllexport) __stdcall void OpenmyForm(HWND handle);
extern "C" __declspec(dllexport) __stdcall void ClosemyForm(void);
USEFORM("Unit1.cpp",Form1);
int formID=0;
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
        return 1;
}
---------------------------------------------------------------------------
__declspec(dllexport) __stdcall void OpenmyForm(HWND handle)
{

  if(formID==0)
  {
    Application->Handle=handle;
    Application->CreateForm(__classid(TForm1),&Form1);
    formID=1;
  }
  Form1->Show();
}
__declspec(dllexport) __stdcall void ClosemyForm(void)
{
  if(formID==1)
  {

    Form1->Close();
    formID=0;
  }
}
2、动态调用这个DLL的方法,Unit1.cpp
---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormALL *FormALL;
void __stdcall (*OpenmyForm)(HWND);
void __stdcall (*ClosemyForm)(void);
HINSTANCE HmyFormDLL;
---------------------------------------------------------------------------
__fastcall TFormALL::TFormALL(TComponent* Owner)
        : TForm(Owner)
{
  HmyFormDLL=LoadLibrary("myFormDLL.dll");
}
---------------------------------------------------------------------------

void __fastcall TFormALL::Button1Click(TObject *Sender)
{
  FARPROC P;
  if(HmyFormDLL!=NULL)
  {
    P=GetProcAddress(HmyFormDLL,"OpenmyForm");
    if(P==NULL)
    {
      ShowMessage("OpenmyForm函数打不开!");
    }
    else
    {
      OpenmyForm=(void __stdcall (__cdecl *)(HWND))P;
      OpenmyForm(Application->Handle);
    }
  }
  else
  {
    ShowMessage("动态链接库myFormDLL.dll打不开!");
  }
}
---------------------------------------------------------------------------
void __fastcall TFormALL::ClosemyDLLFormClick(TObject *Sender)
{
  FARPROC P;
  if(HmyFormDLL!=NULL)
  {
    P=GetProcAddress(HmyFormDLL,"ClosemyForm");
    if(P==NULL)
    {
      ShowMessage("ClosemyForm函数打不开!");
    }
    else
    {
      ClosemyForm=(void __stdcall (__cdecl *)(void))P;
      ClosemyForm();
    }
  }
  else
  {
    ShowMessage("动态链接库myFormDLL.dll打不开!");
  }
}
---------------------------------------------------------------------------
void __fastcall TFormALL::FormDestroy(TObject *Sender)
{
  FreeLibrary(HmyFormDLL);
}
---------------------------------------------------------------------------

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值