How do I compile resources into my application and get them?

How can I create a single executable package containing DLL files and image files?

Then, how can I extract them from an executable in runtime?

+4
source share
2 answers

Option 1 using the IDE (Delphi 2007 or higher):

You can click the "Project" menu, then select "Resources ...", into which you can upload any file. For your purpose this will be RC_DATA.

Option 2 without IDE

If you do not have the above option, you will need to use BRCC32 (the Borland resource compiler) to create the .RES file from the RC file, which you then link to your Application. To link resource files without using the IDE, try the following:

Let's say, for example, we want to add a couple of DLL files, and the name of the DLL files is MyLib1.dll and MyLib2.dll to add this open notepad, and enter the following:

MYLIB1 RCDATA ".. \ MyLib1.dll"

MYLIB2 RCDATA ".. \ MyLib2.dll"

Make sure the .. \ xxx.dll paths are correct, so obviously you need to edit this.

Now you need to save it as a .rc file, so File> Save As .. (make sure that the drop-down filter is all the files.) And name it MyResources.rc. Now you need to use the Resource Compiler to generate the Res file using this console command:

BRCC32 MyResources.RC

You can write this command using the command line, Start> Run> cmd.exe, otherwise you can find the BRCC32.exe file in the bin folder of your Delphi setup and drag and drop the MyResource.RC file onto.

This will create a file called MyResources.RES, which you can include in the main Delphi form of your application, for example:

 {$R *.dfm} {$R MyResources.res} 

you can extract resources using something like this:

 procedure ExtractResource(ResName: String; Filename: String); var ResStream: TResourceStream; begin ResStream:= TResourceStream.Create(HInstance, ResName, RT_RCDATA); try ResStream.Position:= 0; ResStream.SaveToFile(Filename); finally ResStream.Free; end; end; 
+6
source

What seemed convenient to me was to use a .zip container.

Then you will have two implementations:

  • Add some .zip content to the existing .exe, and the .exe code will extract the .zip content upon request;

  • Paste the .zip content as a resource, then fetch each content as requested.

Solution 1 will add the contents of the .zip after compilation. While 2 will add .zip content when compiling. For the installer, I believe that solution 1 makes sense to me. For a way to get some necessary files (libraries and even bitmaps or text) that are associated with a particular exe release, solution 2 could be considered.

Using the .zip format in the format makes it easy to analyze content and allow compression. Using a tool like TotalCommander, you can even read the contents of a .zip file using Ctrl + PgDown over .exe. Very comfortably.

You will find in this link how you implement solution 1, and in this link (on the same page, but in a different message) how to use the TZipRead.Create() constructor for direct access to the bundled .zip as a resource. You will find in our repository how it works with working applications: for example. how we insert icons, text content and graphviz + spell-checker libraries into the SynProject executable.

On performance, there is no difference between the two solutions, at least with our code. Both use memory mapped files to access the content, so it will be more or less identical: very fast.

+3
source

All Articles