【问题标题】:C++ Application That Uses MSI Automation API 32 and 64bit使用 MSI 自动化 API 32 和 64 位的 C++ 应用程序
【发布时间】:2013-09-18 17:22:20
【问题描述】:


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

【问题讨论】:

  • 这是完全正常的,COM方法调用失败时会引发_com_error异常。在您捕获异常或停止使用#import 生成的智能指针类型之前,您和我们都不知道它为什么会失败。捕捉异常。
  • 那么我调用 COM 的方式是否正确?我添加了 catch (const _com_error ex) 块,我收到“com 错误标量删除析构函数”、“com 错误 vftable”。我希望有人可以提供简单的示例如何使用这个 msi 自动化 api。

标签: c++ com windows-installer


【解决方案1】:

我终于找到了解决办法。正确的方法是在链接器包含中包含 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;
}

【讨论】:

  • 抱歉,我差点把这个贴出来,但 C++ 并不是我真正的强项。我的理解是 Windows Installer Function 参考适用于 C/C++ 应用程序,而自动化接口适用于 VB6/ActiveScript 语言。我知道在 C# 世界中,我使用封装函数的互操作库,并且尽可能避免使用 COM 互操作。我一直很安静,因为 Michael Urman 是这个问题的专家,我希望他能参与进来。当我真的没有资格发表这个意见时,我不想发布“不要在 C++ 中使用 COM”。
  • 还是谢谢你,我终于能够为具有 msi 功能的 installshield 套件安装创建主要升级方案。我将从我的帖子community.flexerasoftware.com/… 链接这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 2020-11-25
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 2012-01-19
  • 1970-01-01
相关资源
最近更新 更多