From c208b7368ed64264af72e1f6772b6f947485d645 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Tue, 20 Oct 2026 21:24:47 +0200 Subject: [PATCH] Fix: Format net-state addresses as narrow strings on the client The client is a MultiByte build, so the destination buffers are char[] and _T("%s:%u") expands to an ANSI format that expects a char pointer. The code nevertheless converted inet_ntoa() output to WCHAR* first, and the ANSI %s stopped at the zero byte that follows the first wide character. Every address therefore collapsed to its leading digit, so 192.168.0.55:443 was shown as 1 and 0.0.0.0:80 as 0 in the local and remote address columns of the network connections tab. Write the address and port straight into the char buffers with sprintf_s in all four enumeration paths (legacy and Vista APIs, TCP and UDP), and drop the now unused char2wchar_t helper. Co-Authored-By: deepseek-flash --- client/MachineManager.cpp | 49 +++++++++++---------------------------- client/MachineManager.h | 1 - 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/client/MachineManager.cpp b/client/MachineManager.cpp index b26d1c6..baa1d48 100644 --- a/client/MachineManager.cpp +++ b/client/MachineManager.cpp @@ -1323,7 +1323,6 @@ LPBYTE CMachineManager::getNetStateList() lpBuffer[0] = TOKEN_MACHINE_NETSTATE; DWORD dwOffset = 1; DWORD dwLength = 0; - WCHAR* temp; // 定义扩展函数指针 PFNAllocateAndGetTcpExTableFromStack pAllocateAndGetTcpExTableFromStack; PFNAllocateAndGetUdpExTableFromStack pAllocateAndGetUdpExTableFromStack; @@ -1419,14 +1418,12 @@ LPBYTE CMachineManager::getNetStateList() // 远程IP地址 inadRemote.s_addr = pTcpExTable->table[i].dwRemoteAddr; - temp = char2wchar_t(inet_ntoa(inadLocal)); - wsprintf(szLocalAddr, _T("%s:%u"), temp, - ntohs((unsigned short)(0x0000FFFF & pTcpExTable->table[i].dwLocalPort))); - SAFE_DELETE_AR(temp); - temp = char2wchar_t(inet_ntoa(inadRemote)); - wsprintf(szRemoteAddr, _T("%s:%u"), temp, - ntohs((unsigned short)(0x0000FFFF & dwRemotePort))); - SAFE_DELETE_AR(temp); + // 客户端是 MultiByte 工程,缓冲区是 char[]:这里必须用窄字符格式化, + // 不能先转成 WCHAR* 再交给 _T("%s")(ANSI 的 %s 只会读到首字符,地址显示成 0/1)。 + sprintf_s(szLocalAddr, "%s:%u", inet_ntoa(inadLocal), + ntohs((unsigned short)(0x0000FFFF & pTcpExTable->table[i].dwLocalPort))); + sprintf_s(szRemoteAddr, "%s:%u", inet_ntoa(inadRemote), + ntohs((unsigned short)(0x0000FFFF & dwRemotePort))); // 打印出此入口的信息 char strProcessName[100] = { 0 }; const char* strType = _T("[TCP]"); @@ -1461,10 +1458,8 @@ LPBYTE CMachineManager::getNetStateList() for (DWORD i = 0; i < pUdpExTable->dwNumEntries; ++i) { // 本地IP地址 inadLocal.s_addr = pUdpExTable->table[i].dwLocalAddr; - temp = char2wchar_t(inet_ntoa(inadLocal)); - wsprintf(szLocalAddr, _T("%s:%u"), temp, - ntohs((unsigned short)(0x0000FFFF & pUdpExTable->table[i].dwLocalPort))); - SAFE_DELETE_AR(temp); + sprintf_s(szLocalAddr, "%s:%u", inet_ntoa(inadLocal), + ntohs((unsigned short)(0x0000FFFF & pUdpExTable->table[i].dwLocalPort))); // 打印出此入口的信息 char strProcessName[100] = { 0 }; const char* strType = _T("[UDP]"); @@ -1577,14 +1572,10 @@ LPBYTE CMachineManager::getNetStateList() // 远程IP地址 inadRemote.s_addr = pTcpTable_Vista->table[i].dwRemoteAddr; - temp = char2wchar_t(inet_ntoa(inadLocal)); - wsprintf(szLocalAddr, _T("%s:%u"), temp, - ntohs((unsigned short)(0x0000FFFF & pTcpTable_Vista->table[i].dwLocalPort))); - SAFE_DELETE_AR(temp); - temp = char2wchar_t(inet_ntoa(inadRemote)); - wsprintf(szRemoteAddr, _T("%s:%u"), temp, - ntohs((unsigned short)(0x0000FFFF & dwRemotePort))); - SAFE_DELETE_AR(temp); + sprintf_s(szLocalAddr, "%s:%u", inet_ntoa(inadLocal), + ntohs((unsigned short)(0x0000FFFF & pTcpTable_Vista->table[i].dwLocalPort))); + sprintf_s(szRemoteAddr, "%s:%u", inet_ntoa(inadRemote), + ntohs((unsigned short)(0x0000FFFF & dwRemotePort))); // 打印出此入口的信息 char strProcessName[100] = { 0 }; @@ -1637,10 +1628,8 @@ LPBYTE CMachineManager::getNetStateList() for (DWORD i = 0; i < pUdpExTable->dwNumEntries; ++i) { // 本地IP地址 inadLocal.s_addr = pUdpExTable->table[i].dwLocalAddr; - temp = char2wchar_t(inet_ntoa(inadLocal)); - wsprintf(szLocalAddr, _T("%s:%u"), temp, - ntohs((unsigned short)(0x0000FFFF & pUdpExTable->table[i].dwLocalPort))); - SAFE_DELETE_AR(temp); + sprintf_s(szLocalAddr, "%s:%u", inet_ntoa(inadLocal), + ntohs((unsigned short)(0x0000FFFF & pUdpExTable->table[i].dwLocalPort))); // 打印出此入口的信息 char strProcessName[100] = { 0 }; @@ -1700,16 +1689,6 @@ char* CMachineManager::ProcessPidToName(HANDLE hProcessSnap, DWORD ProcessId, ch } -wchar_t* CMachineManager::char2wchar_t(char* cstr) -{ - int len = MultiByteToWideChar(CP_ACP, 0, cstr, -1, NULL, 0); - wchar_t* wstr = new wchar_t[len]; - memset(wstr, 0, len * sizeof(wchar_t)); - MultiByteToWideChar(CP_ACP, 0, cstr, -1, wstr, len); - return wstr; -} - - BOOL CMachineManager::EnablePrivilege(LPCTSTR lpPrivilegeName, BOOL bEnable) { HANDLE hToken; diff --git a/client/MachineManager.h b/client/MachineManager.h index 36673d2..801830d 100644 --- a/client/MachineManager.h +++ b/client/MachineManager.h @@ -35,7 +35,6 @@ private: bool Is64BitOS(); bool Is64BitPorcess(DWORD dwProcessID); char* ProcessPidToName(HANDLE hProcessSnap, DWORD ProcessId, char* ProcessName); - wchar_t* char2wchar_t(char* cstr); BOOL EnablePrivilege(LPCTSTR lpPrivilegeName, BOOL bEnable); void DeleteService(LPBYTE lpBuffer, UINT nSize); void MyControlService(LPBYTE lpBuffer, UINT nType);