Click here to Skip to main content
Click here to Skip to main content

CWebUpdate 1.0 - An MFC Update class

, 25 Jun 2005
Rate this:
Please Sign up or sign in to vote.
An easy updating method with a small foot print.

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;

// Have CWebUpdate get this exe's path
updObj.SetLocalDirectory("", true);
    
// Set the URL for the update file and where downloads are stored
updObj.SetUpdateFileURL("http://yourwebsite.com/program/update.txt");
updObj.SetRemoteURL("http://yourwebsite.com/program");

// Check for updates
updObj.DoUpdateCheck();

// 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");
        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

  • bool DownloadMissing(int i)

    Returns true if the missing file at 'i' location was downloaded.

  • bool DownloadDifferent(int i)

    Returns true if the different/updated file at 'i' location was downloaded.

The demo project contains a full example of implementing CWebUpdate.

Updates

  • 25th June 2005: First release. (I sense another imminent one.)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

.rich.w
CEO
United States United States
No Biography provided

Comments and Discussions

 
QuestionSubfolders. PinmemberMember 789920710-Jul-11 10:39 
GeneralVersioning Pinmember.dan.g.27-Jun-05 15:00 
GeneralRe: Versioning PinsussAnonymous27-Jun-05 21:47 
GeneralRe: Versioning Pinmembermkcx28-Jun-05 23:28 
GeneralRe: Versioning Pinmember.rich.w29-Jun-05 5:14 
GeneralRe: Versioning PinmemberMember 44580906-Mar-08 1:16 
GeneralComments PinmemberRavi Bhavnani26-Jun-05 6:41 
GeneralRe: Comments Pinmember.rich.w26-Jun-05 6:58 
GeneralRe: Comments PinmemberRavi Bhavnani26-Jun-05 7:05 
GeneralRe: Comments Pinmember.rich.w26-Jun-05 7:06 
GeneralSetLocalDirectory() PinmemberRavi Bhavnani26-Jun-05 7:47 
GeneralRe: Comments Pinmemberdaman37119-Aug-08 10:11 
GeneralRe: Comments PinmemberRavi Bhavnani19-Aug-08 11:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 25 Jun 2005
Article Copyright 2005 by .rich.w
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid