Skip to content

Commit 88ede09

Browse files
committed
fixed non-nvngx dll unloading handling
1 parent beeeca3 commit 88ede09

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

OptiScaler/OptiScaler.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<IncludePath>$(SolutionDir)external\vulkan\include;$(SolutionDir)external\nvngx_dlss_sdk;$(SolutionDir)external\xess\inc\xess;$(SolutionDir)external\simpleini;$(SolutionDir)external\unordered_dense\include;$(SolutionDir)external\spdlog\include;$(IncludePath)</IncludePath>
8383
<LibraryPath>$(ProjectDir)fsr2\lib;$(ProjectDir)fsr2_212\lib;$(ProjectDir)vulkan;$(ProjectDir)d3dx;$(ProjectDir)detours;$(SolutionDir)external\xess\lib;$(LibraryPath)</LibraryPath>
8484
<TargetName>dxgi</TargetName>
85-
<OutDir>D:\Folders\Games\Deep Rock Galactic\FSD\Binaries\Win64\</OutDir>
85+
<OutDir>F:\Games\Steam\steamapps\common\Sons Of The Forest\</OutDir>
8686
<IntDir>.\x64\Debug</IntDir>
8787
</PropertyGroup>
8888
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">

OptiScaler/dllmain.cpp

+53-13
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
#pragma warning (disable : 4996)
1414

