【发布时间】:2018-04-19 22:55:57
【问题描述】:
我想加载一个 DLL(从一个 VCL 应用程序但这不应该很重要 [这是不正确的,因为 VCL 和 FMX 都包含一个消息循环])并显示一个 FireMonkey 模式在该 DLL 中创建的表单。显示表单工作正常,但我在清理之后遇到问题......
我只能找到 2011/2012 年关于该主题的文章,它们大多是指 XE2。遗憾的是,这些解决方案不再有效。 (或者我做错了什么。)
所有示例文件都在这里:https://github.com/gabr42/GpDelphiCode/tree/master/FMX%20from%20DLL
我的 DLL 只是导出 ShowMainForm。
library FMXDLL;
uses
System.SysUtils,
System.Classes,
FMXMain in 'FMXMain.pas' {FormMain};
{$R *.res}
exports
ShowMainForm;
begin
end.
ShowMainForm 初始化 GDI+,然后显示表单。之后,它会尝试清理但失败了。
uses
Winapi.GDIPAPI,
Winapi.GDIPOBJ;
procedure InitGDIP;
begin
// Initialize StartupInput structure
StartupInput.DebugEventCallback := nil;
StartupInput.SuppressBackgroundThread := False;
StartupInput.SuppressExternalCodecs := False;
StartupInput.GdiplusVersion := 1;
GdiplusStartup(gdiplusToken, @StartupInput, nil);
end;
procedure FreeGDIP;
begin
if Assigned(GenericSansSerifFontFamily) then
GenericSansSerifFontFamily.Free;
if Assigned(GenericSerifFontFamily) then
GenericSerifFontFamily.Free;
if Assigned(GenericMonospaceFontFamily) then
GenericMonospaceFontFamily.Free;
if Assigned(GenericTypographicStringFormatBuffer) then
GenericTypographicStringFormatBuffer.free;
if Assigned(GenericDefaultStringFormatBuffer) then
GenericDefaultStringFormatBuffer.Free;
GdiplusShutdown(gdiplusToken);
end;
procedure ShowMainForm; stdcall;
var
FormMain: TFormMain;
begin
InitGDIP;
Application.Title := 'DLL Form';
FormMain := TFormMain.Create(Application);
FormMain.ShowModal;
FormMain.Free;
Application.Terminate;
Application.ProcessMessages;
FreeGDIP;
end;
表单包含一个关闭表单的按钮。
procedure TFormMain.btnCloseClick(Sender: TObject);
begin
Close;
end;
宿主应用程序在创建主窗体时加载此 DLL
procedure TFormHost.FormCreate(Sender: TObject);
begin
FLibHandle := LoadLibrary('FMXDLL');
if FLibHandle = 0 then begin
ShowMessage('Cannot load FMXDLL.DLL');
Application.Terminate;
end
else begin
FShowMain := GetProcAddress(FLibHandle, 'ShowMainForm');
if not assigned(FShowMain) then begin
ShowMessage('Missing export: ShowMainForm');
Application.Terminate;
end;
end;
end;
它有一个显示 FireMonkey 表单的按钮。
procedure TFormHost.Button1Click(Sender: TObject);
begin
FShowMain();
end;
当表单被销毁时,DLL 被卸载。
procedure TFormHost.FormDestroy(Sender: TObject);
begin
if FLibHandle <> 0 then begin
FreeLibrary(FLibHandle);
FLibHandle := 0;
end;
end;
这是观察到的行为(Delphi 10.1 Berlin 在 Windows 10 Creators Edition 上运行):
- 我启动我的宿主程序。任务栏中会出现一个名为“DLL Host”的图标。 [好的]
- 当我单击按钮时,会出现 FireMonkey 表单。 [好的]。
- 这个新表单还有一个名为“DLL 表单”的任务栏按钮。 [好的]
- 当我单击 FireMonkey 表单上的关闭按钮时,它会关闭。 [好的]
- 但是,它的任务栏按钮仍然在屏幕上可见! [绝对不行!]
- 我可以多次单击并关闭 FireMonkey 表单。它将始终正确显示,但其任务栏按钮永远不会消失。
- 当我关闭我的 VCL 表单时,它会从任务栏中消失。 [好的]
- 但是,FireMonkey 表单仍然可见并且程序挂起。 [肯定不行!] 堆栈显示代码在
d3d11.dll内部的某处,如果可以信任的话。
我尝试了不同的方法来创建和销毁 FMX 表单,但似乎没有任何工作正常。
【问题讨论】:
标签: delphi dll firemonkey