-
Notifications
You must be signed in to change notification settings - Fork 0
/
wndhookdll.cc
90 lines (70 loc) · 2.2 KB
/
wndhookdll.cc
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
#define UNICODE
#include <iostream>
#include <windows.h>
#include "wndhookdll.h"
HHOOK hookWnd __attribute__ ((section(".hook"))) = 0;
HHOOK hookShell __attribute__ ((section(".hook"))) = 0;
HINSTANCE hInst;
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdReason, LPVOID lpReserved) {
switch (fdReason) {
case DLL_PROCESS_ATTACH:
hInst = hInstDLL;
break;
}
return TRUE;
}
LRESULT CALLBACK CallWndProc(int code, WPARAM wParam, LPARAM lParam) {
if (code < 0)
return CallNextHookEx(hookWnd, code, wParam, lParam);
else if (code == HC_ACTION) {
CWPSTRUCT* tmp = reinterpret_cast<CWPSTRUCT*>(lParam);
UINT message = tmp->message;
WPARAM wP = tmp->wParam;
LPARAM lP = tmp->lParam;
if (message == WM_CLOSE) {
auto hwndTarget = FindWindow(L"WintileClass", NULL);
if (hwndTarget != NULL)
PostMessage(hwndTarget, WM_APP, wP, message);
}
if (message == WM_MOUSEACTIVATE) {
auto hwndTarget = FindWindow(L"WintileClass", NULL);
if (hwndTarget != NULL)
PostMessage(hwndTarget, WM_MOUSEACTIVATE, wP, lP);
}
}
return CallNextHookEx(hookWnd, code, wParam, lParam);
}
LRESULT CALLBACK ShellProc(int code, WPARAM wParam, LPARAM lParam) {
if (code < 0)
return CallNextHookEx(hookShell, code, wParam, lParam);
else if (code == HSHELL_WINDOWCREATED) {
auto hwndTarget = FindWindow(L"WintileClass", NULL);
if (hwndTarget != NULL)
PostMessage(hwndTarget, WM_APP, wParam, code);
}
return CallNextHookEx(hookShell, code, wParam, lParam);
}
DLLAPI bool start_wnd_hook() {
hookWnd = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, hInst, 0);
hookShell = SetWindowsHookEx(WH_SHELL, ShellProc, hInst, 0);
if (hookWnd == NULL) {
std::cout << "hookWnd is NULL" << std::endl;
return false;
}
if (hookShell == NULL) {
std::cout << "hookShell is NULL" << std::endl;
return false;
}
return true;
}
DLLAPI bool stop_wnd_hook() {
if (UnhookWindowsHookEx(hookWnd) == 0) {
std::cout << "Failed to unhook hookWnd" << std::endl;
return false;
}
if (UnhookWindowsHookEx(hookShell) == 0) {
std::cout << "Failed to unhook hookShell" << std::endl;
return false;
}
return true;
}