Resource file (Delphi)

{$R filename}
{$RESOURCE filename}

{$R *.xxx}
{$R 'My file'}. 
{$R filename.res filename.rc}  

The $R directive specifies the name of a resource file to be included in an application or library.
The named file must be a Windows resource file and the default extension for filenames is .res.
To specify a file name that includes a space, surround the file name with single quotation marks:

{$R 'My file'}. 

The * symbol has a special meaning in $R directives:
it stands for the base name (without extension) of the source-code file where the directive occurs.
Usually, an application's resource (.res) file has the same name as its project (.dpr) file;
in this case, including {$R *.res} in the project file links the corresponding resource file to the application.

Similarly, a form (.dfm or nfm) file usually has the same name as its unit (.pas) file;
including {$R *.nfm} in the .pas file links the corresponding form file to the application. 

{$R filename.res filename.rc} (where the two occurrences of 'filename' match)
makes the .rc file appear in the Project Manager.
When the user opens the .rc file from the Project Manager, the String Table editor is invoked. 

When a {$R filename} directive is used in a unit, the specified file name is simply recorded
in the resulting unit file.
No checks are made at that point to ensure that the filename is correct
and that it specifies an existing file. 

When an application or library is linked (after compiling the program or library source file),
the resource files specified in all used units as well as in the program or library itself are processed,
and each resource in each resource file is copied to the executable being produced.
During the resource processing phase, the linker searches for .res files in the same directory
as the module containing the $R directive, and in the directories specified in the Search path input box
on the Directories/Conditionals page of the Project|Options dialog box
(or in the directories specified in a -R option on the dccil command line).

How to attach a resource file to an existing executable file?

http://stackoverflow.com/questions/6054300/how-to-attach-a-resource-file-to-an-existing-executable-file

I have a resource file(.RES) and i want to add it into an existing executable file
without recompiling and using the IDE! is it possible?

{$APPTYPE CONSOLE}

uses
  Classes,
  Windows,
  SysUtils;

procedure UpdateExeResource(Const Source,Dest:string);
var
  Stream     : TFileStream;
  hDestRes   : THANDLE;
  lpData     : Pointer;
  cbData     : DWORD;
begin
  Stream := TFileStream.Create(Source,fmOpenRead or fmShareDenyNone);
  try
    Stream.Seek(0, soFromBeginning);
    cbData:=Stream.Size;
    if cbData>0 then
    begin
      GetMem(lpData,cbData);
      try
        Stream.Read(lpData^, cbData);
        hDestRes:= BeginUpdateResource(PChar(Dest), False);
        if hDestRes <> 0 then
          if UpdateResource(hDestRes, RT_RCDATA,'DATA',0,lpData,cbData) then
          begin
            if not EndUpdateResource(hDestRes,FALSE) then RaiseLastOSError
          end
          else
          RaiseLastOSError
        else
        RaiseLastOSError;
      finally
        FreeMem(lpData);
      end;
    end;
  finally
    Stream.Free;
  end;
end;

begin
  try
    UpdateExeResource('C:\Users\Dexter\Documents\RAD Studio\Projects\Debug\Win32\Data.txt','C:\Users\Dexter\Documents\RAD Studio\Projects\Debug\Win32\project86.exe');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

You can use Colin Wilson's excellent Resource Utilities.

http://www.wilsonc.demon.co.uk/files/d10/ResourceUtils100.zip

Resource Utilities is a runtime package that contains classes that manipulate resources
in Windows modules (.EXE, .DLL, .BPL, etc.) and resource files (.RES).

program AddResource; 

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  unitNtModule,
  unitResFile,
  unitResourceRCData;

  procedure AddRes(exeName, resName: string);
  var
    exeModule: TNTModule;
    resFile  : TResModule;
  begin
    if ExtractFileExt(exeName) = '' then
      exeName := ChangeFileExt(exeName, '.exe');
    exeModule := TNTModule.Create;
    try
      exeModule.LoadFromFile(exeName);
      resFile := TResModule.Create;
      resFile.LoadFromFile(resName);
      exeModule.AddResource(resFile.ResourceDetails[0]);
      exeModule.SaveToFile(exeName);
    finally FreeAndNil(exeModule); end;
  end; { AddRes }

begin
  if ParamCount <> 2 then
    Writeln('Usage: AddResource <exe file> <resource file>')
  else
    AddRes(ParamStr(1), ParamStr(2));
end.
Uses Classes, Windows, SysUtils, Dialogs;

Type
  TBuffer = Array[0..0] of Byte;
  PBuffer = ^TBuffer;

Var
  FS             : TFileStream;
  ResourceHandle : THandle;
  DataLength     : DWord;
  Data           : PBuffer;
  Ok             : Boolean;

Begin
   ResourceHandle := BeginUpdateResource(pChar('d:\someexefile.exe'), False);
   IF (ResourceHandle <> 0) Then
   Begin
      FS := TFileStream.Create('d:\somebitmap.bmp', fmOpenRead);
      FS.Seek(0, soFromBeginning);
      DataLength := FS.Size;
      GetMem(Data, DataLength);
      FS.Read(Data^, DataLength);
      FS.Free;

      Ok := True;
      IF (not UpdateResource(ResourceHandle, RT_RCDATA, pChar('MyNewResource'), LANG_SYSTEM_DEFAULT{MakeLangID(LANG_NEUTRAL, SUBLANG_NEUTRAL)}, Data, DataLength)) Then Ok := False;

      IF (not EndUpdateResource(ResourceHandle, False)) Then Ok := False;

      IF (Ok) Then ShowMessage('Update of resources successful!')
         Else ShowMessage('Update of resources failed!');

      FreeMem(Data);
   End;
End. 

 

 
posted @ 2013-06-08 22:29  IAmAProgrammer  阅读(936)  评论(0编辑  收藏  举报