1. Technology

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/library/weekly/aa010405a.htm

was emailed to:

Thanks for sharing About.com with others!

Creating and using a resource only DLL with Delphi
Besides storing resource files inside an .EXE file, a Delphi developer can also create a dynamic link library containing only resources. Let's see how.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Part 1: Resource Files Made Easy
• Part 2: Inside the EXE (compiling .rc files)
• Part 3: Web Site inside a Delphi EXE
• Part 5: StringTable resources
• Part 6: Resource Managers
• Part 7: Fixing the "Duplicate resource" error
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• View, modify, add, delete and extract resources in 32bit Windows executables
• Managing DLL's

This is the fourth article in the series about storing more than just executable code inside a Delphi application.
Introductory article described resources as binary data that a resource compiler or developer ads to an application's executable file.
A resource can be either standard or defined. The data in a standard resource describes an icon, cursor, menu, dialog box, bitmap, enhanced metafile, font, accelerator table, message-table entry, string-table entry, or version. An application-defined resource, also called a custom resource, contains any data required by a specific application (another EXE, GIF image, MP3 song, etc.).

RESource only DLL

Dynamic link libraries contain sharable code or resources, they provide the ability for multiple applications to share a single copy of a routine (or resource) they have in common.

Creating a resource DLL
In order to build a DLL with resources as its only content, you need to create and build an empty DLL project, which contains a resource link reference to the .res file that contains your resources.

Here are the steps to follow:

  1. Create a RC file "describing" resources you are to place in a DLL. Here's an example ("adpdllresources" is the name of the RC ASCII file) - one ICON and one GIF image are added to the RC file:

    adpdllresources.rc

    aboutlogo RCDATA aboutlogo.gif
    factory ICON FACTORY.ICO

  2. Compile the RC file into a RES file with the BRCC32 resource compiler.

  3. Create an empty DLL project. Save it as "adpResources.dpr" - when compiled the DLL will be named "adpResources.dll". Here's the full source to the DLL project (only one file, and yes only 4 lines):

    library adpResources;
    
    {$R adpdllresources.RES}
    
    begin
    end.
    

  4. Compile your DLL (make sure the "adpdllresources.RES" is in the same folder as the DLL)
Once the DLL with resources is created, you can use it inside your Delphi applications. Note that since the resources are inside a DLL, any application can use 'em (no need to be developed with Delphi).

Using your resource only DLL
To use you resource only dynamic link library, simply load the DLL and the resources you wish to use.

Here are the steps to follow:

  1. Create a new Delphi project. By default, Delphi ads one empty form to the project. Save the project.
  2. Copy the DLL with resources ("adpResources.DLL") into the folder where your new application was saved
  3. Load the resource you are after...

Here's how to load the "factory" icon and draw it on a Form1's Canvas (when Button1:TButton was clicked).

procedure TForm1.Button1Click(Sender: TObject);
const
  resICON = 'factory';
var
  h : THandle;
  Icon : HIcon;
begin
  h := LoadLibrary('adpResources.DLL');
  try
    if h <> 0 then
    begin
      Icon := LoadIcon(h, resICON);
      DrawIcon(Canvas.Handle, 10, 10, Icon);
    end
    else
    begin
      ShowMessage('Load Resource DLL FAILED!');
    end;
  finally
    FreeLibrary(h);
  end;
end;

Resources from a DLL

If you have added GIF support to Delphi, you can grab the GIF image stored in resource DLL and draw it also:

procedure TForm1.Button2Click(Sender: TObject);
const
  resGIF = 'aboutlogo';
var
  h : THandle;
  gif : TGifImage;
  r:TRect;
begin
  h := LoadLibrary('adpResources.DLL');
  try
    if h <> 0 then
    begin
      gif := TGIFImage.Create;
      try
        gif.LoadFromResourceName(h,resGIF);
        gif.Paint(Canvas,
                  Form1.ClientRect,
                  [goDirectDraw]);
      finally
        gif.Free;
      end;
    end
    else
    begin
      ShowMessage('Load Resource DLL FAILED!');
    end;
  finally
    FreeLibrary(h);
  end;
end;

And that's all. Download the sample project ... and try for yourself - it's easy and fun.

©2014 About.com. All rights reserved.