ActivePatch Developer's Guide - Getting Package File Information

To obtain information about an individual file in a package, the function GetPackageFileInformation can be used. This can also be used to determine if a specific file is present in the package. For example:

// Determine if the file "example.dat" is present in the package

HPACKAGE hPackage;
PACKAGE_FILE_INFORMATION fileInfo;
LPCTSTR lpszPackageFile = _T("update.pkg");     // package file
LPCTSTR lpszPassword = _T("secret");            // package password
LPCTSTR lpszFileName = _T("example.dat");
BOOL bFileFound;

// Open the package for read-only access

hPackage = OpenPackage(lpszPackageFile,          // package file
                       lpszPassword,             // package password
                       PACKAGE_OPEN_READONLY,    // read-only access
                       NULL, 0);                 // callback function

if (hPackage == INVALID_PACKAGE_HANDLE)
{
    // The package could not be opened; use the GetLastError
    // function to determine the reason
    return;
}

// Initialize the fileInfo structure with the size of the
// structure, and call the GetPackageFileInformation function

fileInfo.cbStructure = sizeof(PACKAGE_FILE_INFORMATION);
bFileFound = GetPackageFileInformation(
                 hPackage,                         // package handle
                 PACKAGE_FILE_PLATFORM_DEFAULT,    // file platform
                 0,                                // file checksum
                 lpszFileName,                     // file name
                 &fileInfo);                       // file information

ClosePackage(hPackage);

if (bFileFound)
{
    // The file exists in the package
}

As with the previous example, the first step is to open the package, since we need a valid package handle in order to search for the file. The package is opened in read-only mode using the OpenPackage function, and the return value is checked to make sure that it was successful.

The next step is to initialize the fileInfo structure by setting the cbStructure member to the size of the structure. This is used internally by the function to make sure that a pointer to the correct structure is being passed, and to ensure backwards compatibility in future versions.

Next, the GetPackageFileInformation function is called. The first parameter is the handle to the package that was opened. The second parameter specifies the platform that the file has been targeted for. By specifying the value of PACKAGE_FILE_PLATFORM_DEFAULT, the function will return a file targeted for the current platform. The next parameter specifies a checksum. This can be useful for identifying files that are targeted for multiple platforms or multiple versions of a product, but in this case, a value of zero indicates that the file checksum is unknown and the function should return the file appropriate for the current platform. The next parameter is the actual name of the file to be searched for, and the last parameter is a pointer to the structure that will contain information about the file.

The package is then closed, and the return value is checked. If the file was found, the function will return a non-zero value. If the file could not be found, the function will return zero and additional error information can be obtained by calling the GetLastError function.