Posts: 9
Threads: 4
Joined: Mar 2013
Hi,
Can I add or edit resources in the executable once it has been compiled?
For security reason I need to integrate a database mdb file to qm exe.
At application startup the database will be extracted and saved in a temp directory.
At application unload, the database will be taken from the temporary location and reinserted in the file as a resource. The old database resource will be overwritten.
Any chance to do this?
Thank's
Alessandro
Posts: 12,090
Threads: 142
Joined: Dec 2002
While executing, the exe file is locked and cannot be modified. It cannot modify resources in self. Should be possible with a temporary copy of self, but it is unusual way, unreliable, not recommended, and quite difficult. Exe copies self to exe2, runs exe2 and exits, then exe2 modifies exe, then it runs exe and exits, then exe deletes exe2 and exits.
If possible, instead use a hidden file created on user's computer. If need an initial database, you can add it to exe. Then extract it to a hidden file if the file does not exist. Usually for this is used $temp$ folder.
Also look in stackoverflow etc.
Google for
modify program resources at run time "UpdateResource"
http://stackoverflow.com/questions/4577 ... executable
http://stackoverflow.com/questions/7201 ... -resources
Posts: 9
Threads: 4
Joined: Mar 2013
Gintaras Wrote:Exe copies self to exe2, runs exe2 and exits, then exe2 modifies exe, then it runs exe and exits, then exe deletes exe2 and exits.
Thank's Gintaras!
I like to try your first solution but I did not understand very well the following passage: "then exe2 modifies exe"
I try to implement it following your links.
Thank's
Alessandro
Posts: 12,090
Threads: 142
Joined: Dec 2002
Loads exe using BeginUpdateResource, and updates resurce using UpdateResource and EndUpdateResource. These are Windows API functions, documented in MSDN library.
Then the exe file cannot be in Program Files or other folder where only administrators can modify files. Or it itself must run as administrator.
Posts: 9
Threads: 4
Joined: Mar 2013
Hi Gintaras,
I try to replace mdb database with UpdateResource function. The function work fine and replace database in my exe but this code corrupts my DB.
str tempexesave.from(_qmdir "temp.exe")
str mdbtosave.from(_qmdir "data.mdb")
;create a exe copy
cop ExeFullPath tempexesave
;replace old database
int r=BeginUpdateResource(tempexesave 0)
str filedainc.getfile(mdbtosave)
long lenfiledainc=filedainc.len
UpdateResource(r +10 +200 0 &filedainc lenfiledainc)
r=EndUpdateResource(r 0)
What is wrong?
Thank's
Alessandro
Posts: 12,090
Threads: 142
Joined: Dec 2002
UpdateResource(r +10 +200 0 filedainc filedainc.len)
Posts: 9
Threads: 4
Joined: Mar 2013
Thank's Gintaras
Now it works very well.