15+
typedef BOOL(WINAPI* PFN_FreeLibrary)(HMODULE lpLibrary);
1516
typedef HMODULE(WINAPI* PFN_LoadLibraryA)(LPCSTR lpLibFileName);
1617
typedef HMODULE(WINAPI* PFN_LoadLibraryW)(LPCWSTR lpLibFileName);
1718
typedef HMODULE(WINAPI* PFN_LoadLibraryExA)(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
1819
typedef HMODULE(WINAPI* PFN_LoadLibraryExW)(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
1920
typedef const char* (CDECL* PFN_wine_get_version)(void);
2021

22+
PFN_FreeLibrary o_FreeLibrary = nullptr;
2123
PFN_LoadLibraryA o_LoadLibraryA = nullptr;
2224
PFN_LoadLibraryW o_LoadLibraryW = nullptr;
2325
PFN_LoadLibraryExA o_LoadLibraryExA = nullptr;
@@ -26,20 +28,22 @@ PFN_vkGetPhysicalDeviceProperties o_vkGetPhysicalDeviceProperties = nullptr;
2628
PFN_vkGetPhysicalDeviceProperties2 o_vkGetPhysicalDeviceProperties2 = nullptr;
2729
PFN_vkGetPhysicalDeviceProperties2KHR o_vkGetPhysicalDeviceProperties2KHR = nullptr;
2830

29-
std::string nvngxA("nvngx.dll");
30-
std::string nvngxExA("nvngx");
31-
std::wstring nvngxW(L"nvngx.dll");
32-
std::wstring nvngxExW(L"nvngx");
31+
static std::string nvngxA("nvngx.dll");
32+
static std::string nvngxExA("nvngx");
33+
static std::wstring nvngxW(L"nvngx.dll");
34+
static std::wstring nvngxExW(L"nvngx");
3335

34-
std::string nvapiA("nvapi64.dll");
35-
std::string nvapiExA("nvapi64");
36-
std::wstring nvapiW(L"nvapi64.dll");
37-
std::wstring nvapiExW(L"nvapi64");
36+
static std::string nvapiA("nvapi64.dll");
37+
static std::string nvapiExA("nvapi64");
38+
static std::wstring nvapiW(L"nvapi64.dll");
39+
static std::wstring nvapiExW(L"nvapi64");
3840

39-
std::string dllNameA;
40-
std::string dllNameExA;
41-
std::wstring dllNameW;
42-
std::wstring dllNameExW;
41+
static std::string dllNameA;
42+
static std::string dllNameExA;
43+
static std::wstring dllNameW;
44+
static std::wstring dllNameExW;
45+
46+
static int loadCount = 0;
4347

4448
void AttachHooks();
4549
void DetachHooks();
@@ -85,6 +89,25 @@ static HMODULE LoadNvApi()
8589
return nullptr;
8690
}
8791

92+
static BOOL hkFreeLibrary(HMODULE lpLibrary)
93+
{
94+
if (lpLibrary == nullptr)
95+
return FALSE;
96+
97+
if (lpLibrary == dllModule)
98+
{
99+
loadCount--;
100+
spdlog::info("hkFreeLibrary call for this module loadCount: {0}", loadCount);
101+
102+
if (loadCount == 0)
103+
return o_FreeLibrary(lpLibrary);
104+
else
105+
return TRUE;
106+
}
107+
108+
return o_FreeLibrary(lpLibrary);
109+
}
110+
88111
static HMODULE hkLoadLibraryA(LPCSTR lpLibFileName)
89112
{
90113
if (lpLibFileName == nullptr)
@@ -134,6 +157,7 @@ static HMODULE hkLoadLibraryA(LPCSTR lpLibFileName)
134157
if (pos != std::string::npos && pos == (lcaseLibName.size() - dllNameA.size()))
135158
{
136159
spdlog::info("hkLoadLibraryA {0} call returning this dll!", libName);
160+
loadCount++;
137161
return dllModule;
138162
}
139163

@@ -193,6 +217,7 @@ static HMODULE hkLoadLibraryW(LPCWSTR lpLibFileName)
193217
if (pos != std::string::npos && pos == (lcaseLibName.size() - dllNameW.size()))
194218
{
195219
spdlog::info("hkLoadLibraryW {0} call, returning this dll!", lcaseLibNameA);
220+
loadCount++;
196221
return dllModule;
197222
}
198223

@@ -268,6 +293,7 @@ static HMODULE hkLoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlag
268293
if (pos != std::string::npos && pos == (lcaseLibName.size() - dllNameA.size()))
269294
{
270295
spdlog::info("hkLoadLibraryExA {0} call, returning this dll!", libName);
296+
loadCount++;
271297
return dllModule;
272298
}
273299

@@ -276,6 +302,7 @@ static HMODULE hkLoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlag
276302
if (pos != std::string::npos && pos == (lcaseLibName.size() - dllNameExA.size()))
277303
{
278304
spdlog::info("hkLoadLibraryExA {0} call, returning this dll!", libName);
305+
loadCount++;
279306
return dllModule;
280307
}
281308

@@ -354,6 +381,7 @@ static HMODULE hkLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFla
354381
if (pos != std::string::npos && pos == (lcaseLibName.size() - dllNameW.size()))
355382
{
356383
spdlog::info("hkLoadLibraryExW {0} call, returning this dll!", lcaseLibNameA);
384+
loadCount++;
357385
return dllModule;
358386
}
359387

@@ -362,6 +390,7 @@ static HMODULE hkLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFla
362390
if (pos != std::string::npos && pos == (lcaseLibName.size() - dllNameExW.size()))
363391
{
364392
spdlog::info("hkLoadLibraryExW {0} call, returning this dll!", lcaseLibNameA);
393+
loadCount++;
365394
return dllModule;
366395
}
367396

@@ -411,6 +440,12 @@ static void DetachHooks()
411440

412441
DetourUpdateThread(GetCurrentThread());
413442

443+
if (o_FreeLibrary)
444+
{
445+
DetourDetach(&(PVOID&)o_FreeLibrary, hkFreeLibrary);
446+
o_FreeLibrary = nullptr;
447+
}
448+
414449
if (o_LoadLibraryA)
415450
{
416451
DetourDetach(&(PVOID&)o_LoadLibraryA, hkLoadLibraryA);
@@ -464,6 +499,7 @@ static void AttachHooks()
464499
if (o_LoadLibraryA == nullptr || o_LoadLibraryW == nullptr)
465500
{
466501
// Detour the functions
502+
o_FreeLibrary = reinterpret_cast<PFN_FreeLibrary>(DetourFindFunction("kernel32.dll", "FreeLibrary"));
467503
o_LoadLibraryA = reinterpret_cast<PFN_LoadLibraryA>(DetourFindFunction("kernel32.dll", "LoadLibraryA"));
468504
o_LoadLibraryW = reinterpret_cast<PFN_LoadLibraryW>(DetourFindFunction("kernel32.dll", "LoadLibraryW"));
469505
o_LoadLibraryExA = reinterpret_cast<PFN_LoadLibraryExA>(DetourFindFunction("kernel32.dll", "LoadLibraryExA"));
@@ -474,6 +510,9 @@ static void AttachHooks()
474510
DetourTransactionBegin();
475511
DetourUpdateThread(GetCurrentThread());
476512

513+
if (o_FreeLibrary)
514+
DetourAttach(&(PVOID&)o_FreeLibrary, hkFreeLibrary);
515+
477516
if (o_LoadLibraryA)
478517
DetourAttach(&(PVOID&)o_LoadLibraryA, hkLoadLibraryA);
479518

@@ -854,6 +893,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
854893
switch (ul_reason_for_call)
855894
{
856895
case DLL_PROCESS_ATTACH:
896+
loadCount++;
897+
857898
DisableThreadLibraryCalls(hModule);
858899

859900
dllModule = hModule;
@@ -898,7 +939,6 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
898939
case DLL_PROCESS_DETACH:
899940
CloseLogger();
900941
DetachHooks();
901-
902942
break;
903943

904944
default:

0 commit comments

Comments
 (0)