Introduction
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?
Right, well, 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:
readme.txt 59c23b37d5f063e2a01938d3d06583dc2ea0a933
You can generate SHA-1 hashes for files using CHash's demo project.
So, when 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?
CWebUpdate
uses 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
CWebUpdate
object.
- 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:
CWebUpdate updObj;
updObj.SetLocalDirectory("", true);
updObj.SetUpdateFileURL("http://yourwebsite.com/program/update.txt");
updObj.SetRemoteURL("http://yourwebsite.com/program");
updObj.DoUpdateCheck();
for (int i = 0; i < updObj.GetNumberMissing(); i++)
{
if (updObj.GetMissingAt(i) == "important.exe");
updObj.DownloadMissing(i);
}
CWebUpdate reference
Setting up the class
CString GetLocalDirectory()
Returns the current directory files will be downloaded to.
CString GetRemoteURL()
Returns the current URL files will be downloaded from.
CString GetUpdateFileURL()
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
bool DoUpdateCheck()
Returns true
if an update file was found and parsed, otherwise false
.
int GetNumberDifferent()
Returns the number of different files found.
int GetNumberMissing()
Returns the number of missing/new files found.
int GetNumberSuccessful()
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.
Downloading updates
The demo project contains a full example of implementing CWebUpdate
.
Updates
- 25th June 2005: First release. (I sense another imminent one.)