4

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. 
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Kermia
  • 4,171
  • 13
  • 64
  • 105

2 Answers2

7

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I've also used `TResourceStream.SaveToFile`. but doesn't work and this error will be shown : `Resource [MyResName] not found.` – Kermia May 21 '11 at 10:19
  • And you were calling it from within the executable itself? In that case your code simply contains a bug, and _that_ is the code you should post. – Paul-Jan May 21 '11 at 10:27
  • 2
    most likely you got the HMODULE, resource name or resource type wrong. – David Heffernan May 21 '11 at 10:30
  • My code to extract the resource : `ResStream := TResourceStream.Create(HInstance, 'MyNewResource', RT_RCDATA);` – Kermia May 21 '11 at 10:31
  • you'll need to use `LoadLibrary(PChar('d: \someexefile.exe'))` to get the right module instance as per my answer. Your code currently looks for the resource in your update/extract module rather than the exe you are modifying. – David Heffernan May 21 '11 at 10:35
  • `hMod := LoadLibrary(PChar ('d: \someexefile.exe'))` and then pass hMod rather than HInstance. – David Heffernan May 21 '11 at 10:38
  • Does not work ! `var ResStream: TResourceStream; hMod: Cardinal; begin hMod := LoadLibrary(PChar(ExtractFilePath(application.ExeName))); ResStream := TResourceStream.Create(hMod, 'FPM_OPT', RT_RCDATA); try ResStream.SaveToFile('c:\1.txt'); finally ResStream.Free; end;` – Kermia May 21 '11 at 10:42
  • That's equivalent to using HInstance. Is the resource in the executable in which this code lives, or some other executable? I'm finding it hard to work out what you have done wrong with so little clear detail. – David Heffernan May 21 '11 at 10:50
  • David , I found the problem. `lptype` was incorrect. Thank you so much :) – Kermia May 21 '11 at 10:51
  • 1
    good. If the resource is in the running executable then just use HInstance. No need for LoadLibrary in that case. – David Heffernan May 21 '11 at 10:53
3

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?

Community
  • 1
  • 1
RBA
  • 12,337
  • 16
  • 79
  • 126