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);