12

I want to distribute only a single .exe, however, at runtime I would like it to extract some embedded image resources to the users hard disk drive.

Can I, and if so, how?

menjaraz
  • 7,551
  • 4
  • 41
  • 81
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

4 Answers4

19

Use Delphi's TResourceStream. It's constructor will find and load the resource into memory, and it's SaveToFile method will do the disk write.

Something similar to this should work:

var
  ResStream: TResourceStream;
begin
  ResStream := TResourceStream.Create(HInstance, 'YOURRESOURCENAME', RT_RCDATA);
  try
    ResStream.Position := 0;
    ResStream.SaveToFile('C:\YourDir\YourFileName.jpg');
  finally
    ResStream.Free;
  end;
end;

If you can use the resource ID instead of name, it's a little less memory. In that case, you'd resplace Create with CreateFromID, and supply the numeric ID rather than the string name.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 3
    Whatever value the OS tells you, @Leonix. `HInstance` is a global variable that Delphi initializes for you as your program starts up. It's in the *SysInit* unit. – Rob Kennedy Sep 10 '10 at 14:16
4

Create a TResourceStream. You'll need the module instance handle (usually SysInit.HInstance for the current EXE file, or else whatever you get from LoadLibrary or LoadPackage), the resource type (such as rt_Bitmap or rt_RCData), and either the resource name or numeric ID. Then call the stream's SaveToFile method.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
2
try
   if not Assigned(Bitmap)
   then
      Bitmap := TBitmap.Create();
   Bitmap.LoadFromResourceName(HInstance,SRC);
except
   on E:Exception do
      ShowMessage(e.Message);
end;

And then save the Bitmap to disk.

Edelcom
  • 5,038
  • 8
  • 44
  • 61
1

Maybe this might come in handy too if you need to work with the resources itself. Delphidabbler / ResourceFiles

stOrM
  • 103
  • 1
  • 9