Question

I have the following application, to check installed programs in a system:

#include <iostream>
#include <Msi.h>
#include <Windows.h>

using namespace std;

void main()
{
    UINT ret;
    DWORD dwIndex = 0;
    DWORD dwContext = MSIINSTALLCONTEXT_ALL;
    char szInstalledProductCode[39] = {0};
    char szSid[128] = {0};
    const char* szUserSid = "s-1-1-0";
    DWORD cchSid;
    MSIINSTALLCONTEXT dwInstalledContext;

    do
    {
        memset(szInstalledProductCode, 0, sizeof(szInstalledProductCode));
        cchSid = sizeof(szSid)/sizeof(szSid[0]);

        ret = MsiEnumProductsEx(
            NULL,           // all the products in the context
            szUserSid,  // i.e.Everyone, all users in the system
            dwContext,
            dwIndex,
            szInstalledProductCode,
            &dwInstalledContext,
            szSid,
            &cchSid
        );

        if(ret == ERROR_SUCCESS)
        {
            char* name = MsiGetProductInfoEx (
                szInstalledProductCode,
                cchSid == 0 ? NULL : szSid,
                dwInstalledContext,
                INSTALLPROPERTY_INSTALLEDPRODUCTNAME
            );

            char* version = MsiGetProductInfoEx (
                szInstalledProductCode,
                cchSid == 0 ? NULL : szSid,
                dwInstalledContext,
                INSTALLPROPERTY_VERSIONSTRING
            );

            cout << name << endl;
            cout << "  - " << version << endl;

            dwIndex++;
        }

    } while(ret == ERROR_SUCCESS);
}

I am using Microsoft Visual C++ Express 2010. The application is MBCS. In studio, these four things are in red (error):

  • MSIINSTALLCONTEXT_ALL
  • MSIINSTALLCONTEXT
  • MsiEnumProductsEx
  • MsiGetProductInfoEx

I linked the Msi.lib (Project properties -> Linker -> Input -> Additional Dependencies). I am just trying to figure out how MsiEnumProductsEx function works. I know there are other questions around, but I just can't understand why it isn't working because I think that I have everything for the functions to be available, at least. Thanks!

Was it helpful?

Solution

The MSIINSTALLCONTEXT_ALL (and related identifiers) are defined in <msi.h> only if _WIN32_MSI >= 300. You have to tell the Windows SDK what the minimum OS version you're targeting is, by defining a few macros before installing any SDK headers (like <msi.h> or <windows.h>).

You do that according to this MSDN page.

Once you've defined a suitable minimum version (looks like Windows XP SP2 and up), then _WIN32_MSI will be set to an appropriate level, and you should get the symbols.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top