[英]Wix: Add to Listbox from C++ Custom Action
您好我正在尝试检查用户计算机上的COM端口,然后通过c ++中的自定义操作将它们插入到列表框中。 虽然它没有显示信息,但当我调试它时,它说的是“功能无法执行”这是我的列表框的Wix代码:
<Control Id="ListBoxID" Type="ListBox" Property="COMPORT" Width="80" Height="40" X="80" Y="165" Indirect="no">
<ListBox Property="COMPORT">
</ListBox>
<Condition Action="hide">(DEVICETYPE = "1")</Condition>
<Condition Action="show">(DEVICETYPE = "2")</Condition>
<Condition Action="show">(DEVICETYPE = "3")</Condition>
<Condition Action="hide">(DEVICETYPE = "4")</Condition>
</Control>
这是我的自定义操作:
extern "C" UINT __stdcall GetDatascanPort(MSIHANDLE hInstall)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
HKEY keyHandle;
DWORD i,openStatus,cb_value_buffer,cb_buffer,dwType;
char value_buffer[100],buffer[10];
MSIHANDLE hTable = NULL;
MSIHANDLE hColumns = NULL;
MSIDBERROR insertError = MSIDBERROR_NOERROR;
hr = WcaInitialize(hInstall, "GetDatascanPort");
ExitOnFailure(hr, "Failed to initialize");
WcaLog(LOGMSG_STANDARD, "Initialized.");
if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
"HARDWARE\\DEVICEMAP\\SERIALCOMM",
0,
"",
REG_OPTION_NON_VOLATILE,
KEY_QUERY_VALUE,
default_sa(),
&keyHandle,
&openStatus ) == ERROR_SUCCESS )
{
for (i=0;;i++)
{
cb_value_buffer = sizeof(value_buffer);
cb_buffer = sizeof(buffer);
if (RegEnumValue(keyHandle,
i,
value_buffer,
&cb_value_buffer,
NULL,
&dwType,
(unsigned char *) buffer,
&cb_buffer) != ERROR_SUCCESS)
break;
if (dwType != REG_SZ || strlen(buffer) > 6)
continue;
hr = WcaAddTempRecord(&hTable, &hColumns, L"ListBox",&insertError, 0, 4, L"COMPORT", 1, 0, buffer);
ExitOnFailure(hr, "failed to set COMPORT");
}
RegCloseKey(keyHandle);
if (hTable)
MsiCloseHandle(hTable);
if (hColumns)
MsiCloseHandle(hColumns);
return WcaFinalize(hr);
}
LExit:
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}
有人可以帮我吗? 谢谢
编辑:我有它更新我的列表框但它不清晰,奇怪的符号。 我将我的char更改为CString,这似乎与错误地显示它有关。
CString ComPort;
ComPort = buffer;
hr = WcaAddTempRecord(&hTable, &hColumns, L"ListBox",&insertError, 0, 4, L"COMPORT", 1, ComPort, ComPort);
ExitOnFailure(hr, "failed to set COMPORT");
目标计算机也可能有串口,在for循环中使用我的函数列出它们的最佳方法是什么?
谢谢
我认为你传递的函数太多了。 因为WcaAddTempRecord签名使用变量参数列表,所以它不会验证类型。
代替:
hr = WcaAddTempRecord(&hTable, &hColumns, L"ListBox",&insertError, 0, 4, L"COMPORT", 1, 0, buffer);
尝试:
hr = WcaAddTempRecord(&hTable, &hColumns, L"ListBox",&insertError, 0, 4, L"COMPORT", nIndex, buffer);
其中nIndex
是您要插入的项目的基于1的索引。 另外我不确定你是否可以使用char
,你可能需要使用wchar_t
。
尝试快速测试使用wchar_t缓冲区而不是char缓冲区,看看是否有效。 删除所有其他代码,只需添加一个条目:
hr = WcaAddTempRecord(&hTable, &hColumns, L"ListBox",&insertError, 0, 4, L"COMPORT", 1, L"TestPort");
搞定了。 这是我修改的代码,它可以帮助其他人:
MultiByteToWideChar(CP_ACP,0,buffer,-1,ComPort,strlen(buffer));
ComPort[strlen(buffer)]=0;
hr = WcaAddTempRecord(&hTable, &hColumns, L"ListBox",&insertError, 0, 4, L"COMPORT", value++, ComPort, ComPort);
ExitOnFailure(hr, "failed to set COMPORT");
感谢@snowdude,我知道我必须将char转换为Wide Char。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.