3

How do I substitute the running EXE with a new version ?

DRokie
  • 705
  • 2
  • 9
  • 20
  • a common way is to create a batch (*.bat), and to launch it before closing your current app... then, the batch replaces the previous version... – Whiler Sep 04 '12 at 10:00
  • 2
    You can't. However, you can let program A (your application) start program B (your updater) and then close. Then program B can update program A (which is not running), and then start (the new) program A. Then, if you like, program A can remove program B. – Andreas Rejbrand Sep 04 '12 at 10:00
  • 3
    It is much easier to have the updater start the program (and updates before starting) – whosrdaddy Sep 04 '12 at 11:09

2 Answers2

5

You can do it this way:

  1. rename the running EXE,
  2. copy the new version to the old name,
  3. when the new version is started have it delete the renamed EXE

Note that this is definitely not recommended behaviour.

Community
  • 1
  • 1
Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • 1
    That will fail if your exe is in a protected folder, e.g. under program files with UAC enabled – David Heffernan Sep 04 '12 at 15:32
  • 1
    Indeed: anything on Windows will fail if you do not have the right privileges or have policies that prevent them (just try surviving in a corporate environment and you will certainly be familiar). But enumerating all those is a bit beyond the question at hand. – Jeroen Wiert Pluimers Sep 04 '12 at 15:59
  • But this is one of the reasons why you invariably use a different process to run your updating. Because that process can run elevated. – David Heffernan Sep 04 '12 at 16:00
  • I've seen tons of applications that are not installed locally at all but are temporarily copied locally while running and removed when finished. That's pretty common in the corporate environment. Or apps that run outside the %ProgramFiles% realm (in a portable application setting; user specific Google Chrome does this for instance, so it can auto-update without requiring UAC at all). – Jeroen Wiert Pluimers Sep 04 '12 at 16:07
3

Short: By stopping it, and starting the new version.

Long: When you start an application, the operating system loads the application in memory (or at least the parts that are needed) and sets up all the memory regions the run the program. While the application is running, the binary (what you call the EXE) is not needed anymore by the operating system.

If you would want to replace code in a running application, you could do that by replacing the code segment. I do not know if it is possible or easy in Windows. Another option could be to use some kind of plugin system, where you can replace parts of the application while it is still running.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195