C++使用 MSI 自动化 API 32 和 64 位的应用程序
C++ Application That Uses MSI Automation API 32 and 64bit
我正在尝试创建调用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;
}
相关文章:
- 是否可以从控制台应用程序使用 C++/WinRT 创建窗口?
- 无法通过编程方式确定我的应用程序使用的TLS版本
- 从QT创建者运行的应用程序使用与单独运行时不同的库
- 在FreeBSD下,允许32位应用程序使用超过4GB的RAM
- 应用程序使用QMAP存储对象时停止响应
- 从桌面应用程序使用Windows 8 toast通知
- 当 C++11 应用程序使用非 C++11 库时返回值损坏
- Win32应用程序使用printf将输出写入控制台
- CI for Qt 应用程序:使用不同的 Qt 版本构建
- 带有API的WinRT应用程序,该应用程序使用共享的MFC dll
- C++应用程序使用TCL API启用程序包自动加载
- 如何强制应用程序使用内存而不是页面文件
- 如何在visualstudio2010中为android应用程序使用GUI
- 无法使用QtNetwork,因为应用程序使用不同的线程
- X由于应用程序(使用C++、Qt、OpenGL)而挂起
- 如何使用 Visual Basic 创建登录应用程序(使用 WebHttpRequest)
- 外部应用程序使用共享内存接收消息,但没有包含的数据
- 创建一个 c/c++ 应用程序(使用 Xlib)来渲染 SWF 文件
- 可视化设置一个c++应用程序使用最大CPU使用率,在代码中
- Qt5驱动的应用程序使用Qt4库并失败.如何避免这种情况