Click here to Skip to main content
15,886,919 members
Articles / Programming Languages / C++
Article

Two Simple Lines for Self-Repairing Apps

Rate me:
Please Sign up or sign in to vote.
3.83/5 (9 votes)
16 Feb 2000 92.9K   41   12
Creating Self-Repairing Applications using Windows Installer

Introduction

Ever wondered how MS Office 2000 repairs itself if any files are corrupted or missing? Just two lines of code can add this feature to your app.

First off, you must create an installer program from Windows Installer (free for Visual Studio 6.0 or VC++ 6.0 customers). The self-repairing services are provided via the Windows Installer system. If you are not developing for Windows 2000 only, make sure to set the project settings so that the Setup.exe file will install Windows Installer on the system if it is not already present (Windows Installer comes with Windows 2000, but is not included with previous versions of Windows).

Once your program is installed, it can verify and reinstall needed components with the following two lines.

MsiSetInternalUI(INSTALLUILEVEL_NONE,NULL);
MsiReinstallProduct("{A5A0D1C0-DF1A-11D3-99DC-00A0CCFFFAA1}",
       REINSTALLMODE_FILEVERIFY | REINSTALLMODE_FILEMISSING 
       | REINSTALLMODE_FILEOLDERVERSION | REINSTALLMODE_REPAIR);

To compile, you will need to link your product with the installer library (msi.lib) and include the installer header msi.h.

Make sure to replace the product ID with the product ID that is in the settings dialog for your program. This is needed by Windows Installer. Additionally, the first line is not required. However, it will hide the GUI from the user (without it, a Windows installer dialog will appear). Other settings are:

// Authored user interface with wizards, progress, and errors.
NSTALLUILEVEL_FULL
// Authored user interface with wizard dialog boxes suppressed.
INSTALLUILEVEL_REDUCED
// Simple progress and error handling.
INSTALLUILEVEL_BASIC
// The installer chooses an appropriate user interface level.
INSTALLUILEVEL_DEFAULT
// The installation level is not changed.
INSTALLUILEVEL_NOCHANGE
// Completely silent installation.
INSTALLUILEVEL_NONE

For the reinstall function, possible options are:

// Repair any defects encountered.
REINSTALLMODE_REPAIR
// Reinstall only if the file is missing.
REINSTALLMODE_FILEMISSING
// Reinstall if the file is missing or is an older version.
REINSTALLMODE_FILEOLDERVERSION
// Reinstall if the file is missing or is an equal or older version.
REINSTALLMODE_FILEEQUALVERSION
// Reinstall if the file is missing or is not an exact version.
REINSTALLMODE_FILEEXACT
// Check sum executables, and reinstall if they are missing or corrupt.
REINSTALLMODE_FILEVERIFY
// Reinstall all the files regardless of version.
REINSTALLMODE_FILEREPLACE
// Ensure required user regular entries.
REINSTALLMODE_USERDATA
// Ensure required machine regular entries.
REINSTALLMODE_MACHINEDATA
// Validate shortcuts and progman items.
REINSTALLMODE_SHORTCUT

That's it!

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralTamper Aware and Self Healing Code Pin
Jeffrey Walton28-May-07 20:25
Jeffrey Walton28-May-07 20:25 
QuestionHow to disable the self-repair? Pin
Jo Fredrickson28-Oct-03 15:16
Jo Fredrickson28-Oct-03 15:16 
AnswerRe: How to disable the self-repair? Pin
Yogurt9-Mar-04 6:22
Yogurt9-Mar-04 6:22 
GeneralRe: How to disable the self-repair? Pin
matzy11-Nov-04 8:30
matzy11-Nov-04 8:30 
GeneralRe: How to disable the self-repair? Pin
Guerven10-Jun-07 23:16
Guerven10-Jun-07 23:16 
GeneralUseful Pin
Armen Hakobyan8-Jun-02 3:17
professionalArmen Hakobyan8-Jun-02 3:17 
GeneralUnclear Pin
26-Feb-01 10:20
suss26-Feb-01 10:20 
GeneralRe: Unclear Pin
l a u r e n26-Feb-01 10:46
l a u r e n26-Feb-01 10:46 
GeneralRe: Unclear Pin
27-Feb-01 4:12
suss27-Feb-01 4:12 
Generalunclear about implementation.... Pin
Jeff Douglas2-Feb-01 4:33
Jeff Douglas2-Feb-01 4:33 
QuestionHow to implement this code? Pin
Jeff Thompson10-Jan-01 6:57
Jeff Thompson10-Jan-01 6:57 
GeneralNote on Error Handling Pin
Jesse Ezell17-Feb-00 3:48
Jesse Ezell17-Feb-00 3:48 

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

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