-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathProcessOpen.ahk
227 lines (190 loc) · 11 KB
/
ProcessOpen.ahk
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
; Link: https://www.autohotkey.com/boards/viewtopic.php?f=42&t=66232&hilit=nested+class
; Author:
; Date:
; for: AHK_L
/*
*/
/*
Opens a handle to an existing local process object and sets the access rights to this object.
Parameters:
Process:
The identifier or name of the local process to be opened.
If the specified process is the System Process (0), the function fails and the last error code is 0x00000057 (ERROR_INVALID_PARAMETER).
If the specified process is the Idle process or one of the CSRSS processes, this function fails and the last error code is 0x00000005 (ERROR_ACCESS_DENIED) because their access restrictions prevent user-level code from opening them.
If the specified process is the current process (-1), a "real" handle to itself is created.
DesiredAccess:
The access to the process object.
This access right is checked against the security descriptor for the process.
This parameter can be one or more of the process access rights: https://docs.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights.
If the caller has enabled the SeDebugPrivilege privilege, the requested access is granted regardless of the contents of the security descriptor.
0x1F0FFF PROCESS_ALL_ACCESS
0x000040 PROCESS_DUP_HANDLE
0x000400 PROCESS_QUERY_INFORMATION
0x001000 PROCESS_QUERY_LIMITED_INFORMATION
0x000200 PROCESS_SET_INFORMATION
0x000100 PROCESS_SET_QUOTA
0x000800 PROCESS_SUSPEND_RESUME
0x000001 PROCESS_TERMINATE
0x000008 PROCESS_VM_OPERATION
0x000010 PROCESS_VM_READ
0x000020 PROCESS_VM_WRITE
0x100000 SYNCHRONIZE
Attributes:
0x00 The processes created by this process do not inherit the handle.
0x02 The processes created by this process will inherit the handle.
Return value:
If the function succeeds, the return value is an IProcess class object instance.
If the function fails, the return value is zero. To get extended error information, check A_LastError (NTSTATUS).
Remarks:
To open a handle to another local process and obtain full access rights, you must enable the SeDebugPrivilege privilege.
When all references to the returned object are released, the process handle will be closed.
*/
ProcessOpen(Process := -1, DesiredAccess := 0x1F0FFF, Attributes := 0)
{
Process := Process == -1 ? ProcessExist() ; Current process.
: Type(Process) == "Integer" ? Process ; Process identifier.
: ProcessExist(Process) ; Process name.
return new IProcess(Process, DesiredAccess, Attributes)
}
class IProcess
{
; ===================================================================================================================
; INSTANCE VARIABLES
; ===================================================================================================================
Handle := 0
CloseHandle := TRUE
; ===================================================================================================================
; CONSTRUCTOR
; ===================================================================================================================
__New(ProcessId, DesiredAccess, Attributes := 0, SecurityDescriptor := 0)
{
local OBJECT_ATTRIBUTES := BufferAlloc(6*A_PtrSize) ; https://docs.microsoft.com/en-us/windows/win32/api/ntdef/ns-ntdef-_object_attributes.
NumPut("UInt", OBJECT_ATTRIBUTES.Size, OBJECT_ATTRIBUTES) ; ULONG Length.
NumPut("UPtr", 0 ; HANDLE RootDirectory.
, "UPtr", 0 ; PUNICODE_STRING ObjectName.
, "UInt", Attributes, OBJECT_ATTRIBUTES, A_PtrSize) ; ULONG Attributes. OBJ_INHERIT = 2 (InheritHandle).
NumPut("UPtr", SecurityDescriptor ; PVOID SecurityDescriptor.
, "UPtr", 0, OBJECT_ATTRIBUTES, 4*A_PtrSize) ; PVOID SecurityQualityOfService.
local CLIENT_ID := BufferAlloc(2*A_PtrSize) ; https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-tsts/a11e7129-685b-4535-8d37-21d4596ac057.
NumPut("UPtr", ProcessId ; HANDLE UniqueProcess.
, "UPtr", 0, CLIENT_ID) ; HANDLE UniqueThread.
local hProcess := 0 ; Unique process identifier.
local NtStatus := DllCall("Ntdll.dll\NtOpenProcess", "UPtrP", hProcess ; (out)PHANDLE ProcessHandle.
, "UInt", DesiredAccess ; ACCESS_MASK DesiredAccess.
, "UPtr", OBJECT_ATTRIBUTES.Ptr ; POBJECT_ATTRIBUTES ObjectAttributes.
, "UPtr", CLIENT_ID.Ptr ; PCLIENT_ID ClientId.
, "UInt") ; NTSYSAPI NTSTATUS NTAPI.
if (hProcess == 0)
return !(A_LastError := NtStatus)
this.Handle := hProcess
} ; https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/ntddk/nf-ntddk-ntopenprocess
; ===================================================================================================================
; NESTED CLASSES
; ===================================================================================================================
class FromHandle extends IProcess
{
; ===================================================================================================================
; CONSTRUCTOR
; ===================================================================================================================
__New(hProcess)
{
; Checks if the handle is valid.
this.Handle := ProcessCheckHandle(hProcess)
if (this.Handle == 0)
return 0
}
}
; ===================================================================================================================
; DESTRUCTOR
; ===================================================================================================================
__Delete()
{
if (this.Handle && this.CloseHandle)
DllCall("Kernel32.dll\CloseHandle", "Ptr", this.Handle)
} ; https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
; ===================================================================================================================
; PUBLIC METHODS
; ===================================================================================================================
/*
Retrieves the termination status of this process.
The handle must have the PROCESS_QUERY_LIMITED_INFORMATION access right.
Return value:
If the function succeeds, the return value is the process termination status.
If the function fails, the return value is <0. To get extended error information, check A_LastError (WIN32).
*/
GetExitCode()
{
local ExitCode := 0
return DllCall("Kernel32.dll\GetExitCodeProcess", "Ptr", this.Handle, "UIntP", ExitCode)
? ExitCode : -A_LastError
} ; https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess
/*
Terminates this process and all of its threads.
The handle must have the PROCESS_TERMINATE access right.
Parameters:
ExitCode:
The exit code to be used by the process and threads terminated as a result of this call.
Return value:
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, check A_LastError (NTSTATUS).
*/
Terminate(ExitCode := 0)
{
A_LastError := DllCall("Ntdll.dll\NtTerminateProcess", "UPtr", this.Handle
, "UInt", ExitCode
, "UInt")
return A_LastError == 0 ? TRUE : FALSE
} ; https://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FProcess%2FNtTerminateProcess.html
}
/*
Opens the next process in relation to the specified process.
Parameters:
Process:
A handle to the process.
This parameter can be zero to start from the first process.
DesiredAccess / HandleAttributes:
See the ProcessOpen function.
Return value:
If the function succeeds, the return value is an IProcess class object.
If the function fails, the return value is zero. To get extended error information, check A_LastError (NTSTATUS).
*/
ProcessGetNext(Process, DesiredAccess, HandleAttributes := 0, Flags := 0)
{
local hProcess := 0
A_LastError := DllCall("Ntdll.dll\NtGetNextProcess", "UPtr", IsObject(Process) ? Process.Handle : Process
, "UInt", DesiredAccess
, "UInt", HandleAttributes
, "UInt", Flags
, "UPtrP", hProcess
, "UInt")
return hProcess ? new IProcess.FromHandle(hProcess) : 0
}
/*
Checks whether the specified handle belongs to a process.
Parameters:
Handle:
The handle to be checked.
Return value:
If the handle is valid, the return value is the process handle. Otherwise, it is zero.
*/
ProcessCheckHandle(Handle)
{
return DllCall("Kernel32.dll\GetExitCodeProcess", "UPtr", Handle := Integer(IsObject(Handle)?Handle.Handle:Handle)
, "UIntP", 0)
|| A_LastError !== 0x00000006 ; ERROR_INVALID_HANDLE = 0x00000006.
? Handle : 0
}
/*
Retrieves the process identifier of the specified process.
Parameters:
A handle to the process.
The handle must have the PROCESS_QUERY_LIMITED_INFORMATION access right.
Return value:
If the handle is valid and the process still exists, the return value is the process identifier. Otherwise, it is zero.
Remarks:
Until a process terminates, its process identifier uniquely identifies it on the system.
*/
ProcessGetID(Process)
{
return DllCall("Kernel32.dll\GetProcessId", "Ptr", IsObject(Process)?Process.Handle:Process, "UInt")
} ; https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocessid