请教关于DLL问题 ( 积分: 50 )

  • 主题发起人 主题发起人 aaa123abc
  • 开始时间 开始时间
A

aaa123abc

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序里有很多个FROM
我想把每个FROM放在一个DLL文件里,可不知道怎么动态调用。
查找了以前大富翁论坛的贴子,只能调用DLL里的涵数,可我想在EXE文件里点一个按妞,动态加入DLL.然后显示出DLL里的FROM。
 
其实以前的资料应该很多了,我贴个 Demo 出来吧
//DLL 工程单元
library FormDLL;

uses
SysUtils,
Classes,
Forms,
DLLForm in 'DLLForm.pas' {frmDLL};

{$R *.res}

{返回一个窗体对象,这是DLL的主要功能}
function GetDllChildForm(Parent:TComponent):TfrmDLL;stdcall;
begin
Result := TfrmDLL.Create(Parent);
end;

exports
SynAPP,ShowForm;

begin
end.

//DLL 窗体单元
unit DLLForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TfrmDLL = class(TForm)
private
{ Private declarations }
public
{ Public declarations }

end;

var
frmDLL: TfrmDLL;

procedure SynAPP(App:THandle);stdcall;
procedure ShowForm;stdcall;

implementation

uses Math;

{$R *.dfm}

procedure SynAPP(App:THandle );stdcall;
begin
Application.Handle := App;
end;

procedure ShowForm;stdcall;
begin
try
frmDLL := TfrmDLL.Create (Application);
try
frmDLL.ShowModal;
finally
frmDLL.Free;
end;
except
on E: Exception do
MessageDlg ('Error in DLLForm: ' +
E.Message, mtError, [mbOK], 0);
end;
end;

end.

//调用 DLL 窗体程序部分代码
procedure SynAPP(App:THandle);stdcall;external 'FormDLL.dll';
procedure ShowForm;stdcall;external 'FormDLL.dll';

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
SynApp(Application.Handle );
ShowForm ;
end;
 
我是想在要使用的时候再调用DLL文件。
请问怎么动态调用。
 
要使用的时候,调用下面的函数:
procedure DynaCreateForm;
type TSynAPPProc=procedure (App:THandle);stdcall;
TShowFormProc=procedure();stdcall;
var
hLib:THandle;
procSynAPP:TSynAPPProc;
procShowForm:TShowFormProc;
begin
hLib:=LoadLibary('...DLL File Path...');
procSynAPP:=GetProcAddress(hLib,'SynAPP');
if @procSynAPP=nil then exit;

procShowForm:=GetProcAddress(hLib,'ShowForm');
if @procShowForm=nil then exit;

procSynAPP(Application.Handle);
procShowForm;
freeLibary(hLib);
end;
 
procedure TForm1.Button2Click(Sender: TObject);
type
TSynAPP = procedure (App:THandle);stdcall;
TShowForm = procedure () ;stdcall;
var
sSynAPP: TSynAPP;
sShowForm : TShowForm;
HInst: THandle;
FPointer: TFarProc;
begin
HInst := SafeLoadLibrary('FormDLL.dll');
if HInst > 0 then
try
FPointer := GetProcAddress(HInst,PChar('SynAPP'));
if FPointer = nil then Exit;
sSynAPP := TSynAPP(FPointer);
sSynAPP(Application.Handle);

FPointer := GetProcAddress(HInst,PChar('ShowForm'));
if FPointer = nil then Exit;
sShowForm := TShowForm(FPointer);
sShowForm;
finally
FreeLibrary(HInst);
end;
end;
 
多人接受答案了。
 
后退
顶部