mirror of
https://gitlab.com/Mr_Goldberg/goldberg_emulator
synced 2025-05-23 21:57:40 +08:00
Fix detours not attaching to all threads on startup.
This commit is contained in:
parent
c9236a7337
commit
a8c696c565
49
dll/base.cpp
49
dll/base.cpp
@ -841,16 +841,55 @@ HINTERNET WINAPI Mine_WinHttpOpenRequest(
|
|||||||
return Real_WinHttpOpenRequest(hConnect, pwszVerb, pwszObjectName, pwszVersion, pwszReferrer, ppwszAcceptTypes, dwFlags);
|
return Real_WinHttpOpenRequest(hConnect, pwszVerb, pwszObjectName, pwszVersion, pwszReferrer, ppwszAcceptTypes, dwFlags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static bool network_functions_attached = false;
|
static bool network_functions_attached = false;
|
||||||
|
std::vector<HANDLE> detours_threads;
|
||||||
|
|
||||||
BOOL WINAPI DllMain( HINSTANCE, DWORD dwReason, LPVOID ) {
|
BOOL WINAPI DllMain( HINSTANCE, DWORD dwReason, LPVOID ) {
|
||||||
switch ( dwReason ) {
|
switch ( dwReason ) {
|
||||||
case DLL_PROCESS_ATTACH:
|
case DLL_PROCESS_ATTACH:
|
||||||
if (!file_exists(get_full_program_path() + "disable_lan_only.txt") && !file_exists(get_full_program_path() + "\\steam_settings\\disable_lan_only.txt")) {
|
if (!file_exists(get_full_program_path() + "disable_lan_only.txt") && !file_exists(get_full_program_path() + "\\steam_settings\\disable_lan_only.txt")) {
|
||||||
PRINT_DEBUG("Hooking lan only functions\n");
|
PRINT_DEBUG("Hooking lan only functions\n");
|
||||||
DetourTransactionBegin();
|
DetourTransactionBegin();
|
||||||
DetourUpdateThread( GetCurrentThread() );
|
HANDLE toolHelp = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
|
||||||
|
if (toolHelp != INVALID_HANDLE_VALUE) {
|
||||||
|
THREADENTRY32 te;
|
||||||
|
te.dwSize = sizeof(THREADENTRY32);
|
||||||
|
if (Thread32First(toolHelp, &te)) {
|
||||||
|
bool bUpdatedThread = false;
|
||||||
|
DWORD myPID = GetCurrentProcessId();
|
||||||
|
DWORD myTID = GetCurrentThreadId();
|
||||||
|
HANDLE tHandle;
|
||||||
|
do {
|
||||||
|
if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID)) {
|
||||||
|
if (te.th32OwnerProcessID == myPID) {
|
||||||
|
if (te.th32ThreadID != myTID) {
|
||||||
|
tHandle = OpenThread(THREAD_ALL_ACCESS, false, te.th32ThreadID);
|
||||||
|
if (tHandle != NULL) {
|
||||||
|
PRINT_DEBUG("Hooking thread %d\n", te.th32ThreadID);
|
||||||
|
detours_threads.push_back( tHandle );
|
||||||
|
DetourUpdateThread( tHandle );
|
||||||
|
bUpdatedThread = true;
|
||||||
|
} else {
|
||||||
|
PRINT_DEBUG("Unable to hook thread %d\n", te.th32ThreadID);
|
||||||
|
}
|
||||||
|
} else {//hooking non-pseudo current thread handle is unsupported.
|
||||||
|
PRINT_DEBUG("Hooking thread %d\n", myTID);
|
||||||
|
DetourUpdateThread( GetCurrentThread() );
|
||||||
|
bUpdatedThread = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
te.dwSize = sizeof(THREADENTRY32);
|
||||||
|
} while (Thread32Next(toolHelp, &te));
|
||||||
|
} else {
|
||||||
|
PRINT_DEBUG("Unable to iterate thread list, only hooking current thread\n");
|
||||||
|
DetourUpdateThread( GetCurrentThread() );
|
||||||
|
}
|
||||||
|
CloseHandle(toolHelp);
|
||||||
|
} else {
|
||||||
|
PRINT_DEBUG("Unable to get thread list, only hooking current thread\n");
|
||||||
|
DetourUpdateThread( GetCurrentThread() );
|
||||||
|
}
|
||||||
DetourAttach( &(PVOID &)Real_SendTo, Mine_SendTo );
|
DetourAttach( &(PVOID &)Real_SendTo, Mine_SendTo );
|
||||||
DetourAttach( &(PVOID &)Real_Connect, Mine_Connect );
|
DetourAttach( &(PVOID &)Real_Connect, Mine_Connect );
|
||||||
DetourAttach( &(PVOID &)Real_WSAConnect, Mine_WSAConnect );
|
DetourAttach( &(PVOID &)Real_WSAConnect, Mine_WSAConnect );
|
||||||
@ -871,8 +910,12 @@ BOOL WINAPI DllMain( HINSTANCE, DWORD dwReason, LPVOID ) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case DLL_PROCESS_DETACH:
|
case DLL_PROCESS_DETACH:
|
||||||
|
std::vector<HANDLE>::iterator it;
|
||||||
if (network_functions_attached) {
|
if (network_functions_attached) {
|
||||||
DetourTransactionBegin();
|
DetourTransactionBegin();
|
||||||
|
for (it = detours_threads.begin(); it != detours_threads.end(); it++) {
|
||||||
|
DetourUpdateThread( *it );
|
||||||
|
}
|
||||||
DetourUpdateThread( GetCurrentThread() );
|
DetourUpdateThread( GetCurrentThread() );
|
||||||
DetourDetach( &(PVOID &)Real_SendTo, Mine_SendTo );
|
DetourDetach( &(PVOID &)Real_SendTo, Mine_SendTo );
|
||||||
DetourDetach( &(PVOID &)Real_Connect, Mine_Connect );
|
DetourDetach( &(PVOID &)Real_Connect, Mine_Connect );
|
||||||
|
@ -86,6 +86,7 @@
|
|||||||
|
|
||||||
#ifdef EMU_EXPERIMENTAL_BUILD
|
#ifdef EMU_EXPERIMENTAL_BUILD
|
||||||
#include <winhttp.h>
|
#include <winhttp.h>
|
||||||
|
#include <tlhelp32.h>
|
||||||
|
|
||||||
#include "../detours/detours.h"
|
#include "../detours/detours.h"
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user