While creating an application, I needed to implement some kind of update system. It needed to be modular so I could add and modify existing parts. I also wanted to mould it exactly how I wanted.
CWebUpdate is the result.
How does it work?
CWebUpdate's core revolves around some kind of update file, in the format of:
[filename] [sha-1 hash]
[filename] [sha-1 hash]
which looks a bit like this:
You can generate SHA-1 hashes for files using CHash's demo project.
CWebUpdate is told to check for updates, it downloads this file. It then scans a directory for files in this list. It keeps an internal array of missing files and updated files (each is hashed with SHA-1 and compared).
In this way, you can easily ascertain if files have been added and updated, and then update them.
What's going on behind the scene?
URLDownloadToFile to download files, in a thread. This allows the dialog to be unfrozen while downloads take place.
SHA-1 is used to check file's integrities and that they are the same/different to the ones in the update. The code for this is taken from CHash.
Putting CWebUpdate to use
CWebUpdate is easy to implement (or so I think). Step by step guide to setting up an update system:
- Include CWebUpdate.h in your project somewhere.
- Declare a
- Set the remote URL and update URL.
- Set the local directory to be downloaded to (this can be found automatically by
CWebUpdate if necessary).
- Search for updates.
- Check if any were found, present them to the user (if you want) and update them if necessary..
Right, now, a code sample:
// Have CWebUpdate get this exe's path
// Set the URL for the update file and where downloads are stored
// Check for updates
// If a new file called "important.exe" was found, download it
for (int i = 0; i < updObj.GetNumberMissing(); i++)
if (updObj.GetMissingAt(i) == "important.exe");
Setting up the class
Returns the current directory files will be downloaded to.
Returns the current URL files will be downloaded from.
Returns the current update file URL.
void SetLocalDirectory(LPCSTR pathTo, bool generate)
Sets the directory files will be downloaded to. If generate is
true, the path will be the directory the exe is running from.
void SetRemoteURL(LPCSTR url)
Sets the URL files will be downloaded from.
void SetUpdateFileURL(LPCSTR url)
Sets the current update file URL.
Checking for updates
true if an update file was found and parsed, otherwise
Returns the number of different files found.
Returns the number of missing/new files found.
Returns the number of up to date files found.
CString GetDifferentAt(int i)
Returns the filename of a different file at a location.
CString GetMissingAt(int i)
Returns the filename of a missing/new file at a location.
CString GetSuccessfulAt(int i)
Returns the filename of an up to date file at a location.
The demo project contains a full example of implementing
- 25th June 2005: First release. (I sense another imminent one.)