DLL怎么获得Application信息? ( 积分: 100 )

  • 主题发起人 主题发起人 soFTangeL
  • 开始时间 开始时间
S

soFTangeL

Unregistered / Unconfirmed
GUEST, unregistred user!
好像DLL和MainForm都有一个Application信息?加载DLL时使用 Dll.Application.Handle:=MainForm.Application.Handle,然后在DLL里面不能正确获取MainForm的Application.Title。[:(]
主窗口:
procedure TfrmMain.miAboutClick(Sender: TObject);
var
AboutHandle: THandle;
ShowAboutForm: TShowModalForm;
begin
AboutHandle := LoadLibrary('./About.dll');
if AboutHandle = 0 then
Exit;
@ShowAboutForm := GetProcAddress(AboutHandle, 'ShowModalForm');
if not (@ShowAboutForm = nil) then
ShowAboutForm(Application.Handle);
FreeLibrary(AboutHandle);
end;

Dll部分:
procedure ShowModalForm(AHandle: THandle)
stdcall;
procedure ShowModalForm(AHandle: THandle);
begin
Application.Handle := AHandle;
with TfrmAbout.Create(Application) do
begin
try
ShowModal;
finally
Free;
end;
end;
end;

procedure TfrmAbout.FormShow(Sender: TObject);
begin
lblAppName.Caption := Application.Title
//此处不能获取主窗口所在Application的Title。
end;
 
好像DLL和MainForm都有一个Application信息?加载DLL时使用 Dll.Application.Handle:=MainForm.Application.Handle,然后在DLL里面不能正确获取MainForm的Application.Title。[:(]
主窗口:
procedure TfrmMain.miAboutClick(Sender: TObject);
var
AboutHandle: THandle;
ShowAboutForm: TShowModalForm;
begin
AboutHandle := LoadLibrary('./About.dll');
if AboutHandle = 0 then
Exit;
@ShowAboutForm := GetProcAddress(AboutHandle, 'ShowModalForm');
if not (@ShowAboutForm = nil) then
ShowAboutForm(Application.Handle);
FreeLibrary(AboutHandle);
end;

Dll部分:
procedure ShowModalForm(AHandle: THandle)
stdcall;
procedure ShowModalForm(AHandle: THandle);
begin
Application.Handle := AHandle;
with TfrmAbout.Create(Application) do
begin
try
ShowModal;
finally
Free;
end;
end;
end;

procedure TfrmAbout.FormShow(Sender: TObject);
begin
lblAppName.Caption := Application.Title
//此处不能获取主窗口所在Application的Title。
end;
 
好像在dll里不能正确获得!需要从外部传入!
 
直接传入 Application 对象啊:

procedure ShowModalForm(MainApp: TApplication)
stdcall;
procedure ShowModalForm(MainApp: TApplication);
var
cApp: TApplication;
begin
cApp := Application;
Application := MainApp;
with TDllForm.Create(Application) do
try
ShowModal;
finally
Free;
Application := cApp;
end;
end;

使用:
procedure TForm1.Button1Click(Sender: TObject);
type
TShowModalForm = procedure (MainApp: TApplication)
stdcall;
var
AboutHandle: THandle;
ShowAboutForm: TShowModalForm;
begin
AboutHandle := LoadLibrary('Dll.dll');
if AboutHandle = 0 then
Exit;
@ShowAboutForm := GetProcAddress(AboutHandle, 'ShowModalForm');
if not (@ShowAboutForm = nil) then
ShowAboutForm(Application);
FreeLibrary(AboutHandle);
end;
 
接受答案了.
 
后退
顶部