fix(server): show UTF-8 process names correctly in the connections dialog

The connections dialog decoded procName with CA2T(x, CP_UTF8). In this MBCS
build CA2T expands to CA2A, whose nCodePage argument ATL explicitly ignores
(atlconv.h: "(void)nCodePage") - the call just memcpy's the bytes. So the
UTF-8 process names sent by the client were copied verbatim into a CString and
rendered as GBK: Chinese names turned into mojibake while pure-ASCII names
looked fine, since UTF-8 and GBK coincide there.

Display via the established pattern instead (same as the client list and the
process manager): convert UTF-8 to UTF-16 with MultiByteToWideChar and write
the item text with LVM_SETITEMTEXTW, bypassing the ANSI code page entirely.
This also keeps Chinese names readable on servers running a non-Chinese ACP.

The sort key keeps the raw UTF-8 bytes: byte-wise comparison of UTF-8 equals
code-point order, and no characters can be lost in conversion.
This commit is contained in:
yuanyuanxiang committed 2026-11-02 19:39:17 +01:00
1 parent d3c165de79
commit 8c0351b45b
1 file changed
+30 -4
+30 -4
View File
@@ -93,6 +93,31 @@ static CString AddrSortKey(uint8_t af, const uint8_t* addr)
return key;
}
// 进程名是客户端按 UTF-8 发来的(client/ConnectionsManager.cpp ResolveProcName 用
// WideCharToMultiByte(CP_UTF8) 编码)。显示必须走宽字符直写:本工程是 MBCS 构建,
// CA2T(x, CP_UTF8) 展开成 CA2A,而 CA2A 的 nCodePage 参数被 ATL 显式忽略
// (atlconv.h 里就是一句 "(void)nCodePage"),等于把 UTF-8 字节原样拷进 CString
// 再按 GBK 渲染——中文名全是乱码,纯 ASCII 名恰好两个编码同形所以看不出来。
// 与客户端列表(CClientListDlg)/进程管理同法:转 UTF-16 后 LVM_SETITEMTEXTW 直写,
// 绕开 ANSI 代码页,服务端即使在非中文 ACP 上也能正常显示。
static void SetUtf8ItemText(CListCtrl& list, int iItem, int iSubItem, const char* sz)
{
std::wstring w;
if (sz && sz[0]) {
int wlen = MultiByteToWideChar(CP_UTF8, 0, sz, -1, NULL, 0);
if (wlen > 1) {
w.resize(wlen - 1);
MultiByteToWideChar(CP_UTF8, 0, sz, -1, &w[0], wlen);
}
}
LVITEMW lv = {};
lv.mask = LVIF_TEXT;
lv.iItem = iItem;
lv.iSubItem = iSubItem;
lv.pszText = w.empty() ? const_cast<LPWSTR>(L"") : &w[0];
::SendMessageW(list.GetSafeHwnd(), LVM_SETITEMTEXTW, (WPARAM)iItem, (LPARAM)&lv);
}
BOOL CConnectionsDlg::OnInitDialog()
{
__super::OnInitDialog();
@@ -144,11 +169,10 @@ int CConnectionsDlg::ShowConnectionList(void)
CString strRemoteAddr = FormatAddr(info->af, info->remoteAddr);
CString strState = (info->proto == 1) ? TcpStateName(info->state) : _T("-");
CString strLocalPort, strRemotePort, strPid, strProcName;
CString strLocalPort, strRemotePort, strPid;
strLocalPort.Format(_T("%u"), info->localPort);
strRemotePort.Format(_T("%u"), info->remotePort);
strPid.Format(_T("%u"), info->pid);
strProcName = CA2T(info->procName, CP_UTF8);
auto* data = new ConnItemData;
data->info = *info;
@@ -159,7 +183,9 @@ int CConnectionsDlg::ShowConnectionList(void)
data->key[4].Format(_T("%05u"), info->remotePort);
data->key[5].Format(_T("%03u"), info->state);
data->key[6].Format(_T("%010u"), info->pid);
data->key[7] = strProcName;
// 排序键存原始 UTF-8 字节:UTF-8 的字节序比较等价于码点序比较,比转成
// 任何 ANSI 代码页再比都稳,也不会因转换丢字。
data->key[7] = info->procName;
m_ControlList.InsertItem(i, strProto);
m_ControlList.SetItemText(i, 1, strLocalAddr);
@@ -168,7 +194,7 @@ int CConnectionsDlg::ShowConnectionList(void)
m_ControlList.SetItemText(i, 4, strRemotePort);
m_ControlList.SetItemText(i, 5, strState);
m_ControlList.SetItemText(i, 6, strPid);
m_ControlList.SetItemText(i, 7, strProcName);
SetUtf8ItemText(m_ControlList, i, 7, info->procName);
m_ControlList.SetItemData(i, (DWORD_PTR)data);
}