How I Compile Resources into my Application and Access them?

后端 未结 2 387
故里飘歌
故里飘歌 2021-01-13 17:41

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?

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-13 18:25

    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;
    

提交回复
热议问题