C++使用 MSI 自动化 API 32 和 64 位的应用程序

C++ Application That Uses MSI Automation API 32 and 64bit

本文关键字:应用程序 使用 MSI 自动化 API C++      更新时间:2023-10-16


我正在尝试创建调用Windows安装程序自动化API的简单c ++ win32控制台应用程序(在vs2010中)。但到目前为止,我失败了。此方法会导致"Microsoft C++异常:在内存位置_com_error"错误。如何正确使用此接口?如何使其在只有一个.exe文件的 32 位和 64 位系统上正常工作?

非常感谢,
马立克

#include "stdafx.h"
#include <windows.h>
#include <atlstr.h>
#import "msi.dll"
using namespace WindowsInstaller;
_bstr_t GetInstalledProduct(InstallerPtr pInstaller,_bstr_t upgradeCode){
    StringListPtr installedProducts = pInstaller->GetRelatedProducts(upgradeCode); 
    return installedProducts->Count > 0 ? installedProducts->GetItem(0) : ""; 
}
int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize(NULL);
    InstallerPtr pInstaller("WindowsInstaller.Installer");
    _bstr_t upgradeCode("4C34BD16-CAD4-4059-B074-777793406C5F"); 
    _bstr_t installedProduct = GetInstalledProduct(pInstaller, upgradeCode); 
    StringListPtr features = pInstaller->GetFeatures(installedProduct); 
    ::CoUninitialize();
    return 0;
}

我终于找到了解决方案。正确的方法是在链接器中包含 msi.lib 并从 windows sdk 使用 Msi.h。

#include "stdafx.h"
#include <windows.h>
#include <Msi.h>
int _tmain(int argc, _TCHAR* argv[])
{
    wchar_t productCode[255]; 
    int result = MsiEnumRelatedProducts(L"{4C34BD16-CAD4-4059-A074-777493406C5F}", 0, 0, productCode); 
    wchar_t featureName[255]; 
    wchar_t featureParent[255]; 
    MsiEnumFeatures(productCode, 0, featureName, featureParent); 
    INSTALLSTATE featureState = MsiQueryFeatureState(productCode, L"FeatureName"); 
    return 0;
}