Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

delphi - 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?

Edit

And how to extract the resource file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If your question is, if you can add a resource to a existing exe file, yes it is possible. To do this you must use the UpdateResource function which can add, delete, or replace a resource in a portable executable (PE) file.

update

Here you have a sample code

{$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:UsersDexterDocumentsRAD StudioProjectsDebugWin32Data.txt','C:UsersDexterDocumentsRAD StudioProjectsDebugWin32project86.exe');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...