使用MSI自动化API 32和64位的C ++应用程序 [英] C++ Application That Uses MSI Automation API 32 and 64bit

查看:328
本文介绍了使用MSI自动化API 32和64位的C ++应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图创建简单的c ++ win32控制台应用程序(在vs2010),调用windows安装程序自动化api。但我到目前为止失败了。这种方法导致Microsoft C ++异常:_com_error在内存位置错误。

如何正确使用此API?如何使它在32位和64位系统上只有一个.exe文件正常工作?


非常感谢,

Marek


I am trying to create simple c++ win32 console app(in vs2010) that calls windows installer automation api. But I am failing so far. This approach causes the "Microsoft C++ exception: _com_error at memory location" error.
How to correctly use this api? How to make it work correctly on 32 and 64 bit system with only one .exe file?

Many thanks,
Marek

#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。

I finally found the solution. The correct way is include msi.lib in linker includes and use Msi.h from windows sdk.

#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自动化API 32和64位的C ++应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