2010. október 31., vasárnap

How to create and use a resource-only DLL


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


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;




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:

  DllHandle: THandle;
  CloseButtonBmp: TBitmap;
  OpenButtonBmp: TBitmap;
  DllHandle := LoadLibrary('MyImages.dll');
  if DllHandle <> 0 then
    CloseButtonBmp := TBitmap.Create;
    CloseButtonBmp.Handle := LoadBitmap(DllHandle, 'CLOSEBUTTON');
    OpenButtonBmp := TBitmap.Create;
    OpenButtonBmp.Handle := LoadBitmap(DllHandle, 'OPENBUTTON');

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

Nincsenek megjegyzések:

Megjegyzés küldése