-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzmod_dll.cpp
163 lines (152 loc) · 4.61 KB
/
zmod_dll.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <Windows.h>
#include <map>
#include <filesystem>
#include "detours.h"
#include "zmod_common.cpp"
// #pragma comment(linker, "/export:DetourFinishHelperProcess=DetourFinishHelperProcess,@1")
#pragma comment(linker, "/export:DirectInput8Create=C:\\Windows\\System32\\dinput8.DirectInput8Create")
#pragma comment(linker, "/export:DllCanUnloadNow=C:\\Windows\\System32\\dinput8.DllCanUnloadNow")
#pragma comment(linker, "/export:DllGetClassObject=C:\\Windows\\System32\\dinput8.DllGetClassObject")
#pragma comment(linker, "/export:DllRegisterServer=C:\\Windows\\System32\\dinput8.DllRegisterServer")
#pragma comment(linker, "/export:DllUnregisterServer=C:\\Windows\\System32\\dinput8.DllUnregisterServer")
#pragma comment(linker, "/export:GetdfDIJoystick=C:\\Windows\\System32\\dinput8.GetdfDIJoystick")
typedef HWND(WINAPI CreateWindowExA_t)(
DWORD dwExStyle,
LPCSTR lpClassName,
LPCSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam);
typedef HWND(WINAPI CreateWindowExW_t)(
DWORD dwExStyle,
LPCWSTR lpClassName,
LPCWSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam);
CreateWindowExA_t CreateWindowExA_hook;
CreateWindowExW_t CreateWindowExW_hook;
void load_dlls();
CreateWindowExA_t *CreateWindowExA_orig = CreateWindowExA;
CreateWindowExW_t *CreateWindowExW_orig = CreateWindowExW;
std::filesystem::path module_path;
HWND WINAPI CreateWindowExA_hook(
DWORD dwExStyle,
LPCSTR lpClassName,
LPCSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam)
{
auto ret = CreateWindowExA_orig(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID &)CreateWindowExA_orig, CreateWindowExA_hook);
DetourDetach(&(PVOID &)CreateWindowExW_orig, CreateWindowExW_hook);
DetourTransactionCommit();
load_dlls();
return ret;
}
HWND WINAPI CreateWindowExW_hook(
DWORD dwExStyle,
LPCWSTR lpClassName,
LPCWSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam)
{
auto ret = CreateWindowExW_orig(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID &)CreateWindowExA_orig, CreateWindowExA_hook);
DetourDetach(&(PVOID &)CreateWindowExW_orig, CreateWindowExW_hook);
DetourTransactionCommit();
load_dlls();
return ret;
}
std::wstring to_lower(std::wstring str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}
void load_dlls()
{
std::vector<std::filesystem::path> matching_dlls;
auto parent_path = module_path.parent_path();
for (const auto &entry : std::filesystem::directory_iterator(parent_path))
{
if (!(entry.is_regular_file()))
{
continue;
}
if (to_lower(entry.path().extension().wstring()) != L".dll")
{
continue;
}
auto filename = to_lower(entry.path().filename().wstring());
if (filename == L"zmod_32.dll")
{
continue;
}
if (filename == L"zmod_64.dll")
{
continue;
}
if (filename.find(L"zmod") != 0)
{
continue;
}
matching_dlls.push_back(entry.path());
}
std::sort(matching_dlls.begin(), matching_dlls.end());
for (const auto &dll : matching_dlls)
{
LoadLibraryW(dll.wstring().c_str());
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (DetourIsHelperProcess())
{
return TRUE;
}
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
if (!(DisableThreadLibraryCalls(hinstDLL)))
;
module_path = zmod::get_module_path(hinstDLL);
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID &)CreateWindowExA_orig, CreateWindowExA_hook);
DetourAttach(&(PVOID &)CreateWindowExW_orig, CreateWindowExW_hook);
DetourTransactionCommit();
break;
}
return TRUE;
}