Update the program by writing to the running file


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.

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 ? :)