-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSystemHooks.dpr
217 lines (181 loc) · 5.69 KB
/
SystemHooks.dpr
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
library SystemHooks;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
{$ifdef win64}
{$LIBSUFFIX '64'}
{$endif}
uses
Windows,
Messages,
ActiveX,
Classes;
const
MemMapFile = 'MonitorXettings';
WM_USER = $0400;
WM_COPYDATA = $004A;
WM_SHELLEVENT = WM_USER + 11;
type
PDLLGlobal = ^TDLLGLobal;
TDLLGlobal = packed record
HookHandle: HHOOK;
// HookCBTHandle: NativeUInt;
end;
//// Message structure for WH_CALLWNDPROCRET
// LPCWPRETSTRUCT = ^CWPRETSTRUCT;
// tagCWPRETSTRUCT = record
// lResult: LRESULT;
// lParam: LPARAM;
// wParam: WPARAM;
// message: UINT;
// hwnd: HWND;
// end;
// CWPRETSTRUCT = tagCWPRETSTRUCT;
// NPCWPRETSTRUCT = ^CWPRETSTRUCT;
// TCwpRetStruct = CWPRETSTRUCT;
// PCwpRetStruct = LPCWPRETSTRUCT;
var
GlobalData: PDLLGlobal;
MMF: THandle;
OwnerHandle: HWND;
{$R *.res}
function ShellProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
var
LHWindow: HWND;
LHFullScreen: BOOL;
vRect: PRect;
ParentHandle: HWND;
begin
if nCode < 0 then
begin
Result := CallNextHookEx(GlobalData^.HookHandle, nCode, wParam, lParam);
Exit;
end
else
begin
case nCode of
// a top level window has been activated
HSHELL_WINDOWACTIVATED, HSHELL_REDRAW, HSHELL_GETMINRECT, HSHELL_WINDOWCREATED, HSHELL_WINDOWDESTROYED:
begin
// if IsWindow(LHWindow) then
begin
ParentHandle := FindWindow('MonitorXettingsHwnd', nil);
if ParentHandle <> 0 then
SendMessageTimeout(ParentHandle, WM_SHELLEVENT, wParam, lParam, SMTO_ABORTIFHUNG or SMTO_NORMAL, 500, nil);
end;
end;
// a window is being maximized or minimized
// HSHELL_GETMINRECT:
// begin
// // LHWindow := wParam; // its handle
// // vRect := @lParam; // its rect pointer
// // SendMessageTimeout(OwnerHandle, WM_SHELLEVENT, wParam, lParam, SMTO_ABORTIFHUNG or SMTO_NORMAL, 500, nil);
// end;
end;
end;
Result := CallNextHookEx(GlobalData^.HookHandle, nCode, wParam, lParam);
end;
//function WndProcRet(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
//var
// LHWindow: HWND;
// LHFullScreen: BOOL;
// vRect: PRect;
// ParentHandle: HWND;
// pInfo: LPCWPRETSTRUCT;
//begin
//
// if nCode < 0 then
// begin
// Result := CallNextHookEx(GlobalData^.HookCBTHandle, nCode, wParam, lParam);
// Exit;
// end
// else
// begin
//
// case nCode of
// // a top level window has been activated
//// HC_ACTION:
// HCBT_MOVESIZE:
// begin
//// pInfo := LPCWPRETSTRUCT(lParam);
//// if pInfo.message = WM_WINDOWPOSCHANGED then
// begin
// ParentHandle := FindWindow('MonitorXettingsHwnd', nil);
// if ParentHandle <> 0 then
// SendMessageTimeout(ParentHandle, WM_SHELLEVENT, wParam, lParam, SMTO_ABORTIFHUNG or SMTO_NORMAL, 500, nil);
// end;
// end;
// end;
// end;
//
// Result := CallNextHookEx(GlobalData^.HookCBTHandle, nCode, wParam, lParam);
//end;
//procedure WinEventProc(hWinEventHook: NativeUInt; dwEvent: DWORD; handle: HWND;
// idObject, idChild: LONG; dwEventThread, dwmsEventTime: DWORD);
//var
// LHWindow: HWND;
// LHFullScreen: BOOL;
// vRect: PRect;
// ParentHandle: HWND;
// pInfo: LPCWPRETSTRUCT;
//begin
// //if dwEvent = EVENT_SYSTEM_MOVESIZEEND then
// if dwEvent = EVENT_OBJECT_LOCATIONCHANGE then
// begin
// ParentHandle := FindWindow('MonitorXettingsHwnd', nil);
// if ParentHandle <> 0 then
// SendMessageTimeout(ParentHandle, WM_SHELLEVENT, wParam(0), lParam(0), SMTO_ABORTIFHUNG or SMTO_NORMAL, 500, nil);
// end;
//end;
function RunHook(AHandle: HWND):BOOL; stdcall;
begin
Result := False;
OwnerHandle := AHandle;
GlobalData^.HookHandle := SetWindowsHookEx(WH_SHELL, @ShellProc, HInstance, 0);
if GlobalData^.HookHandle = INVALID_HANDLE_VALUE then Exit;
// if GlobalData^.HookCBTHandle = INVALID_HANDLE_VALUE then Exit;
Result := True;
end;
function KillHook:BOOL; stdcall;
begin
Result := False;
if (GlobalData <> nil) and (GlobalData^.HookHandle <> INVALID_HANDLE_VALUE) then
begin
Result := UnhookWindowsHookEx(GlobalData^.HookHandle);
if not Result then // try once more
Result := UnhookWindowsHookEx(GlobalData^.HookHandle);
end;
end;
procedure CreateGlobalHeap;
begin
MMF := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, SizeOf(TDLLGlobal), MemMapFile);
if MMF = 0 then Exit;
GlobalData := MapViewOfFile(MMF, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TDLLGlobal));
if GlobalData = nil then
CloseHandle(MMF);
end;
procedure DeleteGlobalHeap;
begin
if GlobalData <> nil then UnmapViewOfFile(GlobalData);
if MMF <> INVALID_HANDLE_VALUE then CloseHandle(MMF);
end;
procedure DLLEntry(dwReason: DWORD);
begin
case dwReason of
DLL_PROCESS_ATTACH: CreateGlobalHeap;
DLL_PROCESS_DETACH: DeleteGlobalHeap;
end;
end;
exports
RunHook,
KillHook;
begin
DllProc := @DllEntry;
DLLEntry(DLL_PROCESS_ATTACH);
end.