Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract a resource into a file at runtime?

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?

like image 857
Mawg says reinstate Monica Avatar asked Sep 10 '10 12:09

Mawg says reinstate Monica


1 Answers

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.

like image 103
Ken White Avatar answered Oct 12 '22 11:10

Ken White