1
0
mirror of https://github.com/FunkyFr3sh/cnc-ddraw.git synced 2025-03-15 06:04:49 +01:00

use different function to enumarte modules for mingw build

This commit is contained in:
FunkyFr3sh 2023-10-23 08:20:25 +02:00
parent 0f08cbcc65
commit bae707f865
3 changed files with 60 additions and 26 deletions

View File

@ -5,6 +5,7 @@
#include <windows.h>
HMODULE WINAPI util_enumerate_modules(_In_opt_ HMODULE hModuleLast);
BOOL util_is_bad_read_ptr(void* p);
BOOL util_is_minimized(HWND hwnd);
BOOL util_is_avx_supported();

View File

@ -474,17 +474,8 @@ void hook_create(HOOKLIST* hooks, BOOL initial_hook)
char mod_dir[MAX_PATH] = { 0 };
char mod_filename[MAX_PATH] = { 0 };
HMODULE hmod = NULL;
HANDLE process = NULL;
#ifndef _MSC_VER
HMODULE mods[512];
memset(mods, 0, sizeof(mods));
process = GetCurrentProcess();
EnumProcessModules(process, mods, sizeof(mods) - sizeof(mods[0]), NULL);
for (int i = 0; i < sizeof(mods) / sizeof(mods[0]) && (hmod = mods[i]); i++)
#else
while (hmod = DetourEnumerateModules(hmod))
#endif
while ((hmod = util_enumerate_modules(hmod)))
{
if (hmod == g_ddraw_module)
continue;
@ -515,9 +506,6 @@ void hook_create(HOOKLIST* hooks, BOOL initial_hook)
}
}
}
if (process)
CloseHandle(process);
}
}
@ -561,17 +549,8 @@ void hook_revert(HOOKLIST* hooks)
char mod_dir[MAX_PATH] = { 0 };
char mod_filename[MAX_PATH] = { 0 };
HMODULE hmod = NULL;
HANDLE process = NULL;
#ifndef _MSC_VER
HMODULE mods[512];
memset(mods, 0, sizeof(mods));
process = GetCurrentProcess();
EnumProcessModules(process, mods, sizeof(mods) - sizeof(mods[0]), NULL);
for (int i = 0; i < sizeof(mods) / sizeof(mods[0]) && (hmod = mods[i]); i++)
#else
while (hmod = DetourEnumerateModules(hmod))
#endif
while ((hmod = util_enumerate_modules(hmod)))
{
if (hmod == g_ddraw_module)
continue;
@ -591,9 +570,6 @@ void hook_revert(HOOKLIST* hooks)
}
}
}
if (process)
CloseHandle(process);
}
}

View File

@ -11,6 +11,63 @@
#include "config.h"
/*
The following code is licensed under the MIT license:
DetourEnumerateModules
Copyright (c) Microsoft Corporation
https://github.com/microsoft/Detours
*/
#define MM_ALLOCATION_GRANULARITY 0x10000
HMODULE WINAPI util_enumerate_modules(_In_opt_ HMODULE hModuleLast)
{
PBYTE pbLast = (PBYTE)hModuleLast + MM_ALLOCATION_GRANULARITY;
MEMORY_BASIC_INFORMATION mbi;
ZeroMemory(&mbi, sizeof(mbi));
// Find the next memory region that contains a mapped PE image.
//
for (;; pbLast = (PBYTE)mbi.BaseAddress + mbi.RegionSize) {
if (VirtualQuery(pbLast, &mbi, sizeof(mbi)) <= 0) {
break;
}
// Skip uncommitted regions and guard pages.
//
if ((mbi.State != MEM_COMMIT) ||
((mbi.Protect & 0xff) == PAGE_NOACCESS) ||
(mbi.Protect & PAGE_GUARD)) {
continue;
}
__try {
PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)pbLast;
if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE ||
(DWORD)pDosHeader->e_lfanew > mbi.RegionSize ||
(DWORD)pDosHeader->e_lfanew < sizeof(*pDosHeader)) {
continue;
}
PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +
pDosHeader->e_lfanew);
if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {
continue;
}
return (HMODULE)pDosHeader;
}
#if defined(_MSC_VER)
#pragma prefast(suppress:28940, "A bad pointer means this probably isn't a PE header.")
#endif
__except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
continue;
}
}
return NULL;
}
BOOL util_is_bad_read_ptr(void* p)
{
MEMORY_BASIC_INFORMATION mbi = { 0 };