Mega Code Archive

 
Categories / Delphi / Files
 

Embedding files in an exe and then extracting them using D5

Title: Embedding files in an exe and then extracting them using D5. Question: Isn't there an easy way to embed files in an exe and then extracting them again if you have the power of Delphi 5 at your fingertips? Answer: There has been a few articles written around this subject and the odds are that you have done a bit more work than you have to, because in Delphi 5 there is a much easier way of doing this. First of all I want to introduce you to a demo program that ships with Delphi5. Its called "Resource Explorer", it is located under "'Delphi5'\Demos\Resxplor" and it, as you might have guessed, explores resources. This is a handy little tool that is able to strip just about any resource from a dll or an executable and you will be amazed by how many pointers there are in winword.exe. In this article it is used just to confirm that the resources have been added to our little resource test. Adding and extracting resources from an exe in Delphi 5 is as easy as 1 2 3. 1. Add resources to your project. 2. Using "Resource Explorer", confirm that they have been added. 3. Extract the resources from your exe. And now in more detail: 1. Start a new application with File | New Application and add a textfile to your project with File | New -- Text. This Textfile will contain all your resources in the form 'resource name' 'resource type' 'file name' e.g. "PDF RCDATA foo.pdf" where RCDATA means an arbitrary binary file. If you want to add more resources just add more lines. Now save your project and the only tricky part here is that the Delphi editor adds a ".txt" extension to your file and in this case we want an ".rc" extension to our file. You can force Delphi to save this file with the ".rc" extension if you add double quotes around the filename e.g. ""MyResources.rc"". Finally use Project | Add to project.., select your newly created "MyResources.rc" (you won't see it if you don't turn off the "*.pas" filter) and save your project one more time. 2. Build your project and confirm that your resource has been added to the executable by running "Resource Explorer". Open your executable and check that your resource has been added under "RCData". 3. Now its time to extract your resource from the executable and once again the guys at Borland has made something very easy. The magic is all contained in TResourceStream and all you have to do is this: procedure TForm1.Button1Click(Sender: TObject); var RS : TResourceStream; begin RS := TResourceStream.Create( 0, 'PDF', RT_RCDATA); try RS.SaveToFile('bar.pdf'); finally RS.Free; end; end; TResourceStream.create creates a resource stream that contain a resource from the current process "0", has the name "PDF" and is of the predefined constant type "RT_RCDATA". This is my very first article ever witten in english, so please fell free to rate it and tell me what you think. Thats all folks!