-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdx9hook.cpp
More file actions
242 lines (187 loc) · 7.36 KB
/
dx9hook.cpp
File metadata and controls
242 lines (187 loc) · 7.36 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "dx9hook.h"
#include <d3d9.h>
#include <iostream>
#include <cstdint>
#include "MinHook.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_dx9.h"
#include "imgui/imgui_impl_win32.h"
#pragma comment(lib, "d3d9.lib")
// Forward declare message handler from imgui_impl_win32.cpp
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
namespace DX9 {
typedef HRESULT(WINAPI* Present_t)(IDirect3DDevice9*, const RECT*, const RECT*, HWND, const RGNDATA*);
typedef HRESULT(WINAPI* Reset_t)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*);
typedef HRESULT(WINAPI* EndScene_t)(IDirect3DDevice9*);
typedef LRESULT(CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
Present_t oPresent = nullptr;
Reset_t oReset = nullptr;
EndScene_t oEndScene = nullptr;
WNDPROC oWndProc = nullptr;
bool initialized = false;
bool showMenu = true;
HWND window = nullptr;
LRESULT CALLBACK hkWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
if (showMenu && ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam)) {
return true;
}
// Toggle menu med INSERT key
if (uMsg == WM_KEYUP && wParam == VK_HOME) {
showMenu = !showMenu;
std::cout << "[*] Menu toggled: " << (showMenu ? "ON" : "OFF") << std::endl;
}
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
}
void InitImGui(IDirect3DDevice9* device) {
std::cout << "[*] Initializing ImGui..." << std::endl;
// Find window
D3DDEVICE_CREATION_PARAMETERS params;
device->GetCreationParameters(¶ms);
window = params.hFocusWindow;
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// Setup style
ImGui::StyleColorsDark();
// Setup Platform/Renderer backends
ImGui_ImplWin32_Init(window);
ImGui_ImplDX9_Init(device);
// Hook WndProc for input
oWndProc = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)hkWndProc);
initialized = true;
std::cout << "[+] ImGui initialized successfully" << std::endl;
}
void CleanupImGui() {
if (initialized) {
ImGui_ImplDX9_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
if (window && oWndProc) {
SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)oWndProc);
}
}
}
HRESULT WINAPI hkEndScene(IDirect3DDevice9* device) {
if (!initialized) {
InitImGui(device);
}
if (initialized && showMenu) {
// Start ImGui frame
ImGui_ImplDX9_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
// Demo window
ImGui::ShowDemoWindow();
// Render ImGui
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
}
return oEndScene(device);
}
HRESULT WINAPI hkPresent(IDirect3DDevice9* device, const RECT* src, const RECT* dest, HWND window, const RGNDATA* region) {
return oPresent(device, src, dest, window, region);
}
HRESULT WINAPI hkReset(IDirect3DDevice9* device, D3DPRESENT_PARAMETERS* params) {
std::cout << "[!] Device Reset called" << std::endl;
ImGui_ImplDX9_InvalidateDeviceObjects();
HRESULT hr = oReset(device, params);
ImGui_ImplDX9_CreateDeviceObjects();
return hr;
}
bool GetD3D9Device(void** pTable, size_t Size) {
if (!pTable) {
return false;
}
IDirect3D9* pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (!pD3D) {
std::cout << "[-] Failed to create D3D9 interface" << std::endl;
return false;
}
IDirect3DDevice9* pDummyDevice = nullptr;
HWND window = FindWindowA("Direct3DWindowClass", NULL);
if (!window) {
window = GetForegroundWindow();
}
if (!window) {
window = GetDesktopWindow();
}
D3DPRESENT_PARAMETERS d3dpp = {};
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.hDeviceWindow = window;
HRESULT hr = pD3D->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
d3dpp.hDeviceWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pDummyDevice
);
if (FAILED(hr)) {
std::cout << "[*] HAL device failed, trying NULLREF..." << std::endl;
hr = pD3D->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_NULLREF,
d3dpp.hDeviceWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pDummyDevice
);
if (FAILED(hr)) {
std::cout << "[-] Failed to create device. HRESULT: 0x" << std::hex << hr << std::dec << std::endl;
pD3D->Release();
return false;
}
}
std::cout << "[+] Dummy device created successfully" << std::endl;
memcpy(pTable, *(void***)pDummyDevice, Size);
pDummyDevice->Release();
pD3D->Release();
return true;
}
void Hook() {
std::cout << "[*] Starting DirectX9 Hook..." << std::endl;
void* d3d9Device[119];
if (!GetD3D9Device(d3d9Device, sizeof(d3d9Device))) {
std::cout << "[-] Failed to get D3D9 device VTable" << std::endl;
return;
}
std::cout << "[+] Got D3D9 VTable" << std::endl;
MH_STATUS status = MH_Initialize();
if (status != MH_OK) {
std::cout << "[-] MinHook initialize failed: " << MH_StatusToString(status) << std::endl;
return;
}
std::cout << "[+] MinHook initialized" << std::endl;
status = MH_CreateHook(d3d9Device[42], &hkEndScene, reinterpret_cast<void**>(&oEndScene));
if (status != MH_OK) {
std::cout << "[-] Failed to create EndScene hook: " << MH_StatusToString(status) << std::endl;
}
status = MH_CreateHook(d3d9Device[17], &hkPresent, reinterpret_cast<void**>(&oPresent));
if (status != MH_OK) {
std::cout << "[-] Failed to create Present hook: " << MH_StatusToString(status) << std::endl;
}
status = MH_CreateHook(d3d9Device[16], &hkReset, reinterpret_cast<void**>(&oReset));
if (status != MH_OK) {
std::cout << "[-] Failed to create Reset hook: " << MH_StatusToString(status) << std::endl;
}
status = MH_EnableHook(MH_ALL_HOOKS);
if (status != MH_OK) {
std::cout << "[-] Failed to enable hooks: " << MH_StatusToString(status) << std::endl;
return;
}
std::cout << "[+] All hooks enabled successfully!" << std::endl;
std::cout << "[*] Press INSERT to toggle menu" << std::endl;
}
void Unhook() {
std::cout << "[*] Unhooking..." << std::endl;
CleanupImGui();
MH_DisableHook(MH_ALL_HOOKS);
MH_Uninitialize();
std::cout << "[+] Unhooked successfully" << std::endl;
}
}