From 0d91170fb56655cd0e259bd79d44eeb28128a140 Mon Sep 17 00:00:00 2001 From: narzoul Date: Sun, 28 Feb 2021 22:34:47 +0100 Subject: [PATCH] Use _beginthreadex instead of CreateThread --- DDrawCompat/D3dDdi/KernelModeThunks.cpp | 5 ++--- DDrawCompat/DDraw/RealPrimarySurface.cpp | 5 ++--- DDrawCompat/Dll/Dll.cpp | 12 ++++++++++++ DDrawCompat/Dll/Dll.h | 2 ++ DDrawCompat/Gdi/PresentationWindow.cpp | 9 +++------ 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/DDrawCompat/D3dDdi/KernelModeThunks.cpp b/DDrawCompat/D3dDdi/KernelModeThunks.cpp index cb44cd6..b951faf 100644 --- a/DDrawCompat/D3dDdi/KernelModeThunks.cpp +++ b/DDrawCompat/D3dDdi/KernelModeThunks.cpp @@ -240,7 +240,7 @@ namespace } } - DWORD WINAPI vsyncThreadProc(LPVOID /*lpParameter*/) + unsigned WINAPI vsyncThreadProc(LPVOID /*lpParameter*/) { while (!g_stopVsyncThread) { @@ -323,8 +323,7 @@ namespace D3dDdi Compat::hookIatFunction(origDDrawModule, "gdi32.dll", "D3DKMTQueryAdapterInfo", queryAdapterInfo); Compat::hookIatFunction(origDDrawModule, "gdi32.dll", "D3DKMTSetGammaRamp", setGammaRamp); - g_vsyncThread = CreateThread(nullptr, 0, &vsyncThreadProc, nullptr, 0, nullptr); - SetThreadPriority(g_vsyncThread, THREAD_PRIORITY_TIME_CRITICAL); + g_vsyncThread = Dll::createThread(&vsyncThreadProc, nullptr, THREAD_PRIORITY_TIME_CRITICAL); } void setDcFormatOverride(UINT format) diff --git a/DDrawCompat/DDraw/RealPrimarySurface.cpp b/DDrawCompat/DDraw/RealPrimarySurface.cpp index 02e1f16..3505fc6 100644 --- a/DDrawCompat/DDraw/RealPrimarySurface.cpp +++ b/DDrawCompat/DDraw/RealPrimarySurface.cpp @@ -213,7 +213,7 @@ namespace } } - DWORD WINAPI updateThreadProc(LPVOID /*lpParameter*/) + unsigned WINAPI updateThreadProc(LPVOID /*lpParameter*/) { bool skipWaitForVsync = false; @@ -356,8 +356,7 @@ namespace DDraw void RealPrimarySurface::init() { - g_updateThread = CreateThread(nullptr, 0, &updateThreadProc, nullptr, 0, nullptr); - SetThreadPriority(g_updateThread, THREAD_PRIORITY_TIME_CRITICAL); + g_updateThread = Dll::createThread(&updateThreadProc, nullptr, THREAD_PRIORITY_TIME_CRITICAL); } bool RealPrimarySurface::isFullScreen() diff --git a/DDrawCompat/Dll/Dll.cpp b/DDrawCompat/Dll/Dll.cpp index c0e426f..ba7e9fe 100644 --- a/DDrawCompat/Dll/Dll.cpp +++ b/DDrawCompat/Dll/Dll.cpp @@ -1,3 +1,5 @@ +#include + #include namespace Dll @@ -5,6 +7,16 @@ namespace Dll HMODULE g_currentModule = nullptr; Procs g_origProcs = {}; Procs g_jmpTargetProcs = {}; + + HANDLE createThread(unsigned(__stdcall* threadProc)(void*), unsigned int* threadId, int priority) + { + HANDLE thread = reinterpret_cast(_beginthreadex(nullptr, 0, threadProc, nullptr, 0, threadId)); + if (thread) + { + SetThreadPriority(thread, priority); + } + return thread; + } } #define CREATE_PROC_STUB(procName) \ diff --git a/DDrawCompat/Dll/Dll.h b/DDrawCompat/Dll/Dll.h index 93a04b8..b163af6 100644 --- a/DDrawCompat/Dll/Dll.h +++ b/DDrawCompat/Dll/Dll.h @@ -67,6 +67,8 @@ namespace Dll #undef ADD_FARPROC_MEMBER }; + HANDLE createThread(unsigned(__stdcall* threadProc)(void*), unsigned int* threadId, int priority); + extern HMODULE g_currentModule; extern Procs g_origProcs; extern Procs g_jmpTargetProcs; diff --git a/DDrawCompat/Gdi/PresentationWindow.cpp b/DDrawCompat/Gdi/PresentationWindow.cpp index 69f0ffc..e3a92d7 100644 --- a/DDrawCompat/Gdi/PresentationWindow.cpp +++ b/DDrawCompat/Gdi/PresentationWindow.cpp @@ -10,7 +10,7 @@ namespace const UINT WM_SETPRESENTATIONWINDOWRGN = WM_USER + 2; HANDLE g_presentationWindowThread = nullptr; - DWORD g_presentationWindowThreadId = 0; + unsigned g_presentationWindowThreadId = 0; HWND g_messageWindow = nullptr; LRESULT CALLBACK messageWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) @@ -80,10 +80,8 @@ namespace return CALL_ORIG_FUNC(DefWindowProc)(hwnd, uMsg, wParam, lParam); } - DWORD WINAPI presentationWindowThreadProc(LPVOID /*lpParameter*/) + unsigned WINAPI presentationWindowThreadProc(LPVOID /*lpParameter*/) { - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); - WNDCLASS wc = {}; wc.lpfnWndProc = &messageWindowProc; wc.hInstance = Dll::g_currentModule; @@ -138,8 +136,7 @@ namespace Gdi wc.lpszClassName = "DDrawCompatPresentationWindow"; CALL_ORIG_FUNC(RegisterClassA)(&wc); - g_presentationWindowThread = CreateThread( - nullptr, 0, &presentationWindowThreadProc, nullptr, 0, &g_presentationWindowThreadId); + Dll::createThread(presentationWindowThreadProc, &g_presentationWindowThreadId, THREAD_PRIORITY_TIME_CRITICAL); int i = 0; while (!g_messageWindow && i < 1000)