FireMonkey Form in a dll, loaded from a VCL Applic

2019-02-26 20:11发布

I am attempting to load an FMX based dll, which includes an FMX form, from a VCL application which includes a VCL form.

Taken from Release Notes - http://docwiki.embarcadero.com/RADStudio/en/Release_Notes_for_XE2_Update_2:

To create a DLL that uses GDI+, you need to start up GDI+ from your host application instead, as follows:

For Delphi, add Winapi.GDIPOBJ to the interface section uses clause of your main form unit.

Sounds all well and good, but in practice I am now getting an access violation on exiting my VCL application. I believe this is all linked to Initializing and Freeing the GDI+ but cannot find any more information regarding this.

I have read through both these posts:

https://forums.embarcadero.com/thread.jspa?threadID=63425

Delphi XE2: Possible to instantiate a FireMonkey Form in VCL application?

Anyone else come across this? Eventually I am hoping to get our C application loading the FMX dll (and GDI+), but part of this process involves getting VCL and a FireMonkey dll to work.

1条回答
我只想做你的唯一
2楼-- · 2019-02-26 20:51

This answer applies to Delphi XE2 Update 2, I don't know if things have changed with Update 3.

I started the Embarcadero thread. Starting up GDI+ from the host application isn't an option for me. So far everything is working from the DLL side without errors but nothing has been released to the outside world yet either.

If you are able to modify your C host application, you may find the MSDN GdiplusStartup() documentation useful.

====

Using FireMonkey In a DLL Without Starting GDI+ In Host Application

Add this unit to your project:

unit InitFmxHack;

interface

procedure InitGDIP; // Call before using FMX.
procedure FreeGDIP; // Call after using FMX.

// NOTE:
// InitGDIP() must be called before instantiating a FireMonkey form.
// FreeGDIP() must be called after all FireMonkey forms are destroyed.
//
// InitGDIP/FreeGDIP can not be called from the initalization or finalization sections,
// or any method called from these sections.
//


implementation

uses
  System.SysUtils,
  Winapi.GDIPAPI,
  Winapi.GDIPOBJ,
  FMX.Types;

var
  NeedToShutdownGDIPlus: Boolean;
  GDIPlusInput: TGDIPlusStartupInput;
  gdiplusToken: Cardinal;
  TempRgn: GpRegion;

type
  TBitmapAccess = class(TBitmap);

procedure InitGDIP;
begin
  NeedToShutdownGDIPlus := False;
  case GdipCreateRegion(TempRgn) of
    Ok: GdipDeleteRegion(TempRgn);
    GdiplusNotInitialized:
    begin
      GDIPlusInput.GdiplusVersion := 1;
      GDIPlusInput.DebugEventCallback := nil;
      GDIPlusInput.SuppressBackgroundThread := False;
      GDIPlusInput.SuppressExternalCodecs := False;
      GdiplusStartup(GDIPlusToken, @GDIPlusInput, nil);
      NeedToShutdownGDIPlus := True;
    end;
  end;

end;

procedure FreeGDIP;
begin
  // HACK: Need to forcibly release a GDI+ object held in a global variable.
  FreeAndNil(TBitmapAccess(GetMeasureBitmap).FCanvas);

  // These lines have been copied from Winapi.GDIPOBJ. I'm not 100% sure
  // if there needed but it's probably safer to include them as they are part of
  // the standard FireMonkey shutdown sequence. Similar code is also found in
  // the VGScene library.
  if Assigned(GenericSansSerifFontFamily)           then FreeAndNil(GenericSansSerifFontFamily);
  if Assigned(GenericSerifFontFamily)               then FreeAndNil(GenericSerifFontFamily);
  if Assigned(GenericMonospaceFontFamily)           then FreeAndNil(GenericMonospaceFontFamily);
  if Assigned(GenericTypographicStringFormatBuffer) then FreeAndNil(GenericTypographicStringFormatBuffer);
  if Assigned(GenericDefaultStringFormatBuffer)     then FreeAndNil(GenericDefaultStringFormatBuffer);

  // Finalise GDI+ here if needed...
  if NeedToShutdownGDIPlus then GdiplusShutdown(GDIPlusToken);
end;

end.

To use:

InitFmxHack.InitGDIP;

// 1. Create form here.
// 2. Use form.
// 3. Destroy form. 

InitFmxHack.FreeGDIP;
查看更多
登录 后发表回答