How I Compile Resources into my Application and Access them?

大憨熊 提交于 2019-12-19 08:29:05

问题


How can I make a single executable package that contains DLL and Image Resource Files?

Then how do I extract them from my Executable at Runtime?


回答1:


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

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

Option 2 without the IDE

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

Lets say for example we want to add a a couple of DLL files, and the name of the DLL files are MyLib1.dll and MyLib2.dll, to add this open Notepad, and type the following:

MYLIB1 RCDATA "..\MyLib1.dll"

MYLIB2 RCDATA "..\MyLib2.dll"

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

Now you need to save this as a .rc file, so File>Save As..(make sure the dropdown filter is All 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 that command by using the Command Prompt, Start Menu > Run > cmd.exe, alternatively you can find the BRCC32.exe inside the bin folder of your Delphi setup and drag the MyResource.RC file onto.

This will create a Res file named MyResources.RES which you can include inside the Main Delphi form of your Application, like so:

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

you can extract the resources by 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;



回答2:


What I've found out to be convenient, is to use a .zip container.

Then you'll have two implementations:

  1. Append some .zip content to an existing .exe, and the .exe code will retrieve the .zip content on request;

  2. Embed the .zip content as a resource, then extract on request each content.

Solution 1 will add the .zip content after compilation. Whereas 2 will add the .zip content at compilation. For a setup program, I think solution 1 makes sense to me. For a way of retrieving some needed files (libraries, and even bitmaps or text) which are linked to a particular exe release, solution 2 could be envisaged.

Using .zip as format make it easy to parse the content, and allow compression. Using a tool like TotalCommander, you can even read the .zip file content with Ctrl+PgDown over the .exe. Very convenient.

You'll find in this link how you implement solution 1, and in this link (same page, but another post) how to use the TZipRead.Create() constructor to directly access to a .zip bundled as resource. You'll find in our repository how it works with working applications: e.g. how we embedded icons, textual content and graphviz + spell-checker libraries in the SynProject executable.

About 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.



来源:https://stackoverflow.com/questions/6042222/how-i-compile-resources-into-my-application-and-access-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!