forked from LibreVR/Revive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
70 lines (55 loc) · 1.83 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
#include <Windows.h>
#include <stdio.h>
#include "MinHook.h"
#include "Shlwapi.h"
#include <openvr.h>
#include "Extras\OVR_CAPI_Util.h"
#include "OVR_Version.h"
/// This is the Windows Named Event name that is used to check for HMD connected state.
#define REV_HMD_CONNECTED_EVENT_NAME L"ReviveHMDConnected"
typedef HMODULE(__stdcall* _LoadLibrary)(LPCWSTR lpFileName);
typedef HANDLE(__stdcall* _OpenEvent)(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName);
_LoadLibrary TrueLoadLibrary;
_OpenEvent TrueOpenEvent;
HANDLE ReviveModule;
HMODULE WINAPI HookLoadLibrary(LPCWSTR lpFileName)
{
#if defined(_WIN64)
const char* pBitDepth = "64";
#else
const char* pBitDepth = "32";
#endif
LPCWSTR fileName = PathFindFileNameW(lpFileName);
WCHAR libraryName[MAX_PATH];
swprintf(libraryName, MAX_PATH, L"LibOVRRT%hs_%d.dll", pBitDepth, OVR_MAJOR_VERSION);
if (wcscmp(fileName, libraryName) == 0)
return (HMODULE)ReviveModule;
return TrueLoadLibrary(lpFileName);
}
HANDLE WINAPI HookOpenEvent(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
{
if (wcscmp(lpName, OVR_HMD_CONNECTED_EVENT_NAME) == 0)
return ::CreateEventW(NULL, TRUE, TRUE, REV_HMD_CONNECTED_EVENT_NAME);
return TrueOpenEvent(dwDesiredAccess, bInheritHandle, lpName);
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
ReviveModule = hModule;
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MH_Initialize();
MH_CreateHook(LoadLibraryW, HookLoadLibrary, (PVOID*)&TrueLoadLibrary);
MH_CreateHook(OpenEventW, HookOpenEvent, (PVOID*)&TrueOpenEvent);
MH_EnableHook(LoadLibraryW);
MH_EnableHook(OpenEventW);
break;
case DLL_PROCESS_DETACH:
MH_RemoveHook(LoadLibraryW);
MH_RemoveHook(OpenEventW);
MH_Uninitialize();
default:
break;
}
return TRUE;
}