tags:

views:

82

answers:

3

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
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
If the program is terminated, how do I get to the "update the executable file" part?
moogoo
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
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
@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
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
Then the program itself can have an update mode.
siride
That doesn't solve the problem of having to change the file that is executing, unless you do something like what sirdarius said.
moogoo
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
Oh ok then, I misunderstood.
moogoo
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
+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 program downloads new program into a temporary location
  • old program starts new program (possibly with a special parameter) and terminates itself
  • new program copies itself over old program (it should be ok for a running program to read itself, as long as it doesn't open itself for writing)
  • new program runs new program (copy) with a special argument, and then terminates itself
  • new program (copy) deletes new program and resumes normal operations

Is it clear enough ? :)

SirDarius
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
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
Well, it has to work on Linux, Windows, and mac.
moogoo