怎样将窗体封装在dll中,然后在另一个程序中使用这个窗体(在线等..)
主要的问题是 我想在程序中这样使用:
TfrmRecommend * recommend=new TfrmRecommend(NULL);
该如何完成这个dll,又如何调用呢?
请高手说的详细点,最好能给出例子,在线等
[解决办法]
//---------------------------------------
#include <vcl.h>
#include <windows.h>
#include <comdef.h>
#include "MainForm.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) void __stdcall NewForm();
extern "C " __declspec(dllexport) void __stdcall NewObj(void ** FrmObj);
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
CoInitialize(NULL);
break;
case DLL_PROCESS_DETACH:
CoUninitialize();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}
//---------------------------------------
void __stdcall NewForm()
{
TMainFrm *fm = new TMainFrm(NULL);
fm-> ShowModal();
fm-> DoubleBuffered = true;
delete fm;
fm=NULL;
}
//---------------------------------------
void __stdcall NewObj(void ** FrmObj)
{
TMainFrm *fm = new TMainFrm(NULL);
*FrmObj = fm;
}
//---------------------------------------
[解决办法]
调用
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TestDllPro.h "
#include "MainForm.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
TMainFrm *MainFrm;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
typedef void __stdcall (*MyDLLFunc)();
HMODULE hModule = LoadLibrary((LPCTSTR) "OSAPro.dll ");
if (hModule)
{
MyDLLFunc t = (MyDLLFunc)GetProcAddress(hModule, "NewForm ");
t();
}
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
typedef void __stdcall (*MyDLLFunc)(void** FrmObj);
HMODULE hModule = LoadLibrary((LPCTSTR) "OSAPro.dll ");
if (hModule)
{
MyDLLFunc t = (MyDLLFunc)GetProcAddress(hModule, "NewObjFrm ");
t((void **)&MainFrm);
MainFrm-> ShowModal();
delete MainFrm;
}
}
//---------------------------------------