Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I embed a binary within a Delphi executable and extract at runtime?

Tags:

delphi

I'd like to embed an executable inside of my Delphi binary and extract it at runtime. The purpose of this is to ensure that a helper utility is always available on the system, without having to distribute multiple files.

With Delphi 2007 I have used JvDataEmbedded, but I am building a console application and I want to know if anyone knows of another way to do it without having to add a hidden form for JvDataEmbedded.

I am using Delphi 2010.

like image 216
Mick Avatar asked Mar 23 '26 05:03

Mick


2 Answers

Build a RC file that embeds this program as a resource, and then at runtime you can extract it with a TResourceStream. (See the accepted answer to this question of mine for the general principle.) You can chain the TResourceStream to a TFileStream to write it out to a temp file, then run it.

like image 187
Mason Wheeler Avatar answered Mar 24 '26 18:03

Mason Wheeler


Cool Trick, Mason! To add... It occurred to me that this could run into trouble with UAC/permissions if the "mothership" dumps teh "scout ship" diretly into the same directory as it's running from. If you're on Vista/Windows7, regular programs are prevented from writing to the "\program files" directory, when run without elevated permissions. So in that case, the file will likely be placed into a shadow directory. It gets better: you may have different results if you launch the "mothership" program from an installer, as setup programs have special access, and they may temporarily give that access to programs that they launch (which makes it somewhat hazardous to always say YES to "do you want to run the program now?" at the end of installation).

like image 28
Chris Thornton Avatar answered Mar 24 '26 18:03

Chris Thornton