TeeChart and GDI+ inside a DLL

 

TeeChart VCL controls use GDI+ (Windows graphics library) by default in VCL applications.

 

TeeChart GDI+ editor dialog
TeeChart GDI+ editor dialog

 

NOTE: The fix indicated below has been already included in the latest VCL version: 2014.11.140512    Download link

There is a “problem” with GDI+ when using it inside a DLL. GDI+ needs to be initialized before using it, and the initialization should be done by the caller EXE process, not by the DLL itself.

(Microsoft information about this can be found here in this link)

This is why the Embarcadero RAD Studio RTL WinApi.GDIPOBJ.pas unit, at the bottom of the file initialization section, checks “if not IsLibrary” to initialize GDI+ only when the unit is used by an executable application.

The problem is, if you have a DLL that contains chart controls, and if GDI+ has not been correctly initialized, an Access Violation exception will be raised from code inside the Windows GDI+ gdiplus.dll !

The solution is quite easy. In your EXE application, before calling any DLL function, add this code to initialize GDI+ :

uses WinAPI.GDIPObj, WinAPI.GDIPApi ;

// Initialize StartupInput structure
StartupInput.DebugEventCallback := nil;
StartupInput.SuppressBackgroundThread := False;
StartupInput.SuppressExternalCodecs := False;
StartupInput.GdiplusVersion := 1;

GdiplusStartup(gdiplusToken, @StartupInput, nil);

 

It is also recommended (although it doesn’t seem mandatory), to uninitialize GDI+, as one of your EXE last steps:

GdiplusShutdown(gdiplusToken);

 

The next TeeChart release (very soon !) will include a small fix to improve this situation: if GDI+ is not initialized for any reason, it will fallback to “old” GDI graphics instead, avoiding the exception.

Click here to download a simple test project made in Delphi with RAD XE5 that includes an EXE and a DLL. The DLL contains a Form1 with a Chart1. The EXE main unit initializes GDI+ and calls the DLL to show the chart.

 

8 thoughts to “TeeChart and GDI+ inside a DLL”

  1. Thank you for this excellent article, and thank you for integrating the fix into the latest 2014.11 build. You should update the article with the fact that it has been fixed in 2014.11.140512 for VCL.

  2. I am getting this access violation with XE7 and the lite version that is standard with XE7 Professional. Was it fixed in TeeChart Lite? v2014.11.140711 32 bit.

    1. With XE7, I just played around with your example, and it looks like all that needs to be done is to place the following line in the EXE source:

      uses WinAPI.GDIPObj;

      The initialization section of that unit performs the actions that are in your demo.

Comments are closed.