From 448df31cd901f5490772977d1cf35d24fad47602 Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Sun, 25 Feb 2024 16:45:33 +0800 Subject: [PATCH] update msbuild_win7.yml --- .github/workflows/msbuild_win7.yml | 5 +++ CGAssistant/psworker.cpp | 60 ++++++++++++++++++------------ CGAssistant/psworker.h | 4 +- cgahook/main.cpp | 14 ++++--- cgahook/server.cpp | 9 ++--- 5 files changed, 55 insertions(+), 37 deletions(-) diff --git a/.github/workflows/msbuild_win7.yml b/.github/workflows/msbuild_win7.yml index 8261bf9..04d674c 100644 --- a/.github/workflows/msbuild_win7.yml +++ b/.github/workflows/msbuild_win7.yml @@ -118,6 +118,11 @@ jobs: node-version: '14.16' architecture: 'x86' + - name: Set up Python 2.7 + uses: actions/setup-python@v2 + with: + python-version: '2.7' + - name: Install node-gyp working-directory: ${{env.GITHUB_WORKSPACE}} run: npm install -g node-gyp diff --git a/CGAssistant/psworker.cpp b/CGAssistant/psworker.cpp index 1ef1dcd..7762be9 100644 --- a/CGAssistant/psworker.cpp +++ b/CGAssistant/psworker.cpp @@ -22,10 +22,13 @@ CProcessWorker::CProcessWorker(QObject *parent) : QObject(parent) qRegisterMetaType("CProcessItemList"); QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(OnQueueQueryProcess())); timer->start(500); - connect(timer, SIGNAL(timeout()), this, SLOT(OnCheckFreezeProcess())); - timer->start(1000); + + QTimer *timer2 = new QTimer(this); + connect(timer2, SIGNAL(timeout()), this, SLOT(OnCheckFreezeProcess())); + timer2->start(1000); m_AttachMutex = NULL; m_AttachHwnd = 0; @@ -42,11 +45,12 @@ quint32 CProcessWorker::GetAttachedHwnd() return (quint32)m_AttachHwnd; } -bool CProcessWorker::IsProcessAttached(quint32 ProcessId) +bool CProcessWorker::IsProcessAttached(quint32 ProcessId, quint32 ThreadId) { bool bAttached = false; - WCHAR szMutex[32]; - wsprintfW(szMutex, L"CGAAttachMutex_%d", ProcessId); + + WCHAR szMutex[64]; + wsprintfW(szMutex, L"CGAAttachMutex_%d_%d", ProcessId, ThreadId); HANDLE hAttachMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, szMutex); if(hAttachMutex) { @@ -97,12 +101,12 @@ bool CProcessWorker::InjectByMsgHook(quint32 ThreadId, quint32 hWnd, QString &dl return (errorCode == 0) ? true : false; } -bool CProcessWorker::ReadSharedData(quint32 ProcessId, int &port, quint32 &hWnd) +bool CProcessWorker::ReadSharedData(quint32 ProcessId, quint32 ThreadId, int &port, quint32 &hWnd) { bool bSuccess = false; WCHAR szLockName[32]; - wsprintfW(szLockName, L"CGASharedDataLock_%d", ProcessId); + wsprintfW(szLockName, L"CGASharedDataLock_%d_%d", ProcessId, ThreadId); HANDLE hDataLock = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, szLockName); if(hDataLock) @@ -110,7 +114,7 @@ bool CProcessWorker::ReadSharedData(quint32 ProcessId, int &port, quint32 &hWnd) if(WAIT_OBJECT_0 == WaitForSingleObject(hDataLock, 500)) { WCHAR szMappingName[32]; - wsprintfW(szMappingName, L"CGASharedData_%d", ProcessId); + wsprintfW(szMappingName, L"CGASharedData_%d_%d", ProcessId, ThreadId); HANDLE hFileMapping = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY, 0, sizeof(CGA::CGAShare_t), szMappingName); if (hFileMapping) { @@ -122,6 +126,7 @@ bool CProcessWorker::ReadSharedData(quint32 ProcessId, int &port, quint32 &hWnd) port = data->Port; hWnd = (quint32)data->hWnd; bSuccess = true; + UnmapViewOfFile(pViewOfFile); } CloseHandle(hFileMapping); @@ -140,7 +145,7 @@ void CProcessWorker::OnRetryAttachProcess() int port = 0; quint32 hwnd = 0; - if(!ReadSharedData(timer->m_ProcessId, port, hwnd)) + if(!ReadSharedData(timer->m_ProcessId, timer->m_ThreadId, port, hwnd)) { if(timer->m_retry > 10) { @@ -186,6 +191,7 @@ void CProcessWorker::ConnectToServer(quint32 ProcessId, quint32 ThreadId, int po void CProcessWorker::Disconnect() { g_CGAInterface->Disconnect(); + if(m_AttachMutex != NULL) { CloseHandle(m_AttachMutex); @@ -200,22 +206,22 @@ void CProcessWorker::OnAutoAttachProcess(quint32 ProcessId, quint32 ThreadId) m_AutoAttachPID = ProcessId; m_AutoAttachTID = ThreadId; - qDebug("OnAutoAttachProcess %d %d", ProcessId, ThreadId); + // qDebug("OnAutoAttachProcess %d %d", ProcessId, ThreadId); } void CProcessWorker::OnQueueAttachProcess(quint32 ProcessId, quint32 ThreadId, quint32 hWnd, QString dllPath) { - qDebug("OnQueueAttachProcess %d %d", ProcessId, ThreadId); + //qDebug("OnQueueAttachProcess %d %d", ProcessId, ThreadId); int port = 0; quint32 hwnd = 0; - if(!ReadSharedData(ProcessId, port, hwnd)) + if(!ReadSharedData(ProcessId, ThreadId, port, hwnd)) { int errorCode = -1; QString errorString; if(!InjectByMsgHook(ThreadId, hWnd, dllPath, errorCode, errorString)) { - qDebug("OnQueueAttachProcess %d %d, failed to InjectByMsgHook", ProcessId, ThreadId); + //qDebug("OnQueueAttachProcess %d %d, failed to InjectByMsgHook", ProcessId, ThreadId); NotifyAttachProcessFailed(ProcessId, ThreadId, errorCode, errorString); return; } @@ -225,7 +231,7 @@ void CProcessWorker::OnQueueAttachProcess(quint32 ProcessId, quint32 ThreadId, q } else { - qDebug("OnQueueAttachProcess %d %d, failed to ReadSharedData", ProcessId, ThreadId); + //qDebug("OnQueueAttachProcess %d %d, failed to ReadSharedData", ProcessId, ThreadId); //Already attached to game Disconnect(); @@ -235,16 +241,18 @@ void CProcessWorker::OnQueueAttachProcess(quint32 ProcessId, quint32 ThreadId, q bool CProcessWorker::CreateAttachMutex(quint32 ProcessId, quint32 ThreadId) { - WCHAR szMutex[32]; - wsprintfW(szMutex, L"CGAAttachMutex_%d", ProcessId); - auto AttachMutex = CreateMutexW(NULL, TRUE, szMutex); - if(AttachMutex == NULL) + WCHAR szMutex[64]; + wsprintfW(szMutex, L"CGAAttachMutex_%d_%d", ProcessId, ThreadId); + + auto hAttachMutex = CreateMutexW(NULL, TRUE, szMutex); + + if(hAttachMutex == NULL) { NotifyAttachProcessFailed(ProcessId, ThreadId, -4, tr("Game already attached by another instance of CGAssistant.")); return false; } - m_AttachMutex = AttachMutex; + m_AttachMutex = hAttachMutex; return true; } @@ -255,22 +263,24 @@ void CProcessWorker::OnQueueQueryProcess() const wchar_t szFindGameClass[] = { 39764, 21147, 23453, 36125, 0 }; HWND hWnd = NULL; - DWORD pid, tid; + DWORD pid = 0, tid = 0; WCHAR szText[256]; + DWORD dwCurrentPID = GetCurrentProcessId(); while ((hWnd = FindWindowExW(NULL, hWnd, szFindGameClass, NULL)) != NULL) { - if((tid = GetWindowThreadProcessId(hWnd, (LPDWORD)&pid)) != 0 && pid != GetCurrentProcessId()) + if((tid = GetWindowThreadProcessId(hWnd, (LPDWORD)&pid)) != 0 && pid != dwCurrentPID) { if(GetWindowTextW(hWnd, szText, 256)) { - bool attached = IsProcessAttached(pid); + bool attached = IsProcessAttached(pid, tid); + CProcessItemPtr item(new CProcessItem((quint32)pid, (quint32)tid, (quint32)hWnd, szText, attached)); list.append(item); if(!attached && m_AutoAttachPID == pid && m_AutoAttachTID == tid){ - qDebug("OnQueueAttachProcess %d %d", (quint32)pid, (quint32)tid); + //qDebug("OnQueueAttachProcess %d %d", (quint32)pid, (quint32)tid); OnQueueAttachProcess( (quint32)pid, (quint32)tid, (quint32)hWnd, QString("cgahook.dll") ); @@ -286,7 +296,9 @@ void CProcessWorker::OnQueueQueryProcess() if(ReadSharedData(pid, port, hwnd) && g_CGAInterface->IsConnected() && g_CGAInterface->GetPort() == port) { if(hwnd != GetAttachedHwnd()){ - qDebug("Should not connect to port %d, disconnect", port); + + //qDebug("Should not connect to port %d, disconnect", port); + g_CGAInterface->Disconnect(); } } diff --git a/CGAssistant/psworker.h b/CGAssistant/psworker.h index bd2cd70..b463036 100644 --- a/CGAssistant/psworker.h +++ b/CGAssistant/psworker.h @@ -59,9 +59,9 @@ private slots: void NotifyQueryProcess(CProcessItemList list); //void NotifyServerShutdown(int port); private: - bool IsProcessAttached(quint32 ProcessId); + bool IsProcessAttached(quint32 ProcessId, quint32 ThreadId); bool InjectByMsgHook(quint32 ThreadId, quint32 hWnd, QString &szDllPath, int &errorCode, QString &errorString); - bool ReadSharedData(quint32 ProcessId, int &port, quint32 &hWnd); + bool ReadSharedData(quint32 ProcessId, quint32 ThreadId, int &port, quint32 &hWnd); void ConnectToServer(quint32 ProcessId, quint32 ThreadId, int port, quint32 hWnd); bool CreateAttachMutex(quint32 ProcessId, quint32 ThreadId); void Disconnect(); diff --git a/cgahook/main.cpp b/cgahook/main.cpp index 8e2fd8c..10cd954 100644 --- a/cgahook/main.cpp +++ b/cgahook/main.cpp @@ -10,6 +10,7 @@ extern CGA::CGAService g_CGAService; int g_MainPort = 0; HWND g_MainHwnd = NULL; +ULONG g_MainProcessId = 0; ULONG g_MainThreadId = 0; WNDPROC g_OldProc = NULL; HANDLE g_hQuitEvent = NULL; @@ -351,11 +352,12 @@ LRESULT CALLBACK NewWndProcPOLCN(HWND hWnd, UINT message, WPARAM wParam, LPARAM return CallWindowProcA(g_OldProc, hWnd, message, wParam, lParam); } -void InitializeHooks(int ThreadId, HWND hWnd, CGA::game_type type) +void InitializeHooks(ULONG ProcessId, ULONG ThreadId, HWND hWnd, CGA::game_type type) { if (g_MainThreadId && g_MainThreadId != ThreadId) return; + g_MainProcessId = ProcessId; g_MainThreadId = ThreadId; g_MainHwnd = hWnd; @@ -387,19 +389,19 @@ extern "C" LPCWSTR pModuleName = ExtractFileName(szModulePath); if (!_wcsicmp(pModuleName, L"cg_se_3000.exe") && !strcmp(szClass, "ħÁ¦±¦±´")) { - //InitializeHooks(GetCurrentThreadId(), pMsg->hwnd, CGA::cg_se_3000); + //InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), pMsg->hwnd, CGA::cg_se_3000); } else if (!_wcsicmp(pModuleName, L"cg_item_6000.exe") && !strcmp(szClass, "ħÁ¦±¦±´")) { - InitializeHooks(GetCurrentThreadId(), pMsg->hwnd, CGA::cg_item_6000); + InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), pMsg->hwnd, CGA::cg_item_6000); } else if (!_wcsicmp(pModuleName, L"cg_se_6000.exe") && !strcmp(szClass, "ħÁ¦±¦±´")) { - //InitializeHooks(GetCurrentThreadId(), pMsg->hwnd, CGA::cg_se_6000); + //InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), pMsg->hwnd, CGA::cg_se_6000); } else if (!_wcsicmp(pModuleName, L"POLCN_Launcher.exe")) { - InitializeHooks(GetCurrentThreadId(), pMsg->hwnd, CGA::polcn); + InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), pMsg->hwnd, CGA::polcn); } } } @@ -467,7 +469,7 @@ int WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) return TRUE; } - InitializeHooks(GetCurrentThreadId(), NULL, CGA::polcn); + InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), NULL, CGA::polcn); } else if (!_wcsicmp(pModuleName, L"cg_se_6000.exe"))//Patch se_6000 windowed bug { diff --git a/cgahook/server.cpp b/cgahook/server.cpp index de125fc..2b7dc72 100644 --- a/cgahook/server.cpp +++ b/cgahook/server.cpp @@ -15,6 +15,7 @@ extern CGA::CGAService g_CGAService; extern int g_MainPort; extern HWND g_MainHwnd; +extern ULONG g_MainProcessId; extern ULONG g_MainThreadId; extern HANDLE g_hQuitEvent; extern HANDLE g_hPortMutex; @@ -125,14 +126,12 @@ BOOL CGA_CreatePortMutex(int port) void CGA_CreateSharedData(int port) { - ULONG ProcessId = GetCurrentProcessId(); - WCHAR szLockName[64]; - wsprintfW(szLockName, L"CGASharedDataLock_%d", ProcessId); + wsprintfW(szLockName, L"CGASharedDataLock_%d_%d", g_MainProcessId, g_MainThreadId); g_hDataLock = CreateMutexW(NULL, TRUE, szLockName); WCHAR szMappingName[64]; - wsprintfW(szMappingName, L"CGASharedData_%d", ProcessId); + wsprintfW(szMappingName, L"CGASharedData_%d_%d", g_MainProcessId, g_MainThreadId); g_hFileMapping = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(CGA::CGAShare_t), szMappingName); if (g_hFileMapping && GetLastError() != ERROR_ALREADY_EXISTS) { @@ -141,7 +140,7 @@ void CGA_CreateSharedData(int port) { CGA::CGAShare_t *data = (CGA::CGAShare_t *)pViewOfFile; - data->ProcessId = ProcessId; + data->ProcessId = g_MainProcessId; data->ThreadId = g_MainThreadId; data->hWnd = (int)g_MainHwnd; data->Port = port;