Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing a Binary Resource in an Existing Executable 2

Status
Not open for further replies.

velocirapt0r

Programmer
Dec 4, 2010
6
AU
I have written a program for a small organisation that monitors employee activities, and has a feature that will automatically run a second program or file that is packaged (as a resource within the executable) with it when it is distributed to the employees over the network. In the past, I have always recompiled and edited the resource file whenever I want to change what is packaged with it, but I now have a few people working under me. They are fairly new, and I don't want to trust them with my source for fear that they will fiddle with it, but I do want them to be able to change what is packaged with it, so I have come to the conclusion that the best way to do this is to compile a copy of the program with a fairly vast (say 5MB) dummy resource which can be partially or completely overwritten by a second program which will essentially copy an executable into the place of the available space. I am fairly sure that such a thing is possible, as I have successfully done it with a text file, but binary is presenting a problem.

What I am really asking is can anybody suggest a way in which I might replace a binary resource in an executable using a delphi application? I don't want to use a res hacker; I would like something that gives my subordinates an easy to use GUI, which means I need to do it at runtime in a delphi application.

Thank you in advance to anyone who can give me a hand with this. I am hardly inexperienced with Delphi, but I admit that I am in a pickle...
 
Why is it so important that your main executable contain the packaged file? Your problem could be solved so easily if you simply distributed the packaged file as a secondary file, and have the main program call it when necessary.
 
I'm aware of this, but prefer the packaging solution for several reasons:

* for neatness of the transfer (a customised email client of mine),
* because I have other projects that could be assisted by this,
* because I'm honestly curious, and
* because I didn't become a programmer to give up when I encounter a challenge. I have tried everything I know how to do, I always want to learn more, and because even a failed suggestion from someone on this forum could spark an idea for something I have not yet tried.

Actually, at this point, I'm thinking of just putting all the source and the barebones of Delphi's compiler into a resource file... might have to find a different transfer protocol... :p
 
Okay, fair enough..
It's late at night where I am, so I'm off to bed in a few minutes, but I'll post something for you tomorrow. It's actually not that difficult, but when you're trying to do it for the very first time, it can seem daunting.
 
Thanks, whosrdaddy; that looks pretty much perfect! I'll try integrating in the morning :)
 
Sorry about the late reply. The last couple of days have been crazy for me.

Okay, so back to your problem. The easiest solution (by far) is this:

procedure TForm1.Button1Click(Sender: TObject);
begin
// store the target file inside your main application
JvDataEmbedded1.DataLoadFromFile('c:\temp\MyFile.exe');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
// extract the target file from your application
JvDataEmbedded1.DataSaveToFile('c:\temp\OutputFile.exe');
end;

That's it. It doesn't get any simpler than that. You can call it at runtime and attach any number of files you want.

If you already use JVCL, you already have everything you need. Simply drop a "TJvDataEmbedded" component on your form, and you're ready to go.

If you're not familiar with JVCL, download the latest version and install it from here:


I first started using the DataEmbedded component about 7 years ago, and it always saved me a lot of time and headaches.
 
Don't worry about it; I've had plenty of other things to do too.
Wow, I can imagine it has...
Yes, I am familiar with the JEDI team, and have used a few components before, but never properly explored the whole library. Thank you so much!
 
Am I missing something? It just keeps it in memory? I could achieve that with a filestream... of course, this means much less work if I wanted to copy a file multiple times or something, but as soon as I close the application, I lose it... What I need to do is to put the target file inside the application through either the main application or through a second one.
 
It's not in memory. The file gets physically attached to your main executable when you call DataLoadFromFile, and it remains there permanently. And then when you need to extract the file, you call the DataSaveToFile procedure and it extracts the file to your hard drive.
 
Okay...I must've been having a blonde moment, because I totally screwed that up.

The component I mentioned really DOES attach files into your main executable, but the catch is you have to do it at design time by double-clicking on the component and selecting the file you want to attach. And when you're ready to use it, you extract it with DataSaveToFile.

For some reason, I assumed that DataLoadFromFile can also attach files to the executable at runtime, but apparently that's not the case.

Sorry about the mixup.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top