ActivePatch Developer's Guide - Applying Patch Files

The process of applying the patch file uses the contents of a previously created patch to modify the original version of a file to produce the modified version. Using the same example that created the patch file, we'll copy the house1.mdb database file to a file named house.mdb. This is a copy of the original version of the database, and will be modified in place by the ApplyPatchFile function.

// Apply a patch file, creating the updated version of
// a file from the original version

LPCTSTR lpszPatchFile = _T("house.pat");   // patch file
LPCTSTR lpszPassword = _T("secret");       // patch file password
LPCTSTR lpszFileName = _T("house.mdb");    // original version of database
DWORD dwOptions;
BOOL bResult;

dwOptions = PATCH_OPTION_BACKUP_FILE |
PATCH_OPTION_IGNORE_FILETIME;

bResult = ApplyPatchFile(lpszPatchFile,    // patch file name
                         dwOptions,        // patch options
                         0,                // reserved
                         lpszPassword,     // patch password
                         lpszFileName,     // original file
                         NULL,             // updated file
                         NULL,             // callback function
                         0);               // callback parameter

Many of the parameters are similar to those used with the CreatePatchFile function, however there are some notable differences. In this case, we do specify some options, and no file name is specified for the updated file.

The first option that is specified, PATCH_OPTION_BACKUP_FILE tells the function that you wish to create a backup copy of the original file when the patch is applied. This allows you to easily undo the changes made by the patch and revert to the previous version of the file. Backup copies of the file retain the same base file name, and the extension is replaced with a sequence number. In this example, the backup copy of the house.mdb file would be called house.000. The second option specifies that the time that the file was last modified should be ignored when determining if the patch can be applied safely to the file. By default, the ApplyPatchFile function has a very stringent set of criteria to determine that the original version of the file has not been changed in any way; the PATCH_OPTION_IGNORE_FILETIME option relaxes one of these criteria. Note that the function still performs checks on other attributes such as the size and checksum of the original file. The option to ignore any differences in the file timestamp is particularly useful when applying patches that are distributed over the Internet.

Because the output (updated) file name parameter was specified as NULL, this tells the function that you wish to overwrite the original version of the file with the updated version. In this example, before the function is called, the file house.mdb is identical to the original version of the database, house1.mdb. After the function successfully returns, house.mdb will be identical to house2.mdb, the updated version of the database.

If the ApplyPatchFile function succeeds, it will return a non-zero value. If the function fails, it will return a value of zero and the GetLastError function can be used to determine the cause of the error. The same code used in the CreatePatchFile example can be used to obtain a descriptive error string.