大网数据广告活动
安装更新与部署

A Database and Patch Example

· 52CE 我爱测

An application can use the MsiOpenDatabase function to open a new or existing installation database (.msi file) or patch package (.msp file.) The application checks the return value of MsiOpenDatabase before using the database handle.

An application can use the MsiOpenDatabase function to open a new or existing installation database (.msi file) or patch package (.msp file.) The application checks the return value of MsiOpenDatabase before using the database handle.

The following examples use the PMSIHANDLE type variables defined in msi.h. It is recommended to use the PMSIHANDLE type because the installer closes PMSIHANDLE objects as they go out of scope, whereas your application must close MSIHANDLE objects by calling MsiCloseHandle. For more information see Use PMSIHANDLE instead of HANDLE section in the Windows Installer Best Practices.

The following example opens a database, sample.msi, for reading only. MsiOpenDatabase succeeds only if sample.msi exists in the c:\test directory. Upon success, the returned database handle can be used to query the data in the installation package using MsiDatabaseOpenView and MsiGetSummaryInformation.

PMSIHANDLE hDbReadOnly = 0;
UINT uiStatus1 = MsiOpenDatabase(TEXT("c:\\test\\sample.msi"), MSIDBOPEN_READONLY, &hDbReadOnly);
if (ERROR_SUCCESS != uiStatus1)
{
    // process error
    return uiStatus1;
}

The following example opens the database for reading and writing. If the application calls MsiDatabaseCommit, all changes made to the database are saved. If the application does not call MsiDatabaseCommit, no changes are made to the database.

PMSIHANDLE hDbTransact = 0;
UINT uiStatus2 = MsiOpenDatabase(TEXT("c:\\test\\example.msi"), MSIDBOPEN_TRANSACT, &hDbTransact);
if (ERROR_SUCCESS != uiStatus2)
{
    // process error
    return uiStatus2;
}

The following example takes an existing database, text.msi, and creates a new database, newtest.msi. Any changes that are made can be saved in the new database by calling MsiDatabaseCommit. The existing database specified in the szDatabasePath parameter is unchanged.

PMSIHANDLE hDbOutput = 0;
UINT uiStatus3 = MsiOpenDatabase(TEXT("c:\\test\\test.msi"), TEXT("c:\\test\\newtest.msi"), &hDbOutput);
if (ERROR_SUCCESS != uiStatus3)
{
    // process error
    return uiStatus3;
}

The following example opens a Windows Installer patch package (.msp file) for reading only. The returned patch handle can be used to determine the cabinets and transform substorages included in the patch package by queries on the _Streams and _Storages tables.

Windows Installer 2.0: Not supported. Beginning with Windows Installer 3.0, the application can query the MsiPatchSequence table present in a patch package that uses the new patch sequencing information.

PMSIHANDLE hDbPatch = 0;
LPCTSTR szPersistMode = MSIDBOPEN_READONLY + MSIDBOPEN_PATCHFILE;
UINT uiStatus4 = MsiOpenDatabase(TEXT("c:\\test\\sample.msp"), szPersistMode, &hDbPatch);
if (ERROR_SUCCESS != uiStatus4)
{
    // process error
    return uiStatus4;
}

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。