6

I have a Windows program that needs to update itself from a server automatically.

It is already able to transfer files from the server and update + validate things like DLL plugin files before loading them.

However, this program also needs to update itself. There are probably a few different ways this could be done, and the most obvious one I've seen from various online game clients is creating an "auto patcher" that downloads and then runs the client executable. That introduces the problem of having to update the autopatcher, so if there is a more elegant solution I'd like to hear about it.

I have to imagine there is a way to download what will be the new executable file as a temporary file, let's say "client.exe.tmp" and then launch a separate process that waits for the original client.exe to exit and then renames/copies the new file over the top of it.

Has anyone done this sort of thing before successfully and what method did you use to get it to work?

Jason Champion
  • 2,670
  • 4
  • 35
  • 55

3 Answers3

17
  1. Running exe downloads the new one, puts it somewhere
  2. Running exe renames itself to anything (like .exe.tmp)
  3. Running exe puts the downloaded exe where the running one is (named just like the original)
  4. Running exe starts the downloaded exe
  5. Downloaded exe checks for .exe.tmp file, if found deletes it and kills the old running process
  6. Done
Maxem
  • 2,634
  • 1
  • 20
  • 14
  • 4
    "Running exe renames itself to anything (like .exe.tmp)" sounds somewhat tricky. Can you rename a file while it's being executed? – pezcode Aug 26 '11 at 00:53
  • 4
    Yes, you cannot delete it but you can rename it while it's running. – Maxem Aug 26 '11 at 00:54
  • 2
    That being said I guess the easiest way would be for the exe to create a copy of itself, then execute the copy which does all the replacing. – pezcode Aug 26 '11 at 00:55
  • And how do you get rid of the running copy? Starting the updated exe from the running one and letting the newly started exe do the killing / deletion is somewhat safer as it means that the new exe is at least working to the point that it can be started. – Maxem Aug 26 '11 at 01:00
  • In practice I've found this only works 50% of the time. Quite often the .exe.old still exists for some reason and won't delete because it's in use (even after the executing .exe has been closed down), therefore the current version can't be renamed to .exe.old and the application never updates. – Marcus Jan 03 '13 at 16:30
  • I tried renaming my executable while running in Dart but I get this error: `ERROR - FileSystemException: Cannot rename file to 'myexec.tmp', path = 'exec' (OS Error: The process cannot access the file because it is being used by another process., , errno = 32)`. Did this work for anyone? – Renato Jun 05 '21 at 12:20
4

I like the patcher/maintenance/feature add/remove tool approach. Even if for some reason you need to update something as trivial, I see no "chicken or egg paradox" here, it is more of a "one hand washes the other" thing.

  1. Application checks server for updates, if any, check if patcher is up to date, if needed, application updates patcher
  2. Patcher is executed as a separate process, downloads the update, and notifies application to prompts to install it
  3. You agree, application quits notifying the patcher, patcher unpacks data, replaces exe, does additional stuff that may be needed by the new version and launches it and terminates
dtech
  • 47,916
  • 17
  • 112
  • 190
0

Consider ClickOnce deployment which will help you to insall/replace/update latest version from shared location and executed.

Anto Varghese
  • 3,131
  • 6
  • 31
  • 38