delphidelphi-11-alexandria

How to load a RES file in runtime?


I know how to read a RES file embedded in EXE file and extract data from it : images, strings etc. But for this to work, i need to include a RES file before compiling my EXE, like so :

{$R myfile.res}

But is there a way to load a RES file that is NOT embedded in the EXE, in runtime ?

Thanks


Solution

  • There is no API specifically for loading resources from a .res file. However, the basic format of a .res file is documented on MSDN:

    Resource File Formats

    Resource Types

    Basically, a .res contains a series of RESOURCEHEADER entries, each one representing a specific resource. You will have to open, read, the parse the file data yourself.


    The alternative, as suggested by @AndreasRejbrand, is to compile the .res file into a DLL, and then you can load the DLL at runtime using LoadLibraryEx() specifying the LOAD_LIBRARY_AS_DATAFILE or LOAD_LIBRARY_AS_IMAGE_RESOURCE flag. You can then use the standard resource APIs that you are already familiar with to load resources from that DLL, using the HMODULE handle returned from LoadLibraryEx().