12
12
13
13
#pragma warning (disable : 4996)
14
14
15
+ typedef BOOL (WINAPI* PFN_FreeLibrary)(HMODULE lpLibrary);
15
16
typedef HMODULE (WINAPI* PFN_LoadLibraryA)(LPCSTR lpLibFileName);
16
17
typedef HMODULE (WINAPI* PFN_LoadLibraryW)(LPCWSTR lpLibFileName);
17
18
typedef HMODULE (WINAPI* PFN_LoadLibraryExA)(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
18
19
typedef HMODULE (WINAPI* PFN_LoadLibraryExW)(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
19
20
typedef const char * (CDECL* PFN_wine_get_version)(void );
20
21
22
+ PFN_FreeLibrary o_FreeLibrary = nullptr ;
21
23
PFN_LoadLibraryA o_LoadLibraryA = nullptr ;
22
24
PFN_LoadLibraryW o_LoadLibraryW = nullptr ;
23
25
PFN_LoadLibraryExA o_LoadLibraryExA = nullptr ;
@@ -26,20 +28,22 @@ PFN_vkGetPhysicalDeviceProperties o_vkGetPhysicalDeviceProperties = nullptr;
26
28
PFN_vkGetPhysicalDeviceProperties2 o_vkGetPhysicalDeviceProperties2 = nullptr ;
27
29
PFN_vkGetPhysicalDeviceProperties2KHR o_vkGetPhysicalDeviceProperties2KHR = nullptr ;
28
30
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" );
33
35
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" );
38
40
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 ;
43
47
44
48
void AttachHooks ();
45
49
void DetachHooks ();
@@ -85,6 +89,25 @@ static HMODULE LoadNvApi()
85
89
return nullptr ;
86
90
}
87
91
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
+
88
111
static HMODULE hkLoadLibraryA (LPCSTR lpLibFileName)
89
112
{
90
113
if (lpLibFileName == nullptr )
@@ -134,6 +157,7 @@ static HMODULE hkLoadLibraryA(LPCSTR lpLibFileName)
134
157
if (pos != std::string::npos && pos == (lcaseLibName.size () - dllNameA.size ()))
135
158
{
136
159
spdlog::info (" hkLoadLibraryA {0} call returning this dll!" , libName);
160
+ loadCount++;
137
161
return dllModule;
138
162
}
139
163
@@ -193,6 +217,7 @@ static HMODULE hkLoadLibraryW(LPCWSTR lpLibFileName)
193
217
if (pos != std::string::npos && pos == (lcaseLibName.size () - dllNameW.size ()))
194
218
{
195
219
spdlog::info (" hkLoadLibraryW {0} call, returning this dll!" , lcaseLibNameA);
220
+ loadCount++;
196
221
return dllModule;
197
222
}
198
223
@@ -268,6 +293,7 @@ static HMODULE hkLoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlag
268
293
if (pos != std::string::npos && pos == (lcaseLibName.size () - dllNameA.size ()))
269
294
{
270
295
spdlog::info (" hkLoadLibraryExA {0} call, returning this dll!" , libName);
296
+ loadCount++;
271
297
return dllModule;
272
298
}
273
299
@@ -276,6 +302,7 @@ static HMODULE hkLoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlag
276
302
if (pos != std::string::npos && pos == (lcaseLibName.size () - dllNameExA.size ()))
277
303
{
278
304
spdlog::info (" hkLoadLibraryExA {0} call, returning this dll!" , libName);
305
+ loadCount++;
279
306
return dllModule;
280
307
}
281
308
@@ -354,6 +381,7 @@ static HMODULE hkLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFla
354
381
if (pos != std::string::npos && pos == (lcaseLibName.size () - dllNameW.size ()))
355
382
{
356
383
spdlog::info (" hkLoadLibraryExW {0} call, returning this dll!" , lcaseLibNameA);
384
+ loadCount++;
357
385
return dllModule;
358
386
}
359
387
@@ -362,6 +390,7 @@ static HMODULE hkLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFla
362
390
if (pos != std::string::npos && pos == (lcaseLibName.size () - dllNameExW.size ()))
363
391
{
364
392
spdlog::info (" hkLoadLibraryExW {0} call, returning this dll!" , lcaseLibNameA);
393
+ loadCount++;
365
394
return dllModule;
366
395
}
367
396
@@ -411,6 +440,12 @@ static void DetachHooks()
411
440
412
441
DetourUpdateThread (GetCurrentThread ());
413
442
443
+ if (o_FreeLibrary)
444
+ {
445
+ DetourDetach (&(PVOID&)o_FreeLibrary, hkFreeLibrary);
446
+ o_FreeLibrary = nullptr ;
447
+ }
448
+
414
449
if (o_LoadLibraryA)
415
450
{
416
451
DetourDetach (&(PVOID&)o_LoadLibraryA, hkLoadLibraryA);
@@ -464,6 +499,7 @@ static void AttachHooks()
464
499
if (o_LoadLibraryA == nullptr || o_LoadLibraryW == nullptr )
465
500
{
466
501
// Detour the functions
502
+ o_FreeLibrary = reinterpret_cast <PFN_FreeLibrary>(DetourFindFunction (" kernel32.dll" , " FreeLibrary" ));
467
503
o_LoadLibraryA = reinterpret_cast <PFN_LoadLibraryA>(DetourFindFunction (" kernel32.dll" , " LoadLibraryA" ));
468
504
o_LoadLibraryW = reinterpret_cast <PFN_LoadLibraryW>(DetourFindFunction (" kernel32.dll" , " LoadLibraryW" ));
469
505
o_LoadLibraryExA = reinterpret_cast <PFN_LoadLibraryExA>(DetourFindFunction (" kernel32.dll" , " LoadLibraryExA" ));
@@ -474,6 +510,9 @@ static void AttachHooks()
474
510
DetourTransactionBegin ();
475
511
DetourUpdateThread (GetCurrentThread ());
476
512
513
+ if (o_FreeLibrary)
514
+ DetourAttach (&(PVOID&)o_FreeLibrary, hkFreeLibrary);
515
+
477
516
if (o_LoadLibraryA)
478
517
DetourAttach (&(PVOID&)o_LoadLibraryA, hkLoadLibraryA);
479
518
@@ -854,6 +893,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
854
893
switch (ul_reason_for_call)
855
894
{
856
895
case DLL_PROCESS_ATTACH:
896
+ loadCount++;
897
+
857
898
DisableThreadLibraryCalls (hModule);
858
899
859
900
dllModule = hModule;
@@ -898,7 +939,6 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
898
939
case DLL_PROCESS_DETACH:
899
940
CloseLogger ();
900
941
DetachHooks ();
901
-
902
942
break ;
903
943
904
944
default :
0 commit comments