使用 MSI Automation API 32 和 64 位的 C++ 应用程序

标签 c++ com windows-installer


我正在尝试创建调用 Windows 安装程序自动化 api 的简单 C++ win32 控制台应用程序(在 vs2010 中)。但我到目前为止失败了。此方法会导致“Microsoft C++ 异常:_com_error at memory location”错误。
如何正确使用这个api?如何在只有一个.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;
}

关于使用 MSI Automation API 32 和 64 位的 C++ 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18850008/

相关文章:

com - 是否有列出从 DLL 导出的方法的工具?

c# - 发布WinForm程序更新

Wix 工具集 : Successively check conditions

python - 如何在python和C/C++中使用共享内存

c++ - 编写我自己的 C++ 编译器 .. 卡在变量上

c++ - CoCreateInstance 返回 E_INVALIDARG

wix - Windows Installer 错误 1316。安装程序查找错误的文件名

c++ - 如何使用模板创建编译时类字段?

c++ - 是否可以编写不接受任何类型的 C++ 模板?

c++ - boost-build 无法正确定位 .tli 文件并中止