C++ DLL 获取 MSI Property

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

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

 

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

 

C-TEST.cpp

 1 #include "stdafx.h"
 2 #include <tchar.h>
 3 
 4 UINT GetProperty(MSIHANDLE hInstall)
 5 {
 6     TCHAR* szValueBuf = NULL;
 7     DWORD cchValueBuf = 0;
 8     UINT uiStat =  MsiGetProperty(hInstall, TEXT("ProductName"), TEXT(""), &cchValueBuf);
 9     //cchValueBuf now contains the size of the property's string, without null termination
10     if (ERROR_MORE_DATA == uiStat)
11     {
12         ++cchValueBuf; // add 1 for null termination
13         szValueBuf = new TCHAR[cchValueBuf];
14         if (szValueBuf)
15         {
16             uiStat = MsiGetProperty(hInstall, TEXT("ProductName"), szValueBuf, &cchValueBuf);
17         }
18     }
19 
20     MessageBox(NULL, szValueBuf, _T("Im GetProperty"), MB_OK);
21 
22     if (ERROR_SUCCESS != uiStat)
23     {
24         if (szValueBuf != NULL) {
25             delete[] szValueBuf;
26 
27         }
28         return ERROR_INSTALL_FAILURE;
29     }
30 
31     delete[] szValueBuf;
32 
33     return ERROR_SUCCESS;
34 
35 }
36 
37 UINT _stdcall SampleFunction2(LPCTSTR productName, MSIHANDLE hInstall)
38 {
39     MessageBox(NULL, _T("Hello, welcome to C-TEST") ,_T("I'm Sample Function2's message"), MB_OK);
40     MessageBox(NULL, productName ,_T("I'm Sample Function2's break point"), MB_OK);
41     GetProperty(hInstall);
42     return 0;
43 }

 

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

 

编译通过。

posted @ 2014-05-13 16:17  細水長流  阅读(832)  评论(0编辑  收藏  举报