delphidlldatamoduletimagelistdelphi-10.4-sydney

How to access an image inside a DLL from inside the DLL?


In Delphi 10.4, in a COM Server DLL ShellExtension project, I have added a DataModule to the project and placed a TImageList on the DataModule. The ImageList contains several images added at design-time. Now, from the main unit of the DLL project, I try to access an image in the ImageList:

procedure TMyShellExtension2.MyGetBitmap(ABitmap: Vcl.Graphics.TBitmap);
begin
  ABitmap.PixelFormat := pf24bit;
  ABitmap.Width := 128;
  ABitmap.Height := 128;
  DataModule.DataModule3.ilUrlPics.GetBitmap(0, ABitmap);
  CodeSite.Send('test');    
end;

The GetBitmap line seems to cause an error, as the following CodeSite.Send line is not being executed and the image is not assigned.

Obviously, my way of accessing the image from a TImageList on a DataModule inside a DLL is not correct.

On the other hand, when assigning a "self-made" BitMap, it does work:

ABitmap.PixelFormat := pf24bit;
ABitmap.Width := 128;
ABitmap.Height := 128;
ABitmap.Canvas.Brush.Color := clBlue;
ABitmap.Canvas.FillRect(Rect(0, 0, 127, 127));

Is there another working way of accessing an image inside a DLL? What am I doing wrong?


Solution

  • As several commentators in this question have pointed out and as I have experienced myself, using resources as image-source in DLLs is a better methodology than "using a TImageList on a TDataModule" which would have to be explicitly instantiated in the DLL's entry point (as @RemyLebeau has pointed out).

    So in this specific case I:

    • Store the needed images as Bitmaps in Project → Resources and Images.

    • Make sure to load the created resource e.g. with {$R MyShellExtensionDLL.res}.

    • Load the Bitmaps at run-time with e.g.:

    procedure MyGetBitmap(ABitmap: Vcl.Graphics.TBitmap);  
    begin  
      ABitmap.PixelFormat := pf24bit;  
      ABitmap.Width := 128;  
      ABitmap.Height := 128;  
      ABitmap.LoadFromResourceName(HInstance,'BITMAP1');  
    end;