Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Extracting Resource Data from an Exe

Title: Extracting Resource Data from an Exe Question: Article 832 mentions how one can put Files into an Exe. This shows you how you can get it out Answer: I've written a simple function that does this for you, just cut and paste (some detail is below) procedure ExtractToFile(Instance:THandle; ResID:Integer; ResType, FileName:String); var ResStream: TResourceStream; FileStream: TFileStream; begin try ResStream := TResourceStream.CreateFromID(Instance, ResID, pChar(ResType)); try //if FileExists(FileName) then //DeleteFile(pChar(FileName)); FileStream := TFileStream.Create(FileName, fmCreate); try FileStream.CopyFrom(ResStream, 0); finally FileStream.Free; end; finally ResStream.Free; end; except on E:Exception do begin DeleteFile(FileName); raise; end; end; end; You pass in the Instance of the exe or dll (the same application would just be Application.Instance or Application.Handle, for a dll you got to get that yourself :) ResID is the same ID that was assigned in the resource ResType WAVEFILE, BITMAP, CURSOR, CUSTOM - while this should work with all Resource Types, I've only have success with CUSTOM FileName the file you would like to create from the resource It's pretty easy, create a resource stream, create a file stream, copy what's in the resource stream to the file stream.