Kermia
Kermia

Reputation: 4221

Extract the resource file

I have attached a resource file to an existing exe file using UpdateResource function.

How can I extract it?

Edit

This is my code to attach a resource file to an existing exe file :

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. 

Upvotes: 4

Views: 2648

Answers (2)

David Heffernan
David Heffernan

Reputation: 613592

Use LoadLibraryEx passing LOAD_LIBRARY_AS_IMAGE_ RESOURCE to load the module and then TResourceStream.SaveToFile to save the resource.

I am of course assuming that you don't want to extract the resource from the running executable. If that were the case you could go straight to TResourceStream.SaveToFile.

Upvotes: 7

RBA
RBA

Reputation: 12584

there are several tools you can use:

XN Resource Explorer

Resource Hacker

Resource-Grabber

from code by using DelphiDabbler.

or by using TResourceStream class. this question explain you how to use it: How can I extract a resource into a file at runtime?

Upvotes: 3

Related Questions