问一个关于DLL的问题
Javpp 2005-09-01 12:08:47 我想在WINMAIN函数里直接动态连接DLL,DLL里打包了所有的窗体,但运行退出时出现了读内存错误,请大家帮帮忙!
代码如下:
EXE文件代码,我是将自动生成的工程去掉那个窗体,然后修改WINMAIN函数:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//extern "C" __declspec (dllimport) HANDLE __stdcall LoadMainForm (TApplication *App);
HANDLE (__stdcall *LoadMainForm) (TApplication *App);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize ();
//Load dll
HINSTANCE HInst = LoadLibrary ("DTForm.dll");
if (HInst)
{
LoadMainForm = (HANDLE (__stdcall *) (TApplication *App)) GetProcAddress (HInst, "LoadMainForm");
if (LoadMainForm)
{
//Application->Handle = LoadMainForm (Application);
LoadMainForm (Application);
}
else
{
ShowMessage ("File Error!");
return 1;
}
}
else
{
ShowMessage ("Load dll Error!");
return 1;
}
Application->Run ();
FreeLibrary ("DTForm.dll");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
//---------------------------------------------------------------------------
DLL文件代码,只添加了一个窗口:
DLL文件:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#include <objbase.h>
#include "MainFrm.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 HANDLE LoadMainForm (TApplication *App);
TApplication *dllApp;
void DllUnload (int reason);
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
//将EXE的句柄给DLL
dllApp = Application;
//Application = dllApp;
DllProc = &DllUnload;
return 1;
}
//---------------------------------------------------------------------------
HANDLE __stdcall LoadMainForm (TApplication *App)
{
Application = App;
Application->Title = "Gree AC Remote Control";
Application->Initialize ();
CoInitialize (NULL);
Application->CreateForm (__classid (TfrmMain), &frmMain);
Application->Run ();
return frmMain->Handle;
}
//---------------------------------------------------------------------------
void DllUnload (int reason)
{
if (reason == DLL_PROCESS_DETACH)
{
CoUninitialize ();
//将句柄交还给Application
Application = dllApp;
}
}
//---------------------------------------------------------------------------
DLL中窗口的代码:
CPP文件:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainFrm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Exit1Click(TObject *Sender)
{
//这里不能使用Application->Terminate ();
//因为还没有将DLL的句柄交还给EXE,会出错
//不知道这样做对不对
Close ();
//Application->Terminate ();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormDestroy(TObject *Sender)
{
DllProc (DLL_PROCESS_DETACH);
}
//---------------------------------------------------------------------------
头文件代码:
//---------------------------------------------------------------------------
#ifndef MainFrmH
#define MainFrmH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Menus.hpp>
#include <ToolWin.hpp>
//---------------------------------------------------------------------------
class TfrmMain : public TForm
{
__published: // IDE-managed Components
TCoolBar *CoolBar1;
TToolBar *ToolBar1;
TToolBar *ToolBar2;
TToolButton *ToolButton1;
TToolButton *ToolButton2;
TToolButton *ToolButton3;
TMainMenu *MainMenu1;
TMenuItem *System1;
TMenuItem *Logout1;
TMenuItem *AddUser1;
TMenuItem *ConfigUser1;
TMenuItem *DeleteUser1;
TMenuItem *Setting1;
TMenuItem *N1;
TMenuItem *Logout2;
TMenuItem *Exit1;
TMenuItem *Edit1;
TMenuItem *Add1;
TMenuItem *Inner1;
TMenuItem *OutDoor1;
TMenuItem *Help1;
TMenuItem *Help2;
TMenuItem *About1;
TMenuItem *AboutGree1;
void __fastcall Exit1Click(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TfrmMain(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmMain *frmMain;
//---------------------------------------------------------------------------
#endif
请大家指点一下,代码中可能还有别的错误,请指点,谢谢!!!