forked from reactos/reactos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
212 lines (185 loc) · 5.59 KB
/
process.c
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
/*
* PROJECT: ReactOS Utility Manager (Accessibility)
* LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
* PURPOSE: Process handling functions
* COPYRIGHT: Copyright 2019 Bișoc George (fraizeraust99 at gmail dot com)
*/
/* INCLUDES *******************************************************************/
#include "precomp.h"
/* FUNCTIONS ******************************************************************/
/**
* @GetProcessID
*
* Returns the process executable ID based on the given executable name.
*
* @param[in] lpProcessName
* The name of the executable process.
*
* @return
* Returns the ID number of the process, otherwise 0.
*
*/
DWORD GetProcessID(IN LPCWSTR lpProcessName)
{
PROCESSENTRY32W Process;
/* Create a snapshot and check if the given process name matches with the one from the process entry structure */
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
return 0;
/* Get the whole size of the structure */
Process.dwSize = sizeof(Process);
/* Enumerate the processes */
if (Process32FirstW(hSnapshot, &Process))
{
do
{
if (_wcsicmp(Process.szExeFile, lpProcessName) == 0)
{
/* The names match, return the process ID we're interested */
CloseHandle(hSnapshot);
return Process.th32ProcessID;
}
}
while (Process32NextW(hSnapshot, &Process));
}
CloseHandle(hSnapshot);
return 0;
}
/**
* @IsProcessRunning
*
* Checks if a process is running.
*
* @param[in] lpProcessName
* The name of the executable process.
*
* @return
* Returns TRUE if the given process' name is running,
* FALSE otherwise.
*
*/
BOOL IsProcessRunning(IN LPCWSTR lpProcessName)
{
DWORD dwExitCode, dwProcessID;
HANDLE hProcess;
/* Get the process ID */
dwProcessID = GetProcessID(lpProcessName);
if (dwProcessID == 0)
{
return FALSE;
}
/* Query some information from the process in question and check the exit code given by the process */
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);
GetExitCodeProcess(hProcess, &dwExitCode);
if (dwExitCode == STILL_ACTIVE)
{
/* The process is still active so it's running */
CloseHandle(hProcess);
return TRUE;
}
CloseHandle(hProcess);
return FALSE;
}
/**
* @LaunchProcess
*
* Executes a process.
*
* @param[in] lpProcessName
* The name of the executable process.
*
* @return
* Returns TRUE if the process has been launched successfully,
* FALSE otherwise.
*
*/
BOOL LaunchProcess(LPCWSTR lpProcessName)
{
STARTUPINFOW si;
PROCESS_INFORMATION pi;
HANDLE hUserToken, hProcessToken;
BOOL bSuccess;
WCHAR ExpandedCmdLine[MAX_PATH];
/* Expand the process path string */
ExpandEnvironmentStringsW(lpProcessName, ExpandedCmdLine, ARRAYSIZE(ExpandedCmdLine));
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
/* Get the token of the parent (current) process of the application */
bSuccess = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &hUserToken);
if (!bSuccess)
{
DPRINT("OpenProcessToken() failed with error -> %lu\n", GetLastError());
return FALSE;
}
/* Duplicate a new token so that we can use it to create our process */
bSuccess = DuplicateTokenEx(hUserToken, TOKEN_ALL_ACCESS, NULL, SecurityIdentification, TokenPrimary, &hProcessToken);
if (!bSuccess)
{
DPRINT("DuplicateTokenEx() failed with error -> %lu\n", GetLastError());
CloseHandle(hUserToken);
return FALSE;
}
/* Finally create the process */
bSuccess = CreateProcessAsUserW(hProcessToken,
NULL,
ExpandedCmdLine,
NULL,
NULL,
FALSE,
0, // DETACHED_PROCESS, NORMAL_PRIORITY_CLASS
NULL,
NULL,
&si,
&pi);
if (!bSuccess)
{
DPRINT("CreateProcessAsUserW() failed with error -> %lu\n", GetLastError());
CloseHandle(hUserToken);
CloseHandle(hProcessToken);
return FALSE;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hUserToken);
CloseHandle(hProcessToken);
return TRUE;
}
/**
* @CloseProcess
*
* Closes a process.
*
* @param[in] lpProcessName
* The name of the executable process.
*
* @return
* Returns TRUE if the process has been terminated successfully,
* FALSE otherwise.
*
*/
BOOL CloseProcess(IN LPCWSTR lpProcessName)
{
HANDLE hProcess;
DWORD dwProcessID;
/* Get the process ID */
dwProcessID = GetProcessID(lpProcessName);
if (dwProcessID == 0)
{
return FALSE;
}
/* Open the process so that we can terminate it */
hProcess = OpenProcess(PROCESS_TERMINATE, 0, dwProcessID);
/* Make sure that we do not terminate ourselves of course */
if ((hProcess != NULL) && (dwProcessID != GetCurrentProcessId()))
{
/* Terminate it */
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
return TRUE;
}
CloseHandle(hProcess);
return FALSE;
}