Mirror

How to create and use a resource-only DLL (Views: 438)


Problem/Question/Abstract:

How do I create a DLL with just graphics files, then allow several different DLL's and applications to use these files?

Answer:

First, create a resource source file (*.RC) containing references to the bitmaps:

CLOSEBUTTON BITMAP "C:\Projects\closebtn.bmp"
OPENBUTTON BITMAP "C:\Projects\openbtn.bmp"

This is just an ordinary text file, so you can use the Delphi code editor or any other text editor to create it. Make sure the names of the bitmaps (CLOSEBUTTON, etc.) are ALL UPPERCASE.

Next, compile this .RC file to create the corresponding .RES file, using BRCC32.EXE:

brcc32 myimages.rc

Now start a new DLL project in Delphi and link the .RES file into it with an $R directive:

library MyImages;

uses
  Windows;

{$R MYIMAGES.RES}

begin
end.

Compile this code, and you now have a resource DLL containing the bitmaps.

To use these resources in an EXE or another DLL, you need to use LoadLibrary to get a handle to the DLL, and then LoadBitmap to get a handle to the bitmap:

var
  DllHandle: THandle;
  CloseButtonBmp: TBitmap;
  OpenButtonBmp: TBitmap;
begin
  DllHandle := LoadLibrary('MyImages.dll');
  if DllHandle <> 0 then
  try
    CloseButtonBmp := TBitmap.Create;
    CloseButtonBmp.Handle := LoadBitmap(DllHandle, 'CLOSEBUTTON');
    OpenButtonBmp := TBitmap.Create;
    OpenButtonBmp.Handle := LoadBitmap(DllHandle, 'OPENBUTTON');
    {...}
  finally
    FreeLibrary(DllHandle)
  end;
else
  ShowMessage(SysErrorMessage(GetLastError))
end;

Once you've loaded the bitmaps, you can assign them to their final destinations, wherever that may be, and then free them.

<< Back to main page