How to do file self-updating (Native C ++)

I am using Microsoft Visual Studio 2008 with targeted deployment of Windows. How can I make a self update file? I already have the "network transfer" part, but how do I make the executable file an inscription?

Basically, I want to write an auto-updater for a directory that also includes auto-update, and the updater should update EVERYTHING in the directory.

Perhaps ways to defer changes to the file until a file lock is released will work. If I did, I would probably follow him with a hot patch.

+6
c ++ windows native
source share
7 answers

Write a new executable file and delete or save a copy of the old one - you can send diff over the network and have a third program, an update monitor, or something else to apply it. This will be just a small script that can be run from a remote application when it implements an update. The updater can rename itself to $ UPDATER_OLD_VERSION or whatever, write an updated copy of itself with the corresponding name, and then, when a new update file is launched, check if there is a file named $ UPDATER_OLD_VERSION in the application directory and delete it. All other files can be simply updated / overwritten.

+5
source share

Here's how I did it recently. There is a short-term overlap of the old program and the launch of new programming. It uses the trick that you can rename the current executable file and bring in a new file before closing the old one. This is not 100% fault tolerant, but the only way it could "mask" your application if CopyFile fails.

#include <windows.h> #include <iostream> #include <fstream> void UpgradeService::UpgradeSelf() { std::string temp = root + "\\myprogram_tmp.exe"; remove(temp.c_str()); // ignore return code std::string src = upgradeFolder + "\\myprogram.exe"; std::string dst = root + "\\myprogram.exe"; rename(dst.c_str(),temp.c_str()); CopyFile(src.c_str(),dst.c_str(),false); static char buffer[512]; strcpy(buffer,dst.c_str()); /* CreateProcess API initialization */ STARTUPINFO siStartupInfo; PROCESS_INFORMATION piProcessInfo; memset(&siStartupInfo, 0, sizeof(siStartupInfo)); memset(&piProcessInfo, 0, sizeof(piProcessInfo)); siStartupInfo.cb = sizeof(siStartupInfo); ::CreateProcess(buffer, // application name/path NULL, // command line (optional) NULL, // no process attributes (default) NULL, // default security attributes false, CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE, NULL, // default env NULL, // default working dir &siStartupInfo, &piProcessInfo); ::TerminateProcess( GetCurrentProcess(),0); ::ExitProcess(0); // exit this process // this does not return. } 
+4
source share

Usually you cannot modify the application while it is running. You will need to close the source program and change it from another location.

+3
source share

I do this with InnoSetup setup.exe, which starts silently and disables my application before it starts installing new files. I had to add custom Pascal [Code] sections to get everything turned off, but it does the job.

+2
source share

You cannot overwrite a file at runtime, but you can rename it. In my update, I will rename the existing exe to something meaningful (oldname_dateandtime), and then write a new copy. Then automatically shut down our system and restart it from the service. When the application starts, a new version starts.

+2
source share

When I did the update, I have to separate the program a little. The main reason was the same as what you want to do. Let's say B updates the target application plus A and A updates B. I tried to find another way, but what I did was done in a small and simple update :(

0
source share

I think you are talking about file versions and updates. As you said, you need autoupdater. Therefore, you need to create a service that monitors your installation directory and compares the version of the file on your disk with the server.

As soon as a discrepancy occurs, you can request the latest data from the server. If the files you need to replace are used, you should mark them to remove / unregister during the next reboot, and you can mention this using the registry.

You can copy all the files to the cache and later during the reboot, copy them to the installation directory.

0
source share

All Articles