I am writing a c++ program which sooner or later will need to be updated. I want this to require as little interaction with the user as possible since the program runs constantly in the background and never terminates. I know that under normal circumstances, you can't write to a file that's being executed, so this is my question: is it possible for an application to modify itself without just running another executable and letting that one take over? I can't have more than one file.
A:
You should be able to send a command to the program to tell it to terminate, then update the executable file, then relaunch. It should also save any state so that it can continue where it left off when you restart it, if that matters.
siride
2010-07-29 14:38:13
Send the command from where? The program that's trying to update itself? Or from a separate update program? I think the user wants to write self-modifying code.
FrustratedWithFormsDesigner
2010-07-29 14:40:22
If the program is terminated, how do I get to the "update the executable file" part?
moogoo
2010-07-29 14:40:33
Frustrated, if self-modifying code (which I have no experience with) means writing assembly then ill have to take a different route. And the changes do have to save to disk for when the computer shuts down and stuff.
moogoo
2010-07-29 14:43:18
I'm trying not to encourage self-modifying code. I was meaning a separate update program, or just the new copy of the program itself, telling the old copy to quit (see the answer by SirDarius below/above).
siride
2010-07-29 15:15:47
@moogoo: you would have your update program launch the new copy of the executable after killing the old version and copying over the new version.
siride
2010-07-29 15:16:52
The place where I said it must be obscure, but I've already said I can only have one file and unpacking one two from one isn't an option, so I can't have a separate update program.
moogoo
2010-07-29 15:22:04
Then the program itself can have an update mode.
siride
2010-07-29 15:32:57
That doesn't solve the problem of having to change the file that is executing, unless you do something like what sirdarius said.
moogoo
2010-07-29 15:58:46
Yes it does. You still terminate the old program and copy over the new program. The question is who initiates the termination, copy and relaunch. It's pretty much the same thing, which is why I referred you to SirDarius's answer and voted it up.
siride
2010-07-29 16:04:55
Oh ok then, I misunderstood.
moogoo
2010-07-29 16:28:02
A:
There are some languages like scripting languages or .NET/Java that can allow you to modify your own code. However, C++ does not provide any runtime code generation or modification tools. If you don't want to change language, you're stuffed. It's possible that you could get some nonstandard tool like LLVM to do it, I guess.
DeadMG
2010-07-29 14:45:20
+2
A:
Most programs using auto-update features use another executable to take over execution in order to avoid that problem.
However I see a somewhat simple solution:
old programdownloadsnew programinto a temporary locationold programstartsnew program(possibly with a special parameter) and terminates itselfnew programcopies itself overold program(it should be ok for a running program to read itself, as long as it doesn't open itself for writing)new programrunsnew program (copy)with a special argument, and then terminates itselfnew program (copy)deletesnew programand resumes normal operations
Is it clear enough ? :)
SirDarius
2010-07-29 14:50:37
Yeah that actually made sense. I'll chew on that for a while to see if its possible or not for the way things need to be done. Thoughtful answer, someone update thisfor me.
moogoo
2010-07-29 14:58:34
Depending on the OS, you may be able to rename the file of a running executable. That would allow you to rename the existing file, move the new one to the proper name, and then run it directly from the proper name. Thus saving you a few steps.
TheUndeadFish
2010-07-29 17:09:44