【发布时间】:2015-02-25 01:57:17
【问题描述】:
我在 Visual Studio 2010 中有一个 C++ 应用程序,并且我有一个 Windows Installer(即安装项目)来安装它。我希望能够像这样调用安装程序:
Setup1.msi MYPROPERTY=MyValue
然后能够从我的自定义操作中的属性中提取值“MyValue”。 我试图通过遵循this tutorial(C++ 自定义操作)和this tutorial(将参数传递给自定义操作,但在 C# 中)结合一些 MSDN 搜索来获得此代码:
#define WINDOWS_LEAN_AND_MEAN
#include <Windows.h>
#include <msi.h>
#include <msiquery.h>
#include <stdio.h>
BOOL APIENTRY DllMain(HANDLE, DWORD, LPVOID) {
return TRUE;
}
UINT APIENTRY InstallCustomAction(MSIHANDLE install_handle) {
static const wchar_t* kPropertyName = L"MYPROPERTY";
//auto msi_handle = MsiGetActiveDatabase(install_handle);
DWORD n = 0;
//auto result = MsiGetProperty(msi_handle, kPropertyName, L"", &n);
auto result = MsiGetProperty(install_handle, kPropertyName, L"", &n);
wchar_t* value = nullptr;
if (result == ERROR_MORE_DATA) {
++n;
value = new wchar_t[n];
//result = MsiGetProperty(msi_handle, kPropertyName, value, &n);
result = MsiGetProperty(install_handle, kPropertyName, value, &n);
}
if (result == ERROR_SUCCESS) {
wchar_t buffer[128];
swprintf_s(buffer, L"n = %d, value = %s", n, value);
MessageBox(nullptr, buffer, L"CustomAction", MB_OK);
} else {
MessageBox(nullptr, L"Error reading property", L"Error", MB_OK);
}
delete value;
//MsiCloseHandle(msi_handle);
return ERROR_SUCCESS;
}
我在 IDE 方面完全遵循 C# 教程(我将 Entry Point 设置为 InstallCustomAction 和 Custom Action 数据设置为 /MYPROPERTY=[MYPROPERTY])自定义操作正确触发,但我没有得到参数。
使用原样的代码,我得到 n=0。如果我使用来自 MsiGetActiveDatabase 的 msi_handle,我会收到错误消息(即 MsiGetProperty 返回的不是 ErrorSuccess)。
如何从我的自定义操作中获取用户在命令行中传入的属性?
【问题讨论】:
标签: c++ windows-installer command-line-arguments custom-action