C++ Application That Uses MSI Automation API 32 and 64bit

人走茶凉 提交于 2019-12-12 02:37:06

问题



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;
}

回答1:


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;
}


来源:https://stackoverflow.com/questions/18850008/c-application-that-uses-msi-automation-api-32-and-64bit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!