From 1492ef4bd29730e22ae7a612abf1efa97896c7f8 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Tue, 1 Apr 2025 03:57:38 +0800 Subject: [PATCH] fix: #64 Size of MSG is different on Win32 and Win64 --- client/ScreenManager.cpp | 19 ++++--- common/commands.h | 83 ++++++++++++++++++++++++++++++ server/2015Remote/ScreenSpyDlg.cpp | 12 ++--- server/2015Remote/ScreenSpyDlg.h | 2 +- 4 files changed, 102 insertions(+), 14 deletions(-) diff --git a/client/ScreenManager.cpp b/client/ScreenManager.cpp index 2dd835a..2169dfd 100644 --- a/client/ScreenManager.cpp +++ b/client/ScreenManager.cpp @@ -273,17 +273,24 @@ VOID CScreenManager::SendNextScreen(const char* szBuffer, ULONG ulNextSendLength VOID CScreenManager::ProcessCommand(LPBYTE szBuffer, ULONG ulLength) { - // 数据包不合法 - if (ulLength % sizeof(MSG) != 0) - return; + int msgSize = sizeof(MSG64); + if (ulLength % 28 == 0) // 32位控制端发过来的消息 + msgSize = 28; + else if (ulLength % 48 == 0) // 64位控制端发过来的消息 + msgSize = 48; + else return; // 数据包不合法 // 命令个数 - ULONG ulMsgCount = ulLength / sizeof(MSG); + ULONG ulMsgCount = ulLength / msgSize; // 处理多个命令 - for (int i = 0; i < ulMsgCount; ++i) + BYTE* ptr = szBuffer; + MSG32 msg32; + MSG64 msg64; + for (int i = 0; i < ulMsgCount; ++i, ptr += msgSize) { - MSG *Msg = (MSG *)(szBuffer + i * sizeof(MSG)); + MSG64* Msg = msgSize == 48 ? (MSG64*)ptr : + (MSG64*)msg64.Create(msg32.Create(ptr, msgSize)); switch (Msg->message) { case WM_LBUTTONDOWN: diff --git a/common/commands.h b/common/commands.h index 7693a14..53a9111 100644 --- a/common/commands.h +++ b/common/commands.h @@ -241,3 +241,86 @@ inline bool WriteBitmap(LPBITMAPINFO bmpInfo, const void* bmpData, const std::st } return false; } + +#ifdef _WIN32 +#ifdef _WINDOWS +#include +#else +#define WIN32_LEAN_AND_MEAN +#include +#endif +class MSG32 { // 自定义控制消息(32位) +public: + uint32_t hwnd; + uint32_t message; + uint32_t wParam; + uint32_t lParam; + uint32_t time; + POINT pt; + + MSG32(const void* buffer, int size) { + if (size == sizeof(MSG32)) { + memcpy(this, buffer, sizeof(MSG32)); + } + else { + memset(this, 0, sizeof(MSG32)); + } + } + + MSG32() { + memset(this, 0, sizeof(MSG32)); + } + + MSG32* Create(const void* buffer, int size) { + if (size == sizeof(MSG32)) { + memcpy(this, buffer, sizeof(MSG32)); + } + else { + memset(this, 0, sizeof(MSG32)); + } + return this; + } +}; + +// Windows 自定义的消息MSG在32位和64位系统下大小不同,导致跨平台架构远程控制异常 +// 需要使用自定义的消息(统一采用64位windows 的MSG定义) +class MSG64 { // 自定义控制消息(64位) +public: + uint64_t hwnd; + uint64_t message; + uint64_t wParam; + uint64_t lParam; + uint64_t time; + POINT pt; + + MSG64(const MSG& msg) :hwnd((uint64_t)msg.hwnd), message(msg.message), wParam(msg.wParam), + lParam(msg.lParam), time(msg.time), pt(msg.pt) {} + + MSG64(const MSG32& msg) :hwnd((uint64_t)msg.hwnd), message(msg.message), wParam(msg.wParam), + lParam(msg.lParam), time(msg.time), pt(msg.pt) {} + + MSG64(const void* buffer, int size) { + if (size == sizeof(MSG64)) { + memcpy(this, buffer, sizeof(MSG64)); + } + else { + memset(this, 0, sizeof(MSG64)); + } + } + + MSG64() { + memset(this, 0, sizeof(MSG64)); + } + + MSG64* Create(const MSG32* msg32) { + hwnd = msg32->hwnd; + message = msg32->message; + wParam = msg32->wParam; + lParam = msg32->lParam; + time = msg32->time; + pt = msg32->pt; + return this; + } +}; + +#endif diff --git a/server/2015Remote/ScreenSpyDlg.cpp b/server/2015Remote/ScreenSpyDlg.cpp index 9d6b7fe..30632ac 100644 --- a/server/2015Remote/ScreenSpyDlg.cpp +++ b/server/2015Remote/ScreenSpyDlg.cpp @@ -535,8 +535,7 @@ BOOL CScreenSpyDlg::PreTranslateMessage(MSG* pMsg) case WM_MBUTTONUP: case WM_MOUSEWHEEL: { - MSG Msg; - memcpy(&Msg, pMsg, sizeof(MSG)); + MSG64 Msg(*pMsg); Msg.lParam = MAKEDWORD(HIWORD(pMsg->lParam) + m_ulVScrollPos, LOWORD(pMsg->lParam) + m_ulHScrollPos); Msg.pt.x += m_ulHScrollPos; Msg.pt.y += m_ulVScrollPos; @@ -551,8 +550,7 @@ BOOL CScreenSpyDlg::PreTranslateMessage(MSG* pMsg) return TRUE; if (pMsg->wParam != VK_LWIN && pMsg->wParam != VK_RWIN) { - MSG Msg; - memcpy(&Msg, pMsg, sizeof(MSG)); + MSG64 Msg(*pMsg); Msg.lParam = MAKEDWORD(HIWORD(pMsg->lParam) + m_ulVScrollPos, LOWORD(pMsg->lParam) + m_ulHScrollPos); Msg.pt.x += m_ulHScrollPos; Msg.pt.y += m_ulVScrollPos; @@ -567,15 +565,15 @@ BOOL CScreenSpyDlg::PreTranslateMessage(MSG* pMsg) } -VOID CScreenSpyDlg::SendCommand(MSG* Msg) +VOID CScreenSpyDlg::SendCommand(const MSG64* Msg) { if (!m_bIsCtrl) return; - const int length = sizeof(MSG) + 1; + const int length = sizeof(MSG64) + 1; BYTE szData[length + 3]; szData[0] = COMMAND_SCREEN_CONTROL; - memcpy(szData + 1, Msg, sizeof(MSG)); + memcpy(szData + 1, Msg, sizeof(MSG64)); szData[length] = 0; m_iocpServer->OnClientPreSending(m_ContextObject, szData, length); } diff --git a/server/2015Remote/ScreenSpyDlg.h b/server/2015Remote/ScreenSpyDlg.h index 1443d2c..db151a5 100644 --- a/server/2015Remote/ScreenSpyDlg.h +++ b/server/2015Remote/ScreenSpyDlg.h @@ -59,7 +59,7 @@ public: CString m_strClientIP; BOOL m_bIsTraceCursor; CCursorInfo m_CursorInfo; //自定义的一个系统的光标类 - VOID SendCommand(MSG* Msg); + VOID SendCommand(const MSG64* Msg); VOID UpdateServerClipboard(char *szBuffer,ULONG ulLength); VOID SendServerClipboard(void);