Skip to content

Commit

Permalink
Call DDrawCompat for each Dllmain() call
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Sep 11, 2024
1 parent 641bfc0 commit 8e9ebef
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 34 deletions.
59 changes: 42 additions & 17 deletions DDrawCompat/v0.3.1/Common/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,35 +242,60 @@ namespace
static bool RunOnce = true;
if (RunOnce)
{
// Get System32 path
char syspath[MAX_PATH];
GetSystemDirectory(syspath, MAX_PATH);
RunOnce = false;

// Load dbghelp.dll from System32
char path[MAX_PATH];
strcpy_s(path, MAX_PATH, syspath);
PathAppend(path, "dbghelp.dll");
HMODULE dll = LoadLibrary(path);
HMODULE help_dll = nullptr;
HMODULE eng_dll = nullptr;

// Check if modules are loaded
GetModuleHandleExA(0, "dbghelp.dll", &help_dll);
GetModuleHandleExA(0, "dbgeng.dll", &eng_dll);
bool LoadFromSystem32 = (!help_dll && !eng_dll);
if (help_dll)
{
Compat32::Log() << "Warning: DbgHelp: is already loaded!";
}
if (eng_dll)
{
Compat32::Log() << "Warning: DbgEng: is already loaded!";
}

// Try loading dbgeng.dll from System32
strcpy_s(path, MAX_PATH, syspath);
PathAppend(path, "dbgeng.dll");
dll = LoadLibrary(path);
if (LoadFromSystem32)
{
// Get System32 path
char syspath[MAX_PATH];
GetSystemDirectory(syspath, MAX_PATH);

// Load dbghelp.dll from System32
strcpy_s(path, MAX_PATH, syspath);
PathAppend(path, "dbghelp.dll");
help_dll = LoadLibrary(path);

// Try loading dbgeng.dll from System32
strcpy_s(path, MAX_PATH, syspath);
PathAppend(path, "dbgeng.dll");
eng_dll = LoadLibrary(path);
}

// Try loading dbgeng.dll from local path
if (!dll || !GetProcAddress(dll, "DebugCreate"))
if (!eng_dll || !GetProcAddress(eng_dll, "DebugCreate"))
{
dll = LoadLibrary("dbgeng.dll");
eng_dll = LoadLibrary("dbgeng.dll");
}

// Hook function and get process address
if (dll && GetProcAddress(dll, "DebugCreate"))
if (eng_dll && GetProcAddress(eng_dll, "DebugCreate"))
{
Compat32::hookIatFunction(dll, "GetProcAddress", dbgEngGetProcAddress);
if (DDrawCompat::IsEnabled())
{
Compat32::hookIatFunction(eng_dll, "GetProcAddress", dbgEngGetProcAddress);
}

pDebugCreate = (PFN_DebugCreate)GetProcAddress(dll, "DebugCreate");
pDebugCreate = (PFN_DebugCreate)GetProcAddress(eng_dll, "DebugCreate");

GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)pDebugCreate, &eng_dll);
}
RunOnce = false;
}
if (!pDebugCreate)
{
Expand Down
7 changes: 0 additions & 7 deletions DDrawCompat/v0.3.1/Dll/DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,6 @@ namespace Compat32

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
static bool skipDllMain = false;
if (skipDllMain)
{
skipDllMain = true;
return TRUE;
}

if (fdwReason == DLL_PROCESS_ATTACH)
{
Dll::g_currentModule = hinstDLL;
Expand Down
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7135
#define BUILD_NUMBER 7136
34 changes: 25 additions & 9 deletions Dllmain/Dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
// Start DDrawCompat
if (Config.DDrawCompat)
{
DDrawCompat::Start(hModule_dll, DLL_PROCESS_ATTACH);
DDrawCompat::Start(hModule_dll, fdwReason);
}
#endif // DDRAWCOMPAT
}
Expand Down Expand Up @@ -571,13 +571,29 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
}
break;
case DLL_THREAD_ATTACH:
#ifdef DDRAWCOMPAT
// Unload and Unhook DDrawCompat
if (DDrawCompat::IsEnabled())
{
DDrawCompat::Start(nullptr, fdwReason);
}
#endif // DDRAWCOMPAT

// Check if thread has started
if (Config.ForceTermination && Fullscreen::IsThreadRunning())
{
FullscreenThreadStartedFlag = true;
}
break;
case DLL_THREAD_DETACH:
#ifdef DDRAWCOMPAT
// Unload and Unhook DDrawCompat
if (DDrawCompat::IsEnabled())
{
DDrawCompat::Start(nullptr, fdwReason);
}
#endif // DDRAWCOMPAT

if (Config.ForceTermination)
{
// Check if thread has started
Expand Down Expand Up @@ -612,14 +628,6 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
Fullscreen::StopThread();
WriteMemory::StopThread();

#ifdef DDRAWCOMPAT
// Unload and Unhook DDrawCompat
if (DDrawCompat::IsEnabled() || Config.Dd7to9)
{
DDrawCompat::Start(nullptr, DLL_PROCESS_DETACH);
}
#endif // DDRAWCOMPAT

// Unload DdrawWrapper
if (Config.Dd7to9)
{
Expand All @@ -629,6 +637,14 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
// Unhook all APIs
Hook::UnhookAll();

#ifdef DDRAWCOMPAT
// Unload and Unhook DDrawCompat
if (DDrawCompat::IsEnabled())
{
DDrawCompat::Start(nullptr, fdwReason);
}
#endif // DDRAWCOMPAT

// Unload loaded dlls
Utils::UnloadAllDlls();

Expand Down

0 comments on commit 8e9ebef

Please sign in to comment.