ReactOS 0.4.15-dev-7788-g1ad9096
process.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Utility Manager Resources DLL (UManDlg.dll)
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Process handling functions
5 * COPYRIGHT: Copyright 2019-2020 George Bișoc (george.bisoc@reactos.org)
6 */
7
8/* INCLUDES *******************************************************************/
9
10#include "umandlg.h"
11
12/* FUNCTIONS ******************************************************************/
13
26DWORD GetProcessID(IN LPCWSTR lpszProcessName)
27{
29
30 /* Create a snapshot and check if the given process name matches with the one from the process entry structure */
32
33 if (hSnapshot == INVALID_HANDLE_VALUE)
34 return 0;
35
36 /* Get the whole size of the structure */
37 Process.dwSize = sizeof(Process);
38
39 /* Enumerate the processes */
40 if (Process32FirstW(hSnapshot, &Process))
41 {
42 do
43 {
44 if (_wcsicmp(Process.szExeFile, lpszProcessName) == 0)
45 {
46 /* The names match, return the process ID we're interested */
47 CloseHandle(hSnapshot);
48 return Process.th32ProcessID;
49 }
50 }
51 while (Process32NextW(hSnapshot, &Process));
52 }
53
54 CloseHandle(hSnapshot);
55 return 0;
56}
57
72{
73 DWORD dwReturn, dwProcessID;
75
76 /* Get the process ID */
77 dwProcessID = GetProcessID(lpszProcessName);
78 if (dwProcessID == 0)
79 {
80 return FALSE;
81 }
82
83 /* Synchronize the process to get its signaling state */
84 hProcess = OpenProcess(SYNCHRONIZE, FALSE, dwProcessID);
85 if (!hProcess)
86 {
87 DPRINT("IsProcessRunning(): Failed to open the process! (Error: %lu)\n", GetLastError());
88 return FALSE;
89 }
90
91 /* Wait for the process */
92 dwReturn = WaitForSingleObject(hProcess, 0);
93 if (dwReturn == WAIT_TIMEOUT)
94 {
95 /* The process is still running */
97 return TRUE;
98 }
99
101 return FALSE;
102}
103
117BOOL LaunchProcess(IN LPCWSTR lpszProcessName)
118{
119 STARTUPINFOW si;
121 HANDLE hUserToken, hProcessToken;
123 WCHAR ExpandedCmdLine[MAX_PATH];
124
125 /* Expand the process path string */
126 ExpandEnvironmentStringsW(lpszProcessName, ExpandedCmdLine, ARRAYSIZE(ExpandedCmdLine));
127
128 ZeroMemory(&pi, sizeof(pi));
129 ZeroMemory(&si, sizeof(si));
130 si.cb = sizeof(si);
133
134 /* Get the token of the parent (current) process of the application */
136 if (!bSuccess)
137 {
138 DPRINT("OpenProcessToken() failed with error -> %lu\n", GetLastError());
139 return FALSE;
140 }
141
142 /* Duplicate a new token so that we can use it to create our process */
144 if (!bSuccess)
145 {
146 DPRINT("DuplicateTokenEx() failed with error -> %lu\n", GetLastError());
148 return FALSE;
149 }
150
151 /* Finally create the process */
152 bSuccess = CreateProcessAsUserW(hProcessToken,
153 NULL,
154 ExpandedCmdLine,
155 NULL,
156 NULL,
157 FALSE,
158 0, // DETACHED_PROCESS, NORMAL_PRIORITY_CLASS
159 NULL,
160 NULL,
161 &si,
162 &pi);
163
164 if (!bSuccess)
165 {
166 DPRINT("CreateProcessAsUserW() failed with error -> %lu\n", GetLastError());
168 CloseHandle(hProcessToken);
169 return FALSE;
170 }
171
172 CloseHandle(pi.hProcess);
173 CloseHandle(pi.hThread);
175 CloseHandle(hProcessToken);
176 return TRUE;
177}
178
192BOOL CloseProcess(IN LPCWSTR lpszProcessName)
193{
195 DWORD dwProcessID;
196
197 /* Get the process ID */
198 dwProcessID = GetProcessID(lpszProcessName);
199 if (dwProcessID == 0)
200 {
201 return FALSE;
202 }
203
204 /* Make sure that the given process ID is not ours, the parent process, so that we do not kill ourselves */
205 if (dwProcessID == GetCurrentProcessId())
206 {
207 return FALSE;
208 }
209
210 /* Open the process so that we can terminate it */
212 if (!hProcess)
213 {
214 DPRINT("CloseProcess(): Failed to open the process for termination! (Error: %lu)\n", GetLastError());
215 return FALSE;
216 }
217
218 /* Terminate it */
221 return TRUE;
222}
BOOL CloseProcess(IN LPCWSTR lpszProcessName)
Definition: process.c:192
BOOL IsProcessRunning(IN LPCWSTR lpszProcessName)
Definition: process.c:71
DWORD GetProcessID(IN LPCWSTR lpszProcessName)
Definition: process.c:26
BOOL LaunchProcess(IN LPCWSTR lpszProcessName)
Definition: process.c:117
HANDLE hUserToken
Definition: install.c:39
#define WAIT_TIMEOUT
Definition: dderror.h:14
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
BOOL WINAPI DECLSPEC_HOTPATCH CreateProcessAsUserW(_In_opt_ HANDLE hToken, _In_opt_ LPCWSTR lpApplicationName, _Inout_opt_ LPWSTR lpCommandLine, _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, _In_ BOOL bInheritHandles, _In_ DWORD dwCreationFlags, _In_opt_ LPVOID lpEnvironment, _In_opt_ LPCWSTR lpCurrentDirectory, _In_ LPSTARTUPINFOW lpStartupInfo, _Out_ LPPROCESS_INFORMATION lpProcessInformation)
Definition: logon.c:993
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:294
BOOL WINAPI DuplicateTokenEx(IN HANDLE ExistingTokenHandle, IN DWORD dwDesiredAccess, IN LPSECURITY_ATTRIBUTES lpTokenAttributes OPTIONAL, IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, IN TOKEN_TYPE TokenType, OUT PHANDLE DuplicateTokenHandle)
Definition: security.c:3859
#define CloseHandle
Definition: compat.h:739
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GetCurrentProcess()
Definition: compat.h:759
#define MAX_PATH
Definition: compat.h:34
DWORD WINAPI ExpandEnvironmentStringsW(IN LPCWSTR lpSrc, IN LPWSTR lpDst, IN DWORD nSize)
Definition: environ.c:519
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1532
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1227
BOOL WINAPI Process32NextW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
Definition: toolhelp.c:1073
HANDLE WINAPI CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID)
Definition: toolhelp.c:1255
BOOL WINAPI Process32FirstW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
Definition: toolhelp.c:984
static BOOLEAN bSuccess
Definition: drive.cpp:433
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
#define PROCESS_TERMINATE
Definition: pstypes.h:157
@ SecurityIdentification
Definition: lsa.idl:56
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:71
@ TokenPrimary
Definition: imports.h:273
static refpint_t pi[]
Definition: server.c:96
#define SYNCHRONIZE
Definition: nt_native.h:61
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define DPRINT
Definition: sndvol32.h:71
DWORD cb
Definition: winbase.h:852
DWORD dwFlags
Definition: winbase.h:863
WORD wShowWindow
Definition: winbase.h:864
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define TH32CS_SNAPPROCESS
Definition: tlhelp32.h:26
#define IN
Definition: typedefs.h:39
#define ZeroMemory
Definition: winbase.h:1712
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define STARTF_USESHOWWINDOW
Definition: winbase.h:491
DWORD WINAPI GetCurrentProcessId(void)
Definition: proc.c:1158
#define SW_SHOWNORMAL
Definition: winuser.h:770
#define TOKEN_DUPLICATE
Definition: setypes.h:926
#define TOKEN_QUERY
Definition: setypes.h:928
#define TOKEN_ALL_ACCESS
Definition: setypes.h:946
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185