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