首页 > 代码库 > C++ DLL 获取 MSI Property

C++ DLL 获取 MSI Property

VS2010 创建  C++, Win32 DLL工程C-TEST。

Stdafx.h中,在<windows.h>之后 添加引用。

 

#include <msi.h>
#include <msiquery.h>

 

C-TEST.cpp

// C-TEST.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <tchar.h>

UINT GetProperty(MSIHANDLE hInstall)
{
    TCHAR* szValueBuf = NULL;
    DWORD cchValueBuf = 0;
    UINT uiStat =  MsiGetProperty(hInstall, TEXT("ProductName"), TEXT(""), &cchValueBuf);
    //cchValueBuf now contains the size of the property‘s string, without null termination
    if (ERROR_MORE_DATA =http://www.mamicode.com/= uiStat)
    {
        ++cchValueBuf; // add 1 for null termination
        szValueBuf = new TCHAR[cchValueBuf];
        if (szValueBuf)
        {
            uiStat = MsiGetProperty(hInstall, TEXT("ProductName"), szValueBuf, &cchValueBuf);
        }
    }

    MessageBox(NULL, szValueBuf, _T("Im GetProperty"), MB_OK);

    if (ERROR_SUCCESS != uiStat)
    {
        if (szValueBuf != NULL) {
            delete[] szValueBuf;

        }
        return ERROR_INSTALL_FAILURE;
    }

    // custom action uses MyProperty
    // ...

    delete[] szValueBuf;

    return ERROR_SUCCESS;

}

UINT _stdcall SampleFunction2(LPCTSTR applicationName, MSIHANDLE hInstall)
{
    MessageBox(NULL, applicationName ,_T("I‘m Sample Function2‘s message"), MB_OK);
    return GetProperty(hInstall);
}
View Code

 

添加 C-TEST.def 文件

LIBRARY "C-TEST"
EXPORTS
SampleFunction2

 

编译,

1) 如果没有 #include <tchar.h>,会出现 error C3861: ‘_T‘: identifier not found

 

2)error LNK1120: 1 unresolved externals

解决方案:

工程右键 Property -> Configuration Properties -> Linker / Input / Additional Dependencies

添加  msi.lib

 

编译通过。