-
Notifications
You must be signed in to change notification settings - Fork 332
/
main.cpp
86 lines (72 loc) · 2.26 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <Windows.h>
#include <stdio.h>
#include <Shlwapi.h>
#include <string>
#include <detours/detours.h>
#include "Extras\OVR_CAPI_Util.h"
#include "OVR_Version.h"
static HMODULE(WINAPI* TrueLoadLibraryW)(LPCWSTR lpFileName) = LoadLibraryW;
static HMODULE(WINAPI* TrueLoadLibraryExW)(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags) = LoadLibraryExW;
WCHAR revModuleName[MAX_PATH];
WCHAR ovrModuleName[MAX_PATH];
HMODULE WINAPI HookLoadLibraryW(LPCWSTR lpFileName)
{
LPCWSTR name = PathFindFileNameW(lpFileName);
LPCWSTR ext = PathFindExtensionW(name);
size_t length = ext - name;
// Load our own library again so the ref count is incremented.
if (wcsncmp(name, ovrModuleName, length) == 0)
return TrueLoadLibraryW(revModuleName);
return TrueLoadLibraryW(lpFileName);
}
HMODULE WINAPI HookLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
LPCWSTR name = PathFindFileNameW(lpLibFileName);
LPCWSTR ext = PathFindExtensionW(name);
size_t length = ext - name;
// Load our own library again so the ref count is incremented.
if (wcsncmp(name, ovrModuleName, length) == 0)
return TrueLoadLibraryExW(revModuleName, hFile, dwFlags);
return TrueLoadLibraryExW(lpLibFileName, hFile, dwFlags);
}
void AttachDetours()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&TrueLoadLibraryW, HookLoadLibraryW);
DetourAttach((PVOID*)&TrueLoadLibraryExW, HookLoadLibraryExW);
DetourTransactionCommit();
}
void DetachDetours()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID*)&TrueLoadLibraryW, HookLoadLibraryW);
DetourDetach((PVOID*)&TrueLoadLibraryExW, HookLoadLibraryExW);
DetourTransactionCommit();
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (DetourIsHelperProcess())
return TRUE;
#if defined(_WIN64)
const char* pBitDepth = "64";
#else
const char* pBitDepth = "32";
#endif
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
GetModuleFileName((HMODULE)hModule, revModuleName, MAX_PATH);
swprintf(ovrModuleName, MAX_PATH, L"LibOVRRT%hs_%d.dll", pBitDepth, OVR_MAJOR_VERSION);
DetourRestoreAfterWith();
AttachDetours();
break;
case DLL_PROCESS_DETACH:
DetachDetours();
break;
default:
break;
}
return TRUE;
}