Skip to content

Commit

Permalink
Pin modules to ensure they stay loaded on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Sep 25, 2024
1 parent 0e6baf6 commit b70fec0
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Common/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,12 @@ void CopyReplaceSlash(char* DestStr, size_t Size, LPCSTR SrcStr)
}
}

void PinModule(HMODULE dll)
{
HMODULE dummy = nullptr;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCSTR>(dll), &dummy);
}

BOOL GetAppsLightMode()
{
HKEY hKey;
Expand Down
1 change: 1 addition & 0 deletions Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ bool GetConfigName(char* ConfigName, rsize_t size, char* ext);
bool GetConfigName(wchar_t* ConfigName, rsize_t size, wchar_t* ext);
bool CheckPathNameMatch(LPCSTR lpFileName1, LPCSTR lpFileName2);
void CopyReplaceSlash(char* DestStr, size_t Size, LPCSTR SrcStr);
void PinModule(HMODULE dll);
BOOL GetAppsLightMode();
void ClearGDISurface(HWND hWnd, COLORREF color);
HMONITOR GetMonitorHandle();
Expand Down
2 changes: 1 addition & 1 deletion Resources/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 2217
#define BUILD_NUMBER 2218
3 changes: 3 additions & 0 deletions Wrappers/d3d8/d3d8wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ HMODULE GetD3d8ScriptDll()
if (script_d3d8_dll)
{
Logging::Log() << "Loaded d3d8.dll from: " << script_path_dll.c_str();
PinModule(script_d3d8_dll);
}

// Load d3d8.dll from 'plugins' folder
Expand All @@ -61,6 +62,7 @@ HMODULE GetD3d8ScriptDll()
if (script_d3d8_dll)
{
Logging::Log() << "Loaded d3d8.dll from: " << plugin_path_dll.c_str();
PinModule(script_d3d8_dll);
}
}

Expand Down Expand Up @@ -118,6 +120,7 @@ bool GetLocalDirect3DCreate8()
m_pDirect3DCreate8_local = (Direct3DCreate8Proc)GetProcAddress(h_d3d8, "Direct3DCreate8");
if (m_pDirect3DCreate8_local)
{
PinModule(h_d3d8);
return true;
}
}
Expand Down
6 changes: 6 additions & 0 deletions Wrappers/d3d9/d3d9wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ HMODULE GetSystemD3d9()
GetSystemDirectoryA(Path, MAX_PATH);
strcat_s(Path, "\\d3d9.dll");
GetModuleHandleExA(NULL, Path, &h_d3d9);
if (h_d3d9)
{
PinModule(h_d3d9);
}
}

return h_d3d9;
Expand Down Expand Up @@ -85,6 +89,7 @@ bool GetDirect3DCreate9()
m_pDirect3DCreate9 = (Direct3DCreate9Proc)GetProcAddress(h_d3d9, "Direct3DCreate9");
if (m_pDirect3DCreate9)
{
PinModule(h_d3d9);
return true;
}
}
Expand Down Expand Up @@ -113,6 +118,7 @@ bool GetLocalDirect3DCreate9()
m_pDirect3DCreate9_local = (Direct3DCreate9Proc)GetProcAddress(h_d3d9, "Direct3DCreate9");
if (m_pDirect3DCreate9_local)
{
PinModule(h_d3d9);
return true;
}
}
Expand Down
1 change: 1 addition & 0 deletions Wrappers/dinput8/dinput8wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ bool GetLocalDirectInput8Create()
m_pDirectInput8Create_local = (DirectInput8CreateProc)GetProcAddress(h_dinput8, "DirectInput8Create");
if (m_pDirectInput8Create_local)
{
PinModule(h_dinput8);
return true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions Wrappers/dsound/dsoundwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ bool GetLocalDirectSoundCreate8()
m_pDirectSoundCreate8_local = (DirectSoundCreate8Proc)GetProcAddress(h_dsound, "DirectSoundCreate8");
if (m_pDirectSoundCreate8_local)
{
PinModule(h_dsound);
return true;
}
}
Expand Down Expand Up @@ -93,6 +94,7 @@ bool GetSystem32DirectSoundCreate8()
m_pDirectSoundCreate8_system32 = (DirectSoundCreate8Proc)GetProcAddress(h_dsound, "DirectSoundCreate8");
if (m_pDirectSoundCreate8_system32)
{
PinModule(h_dsound);
return true;
}
}
Expand Down
3 changes: 3 additions & 0 deletions dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,9 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
{
DelayedStart();
}

// Pin current module
PinModule(m_hModule);
}
break;
case DLL_THREAD_ATTACH:
Expand Down

0 comments on commit b70fec0

Please sign in to comment.