C++ DLL 获取 MSI Property

时间:2021-01-18 22:54:17

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

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

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

C-TEST.cpp

 #include "stdafx.h"
#include <tchar.h> UINT GetProperty(MSIHANDLE hInstall)
{
TCHAR* szValueBuf = NULL;
DWORD cchValueBuf = ;
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 == 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;
} delete[] szValueBuf; return ERROR_SUCCESS; } UINT _stdcall SampleFunction2(LPCTSTR productName, MSIHANDLE hInstall)
{
MessageBox(NULL, _T("Hello, welcome to C-TEST") ,_T("I'm Sample Function2's message"), MB_OK);
MessageBox(NULL, productName ,_T("I'm Sample Function2's break point"), MB_OK);
GetProperty(hInstall);
return ;
}

添加 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

C++ DLL 获取 MSI Property

编译通过。