-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopenglhook.cpp
More file actions
115 lines (85 loc) · 3.32 KB
/
openglhook.cpp
File metadata and controls
115 lines (85 loc) · 3.32 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
#include "openglhook.h"
#include <Windows.h>
#include <iostream>
#include <gl/GL.h>
#include "MinHook.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_opengl3.h"
#include "imgui/imgui_impl_win32.h"
#pragma comment(lib, "opengl32.lib")
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
namespace OpenGL {
typedef BOOL(WINAPI* WglSwapBuffers_t)(HDC hdc);
typedef LRESULT(CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
WglSwapBuffers_t oWglSwapBuffers = 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;
}
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(HDC hdc) {
std::cout << "[*] Initializing ImGui for OpenGL..." << std::endl;
window = WindowFromDC(hdc);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplWin32_Init(window);
ImGui_ImplOpenGL3_Init("#version 130");
oWndProc = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)hkWndProc);
initialized = true;
std::cout << "[+] ImGui initialized successfully for OpenGL" << std::endl;
}
BOOL WINAPI hkWglSwapBuffers(HDC hdc) {
if (!initialized && hdc) {
InitImGui(hdc);
}
if (initialized && showMenu) {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow();
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
return oWglSwapBuffers(hdc);
}
void Hook() {
std::cout << "[*] Starting OpenGL Hook..." << std::endl;
while (!GetModuleHandleA("opengl32.dll")) {
Sleep(100);
}
HMODULE openGL32 = GetModuleHandleA("opengl32.dll");
void* fnWglSwapBuffers = GetProcAddress(openGL32, "wglSwapBuffers");
MH_Initialize();
MH_CreateHook(fnWglSwapBuffers, &hkWglSwapBuffers, reinterpret_cast<void**>(&oWglSwapBuffers));
MH_EnableHook(fnWglSwapBuffers);
std::cout << "[+] OpenGL hook enabled successfully!" << std::endl;
std::cout << "[*] Press HOME to toggle menu" << std::endl;
}
void Unhook() {
std::cout << "[*] Unhooking OpenGL..." << std::endl;
if (initialized) {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
if (window && oWndProc) {
SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)oWndProc);
}
}
MH_DisableHook(MH_ALL_HOOKS);
MH_Uninitialize();
std::cout << "[+] OpenGL unhooked successfully" << std::endl;
}
}