-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathuProcessAndWindowUtils.pas
387 lines (332 loc) · 11.9 KB
/
uProcessAndWindowUtils.pas
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
{
******************************************************
USB Disk Ejector
Copyright (c) 2006 - 2015 Bennyboy
Http://quickandeasysoftware.net
******************************************************
}
{
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit uProcessAndWindowUtils;
interface
uses Windows, Messages, sysutils, JCLSysInfo, JwaWindows, TlHelp32, classes, strutils;
function EnumWindowsAndCloseFunc (Handle: THandle; MountPoint: string): BOOL; stdcall;
function EnumChildWindowsAndCloseFunc (Handle: THandle; DriveString: string): BOOL; stdcall;
function CloseAppsRunningFrom(MountPoint: String; ForceClose: Boolean): boolean;
procedure ListAllHandleNames(DosDeviceName: string; OutList: TStringlist);
procedure FindOpenHandlesTest(MountPoint: string);
implementation
var
TopWindow: hwnd;
{*****************************************************NEW TEST STUFF**********************}
//Match it to the dos device name! check that name with listusbdrives util - get handle or processid and close it!
procedure NTSetPrivilege(Privilege: string; Enabled: Boolean);
var
Token: THandle;
TokenPriv: TOKEN_PRIVILEGES;
PrevTokenPriv: TOKEN_PRIVILEGES;
ReturnLength: Cardinal;
begin
if OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token) then
try
if LookupPrivilegeValue(nil, PChar(Privilege), TokenPriv.Privileges[0].Luid) then
begin
TokenPriv.PrivilegeCount := 1;
case Enabled of
True: TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
False: TokenPriv.Privileges[0].Attributes := 0;
end;
ReturnLength := 0;
PrevTokenPriv := TokenPriv;
AdjustTokenPrivileges(Token, False, @TokenPriv, SizeOf(PrevTokenPriv), @PrevTokenPriv, @ReturnLength);
end;
finally
CloseHandle(Token);
end;
end;
procedure ListAllHandleNames(DosDeviceName: string; OutList: TStringlist);
const
MaximumHandleCount = 100000;
var
Buffer : Pointer;
MemoryNeeded : ULong;
Unused : ULong;
HandleInformation : PSystemHandleInformation;
HandleCount : PULong;
ObjectName : PObjectNameInformation;
LocalHandle : THandle;
ProcessHandle : THandle;
ObjectString : string;
i : Integer;
begin
// Initialisierung
MemoryNeeded := MaximumHandleCount * SizeOf(TSystemHandleInformation);
// Handle Liste holen
GetMem(Buffer, MemoryNeeded);
if not NT_SUCCESS(NtQuerySystemInformation(SystemHandleInformation, Buffer, MemoryNeeded, nil)) then
begin
writeln('NtQuerySystemInformation fehlgeschlagen (Handle Array zu klein?).');
FreeMem(Buffer);
Exit;
end;
HandleCount := Buffer;
HandleInformation := Pointer(LongWord(Buffer) + 4);
for i := 0 to HandleCount^ - 1 do
begin
ProcessHandle := OpenProcess(PROCESS_DUP_HANDLE, false, HandleInformation^.ProcessId);
if ProcessHandle <> 0 then
begin
// Spezielle Named Pipes machen Probleme bei der Abfrage. Diese Pipes haben
// als Access Mask 0x12019F. Entsprechend werden nur Handles abgefragt, die
// nicht 0x12019F als Access Mask haben.
if HandleInformation^.GrantedAccess <> $12019F then
begin
GetMem(ObjectName, 65536);
if DuplicateHandle(ProcessHandle, HandleInformation^.Handle, GetCurrentProcess(), @LocalHandle, 0, false, DUPLICATE_SAME_ACCESS) and (ObjectName^.Name.Buffer <> nil)then
begin
if NT_SUCCESS(NtQueryObject(LocalHandle, ObjectNameInformation, ObjectName, 65536, @Unused)) and (ObjectName^.Name.Buffer <> nil) then
begin
ObjectString := LowerCase(WideString(ObjectName^.Name.Buffer));
//OutputDebugString(pChar((Format('Process: %.8x - Handle: %.8x ObjectName: %s', [HandleInformation^.ProcessId, HandleInformation^.Handle, ObjectString]))));
if AnsiContainsText(ObjectString, DosDeviceName) then
OutList.Add(Format('Process: %.8x - Handle: %.8x ObjectName: %s', [HandleInformation^.ProcessId, HandleInformation^.Handle, ObjectString]))
end;
CloseHandle(LocalHandle);
end;
FreeMem(ObjectName);
end;
CloseHandle(ProcessHandle);
end;
inc(HandleInformation);
end;
FreeMem(Buffer);
end;
procedure FindOpenHandlesTest(MountPoint: string);
var
szDosDeviceName, VolumeName: array[0..MAX_PATH-1] of Char;
funcResult, i: integer;
szDevicePath, szVolumeAccessPath: string;
FoundHandles: TStringList;
begin
GetVolumeNameForVolumeMountPoint(pchar(MountPoint), VolumeName, MAX_PATH);
szDevicePath:=ExcludeTrailingPathDelimiter( VolumeName );
szVolumeAccessPath:=ExcludeTrailingPathDelimiter( VolumeName );
szDevicePath:=Copy(szVolumeAccessPath, 5, length(szVolumeAccessPath) -4);
szDosDeviceName[0]:=#0;
//Get the dos device name
funcResult := QueryDosDevice(PChar(szDevicePath), szDosDeviceName, MAX_PATH);
if funcResult = 0 then
else
OutputDebugString(pchar('OI!!!!!!!!!!!!!!!!!!!******************************************************* ' + szDosDeviceName));
FoundHandles := TStringList.Create;
try
ListAllHandleNames(szDosDeviceName, FoundHandles);
if FoundHandles.count > 0 then
for I := 0 to FoundHandles.Count - 1 do
OutputDebugString( PChar( FoundHandles[i] ));
finally
FoundHandles.Free;
end;
end;
{*******************************************************************************************************************************************************}
{******************Close Explorer Windows For A Specified Drive****************}
function EnumChildWindowsAndCloseFunc(Handle: THandle;
DriveString: string): BOOL;
var
WindowText : array[0.. MAX_PATH - 1] of Char;
FoundPos: integer;
Res: cardinal;
begin
//PostMessage(Handle, WM_GETTEXT, sizeof(WindowText), integer(@WindowText[0]));
windows.SendMessageTimeout(Handle, WM_GETTEXT, SizeOf(WindowText), Cardinal(@WindowText[0]), SMTO_ABORTIFHUNG or SMTO_NORMAL, 500, @Res);
FoundPos:= pos(DriveString, WindowText);
if FoundPos > 0 then
begin
PostMessage (TopWindow, WM_CLOSE, 0, 0);
end;
Result:=true;
end;
function EnumWindowsAndCloseFunc(Handle: THandle;
MountPoint: string): BOOL;
var
WindowHandle: HWND;
WindowName, WindowText: array[0..MAX_PATH] of Char;
FoundPos: integer;
DriveString: string;
Res: cardinal;
begin
//Build the search string
DriveString:= ExcludeTrailingPathDelimiter ( MountPoint );
//Get the window caption
//PostMessage(Handle, WM_GETTEXT, SizeOf(WindowName), cardinal(@WindowName[0]));
{SendMessage sometimes never returned, PostMessage just didnt work a lot of the time, so SendMessageTimeout is the alternative}
windows.SendMessageTimeout(Handle, WM_GETTEXT, SizeOf(WindowName), Cardinal(@WindowName[0]), SMTO_ABORTIFHUNG or SMTO_NORMAL, 500, @Res);
//Look for CabinetWClass in all windows
WindowHandle := FindWindow('CabinetWClass', WindowName);
if WindowHandle > 0 then //Found an explorer window
begin
//Get its caption and see if its got drive letter in it
GetWindowText(WindowHandle, WindowText, MAX_PATH);
FoundPos:= pos(DriveString, WindowText);
if Foundpos > 0 then
begin
PostMessage (WindowHandle, WM_CLOSE, 0, 0);
end;
//Search all its hidden child windows
TopWindow:=WindowHandle;
EnumChildWindows(WindowHandle, @EnumChildWindowsAndCloseFunc, LParam(DriveString));
end;
Result :=True;
end;
{*******************Close Apps Running From A Specified Drive******************}
Function InstanceToWnd( Const TgtPID:DWORD):HWND;
Var
ThisHWnd :HWND;
ThisPID :DWORD;
Begin
Result := 0;
ThisPID := 0;
// Find the first Top Level Window
ThisHWnd := FindWindow( Nil, Nil);
ThisHWnd := GetWindow( ThisHWnd, GW_HWNDFIRST );
While ThisHWnd <> 0 Do
Begin
//Check if the window isn't a child (redundant?)
If GetParent( ThisHWnd ) = 0 Then
Begin
//Get the window's thread & ProcessId
GetWindowThreadProcessId( ThisHWnd, Addr(ThisPID) );
If ThisPID = TgtPID Then
Begin
Result := ThisHWnd;
Break;
End;
End;
// 'retrieve the next window
ThisHWnd := GetWindow( ThisHWnd, GW_HWNDNEXT );
End;
End;
procedure CloseWindowByID(ID: Cardinal);
var
wind: hwnd;
begin
wind:=InstanceToWnd(ID);
if wind <> 0 then
begin
//postMessage (wind, WM_CLOSE, 0, 0);
sendMessage (wind, WM_CLOSE, 0, 0); //wait to return
sleep(3000);
end;
end;
procedure TerminateProcessById(ID: Cardinal);
var
HndProcess : THandle;
begin
HndProcess := OpenProcess(PROCESS_TERMINATE,TRUE, ID);
if HndProcess <> 0 then
try
TerminateProcess(HndProcess,0);
finally
CloseHandle(HndProcess);
end;
end;
function GetProcessFileName(PID: DWORD): string;
var
Handle: THandle;
begin
Result := '';
Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
if Handle <> 0 then
try
SetLength(Result, MAX_PATH);
begin
if GetModuleFileNameEx(Handle, 0, PChar(Result), MAX_PATH) > 0 then
SetLength(Result, StrLen(PChar(Result)))
else
Result := '';
end
finally
CloseHandle(Handle);
end;
end;
function KillAppsFromDrive_NT(DriveString: string; ForceClose: Boolean): Boolean;
const
RsSystemIdleProcess = 'System Idle Process';
RsSystemProcess = 'System Process';
var
SnapProcHandle: THandle;
ProcEntry: TProcessEntry32;
NextProc: Boolean;
FileName: string;
begin
SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
Result := (SnapProcHandle <> INVALID_HANDLE_VALUE);
if Result then
try
ProcEntry.dwSize := SizeOf(ProcEntry);
NextProc := Process32First(SnapProcHandle, ProcEntry);
while NextProc do
begin
if ProcEntry.th32ProcessID = 0 then
begin
// PID 0 is always the "System Idle Process" but this name cannot be
// retrieved from the system and has to be fabricated.
FileName := RsSystemIdleProcess;
end
else
begin
if GetWindowsVersion >= wvWin2000 then //IsWin2k or IsWinXP then
begin
FileName := GetProcessFileName(ProcEntry.th32ProcessID);
if FileName = '' then
FileName := ProcEntry.szExeFile;
end
else
begin
FileName := ProcEntry.szExeFile;
end;
end;
//If running from the drive - then close it
if (ExtractFileDrive(Filename) = DriveString) or (Copy(FileName, 0, Length(DriveString)) = DriveString ) then
if ForceClose then
TerminateProcessById(ProcEntry.th32ProcessID)
else
CloseWindowById(ProcEntry.th32ProcessID);
NextProc := Process32Next(SnapProcHandle, ProcEntry);
end;
finally
CloseHandle(SnapProcHandle);
end;
end;
function CloseAppsRunningFrom(MountPoint: String; ForceClose: Boolean): boolean;
var
DriveString: string;
begin
result:=false;
if GetWindowsVersion < wvWin2000 then exit;
if length(MountPoint) > 1 then
DriveString:=MountPoint
else
if length(MountPoint) = 0 then //If no mountpoint
exit
else
begin
//Driveletter must be upper case
MountPoint:=UpCase(MountPoint[1]);
//Build the search string
DriveString:=MountPoint + ':';
end;
result:=KillAppsFromDrive_NT(DriveString, ForceClose);
end;
end.