【发布时间】: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