ReactOS  0.4.15-dev-489-g75a0787
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 BiČ™oc George (fraizeraust99 at gmail dot com)
6  */
7 
8 /* INCLUDES *******************************************************************/
9 
10 #include "umandlg.h"
11 
12 /* FUNCTIONS ******************************************************************/
13 
26 DWORD 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 
71 BOOL IsProcessRunning(IN LPCWSTR lpszProcessName)
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)", 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 
117 BOOL LaunchProcess(IN LPCWSTR lpszProcessName)
118 {
119  STARTUPINFOW si;
121  HANDLE hUserToken, hProcessToken;
122  BOOL bSuccess;
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 
192 BOOL 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 */
211  hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID);
212  if (!hProcess)
213  {
214  DPRINT("CloseProcess(): Failed to open the process for termination! (Error: %lu)", GetLastError());
215  return FALSE;
216  }
217 
218  /* Terminate it */
221  return TRUE;
222 }
#define IN
Definition: typedefs.h:39
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:407
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
BOOL CloseProcess(IN LPCWSTR lpszProcessName)
Definition: process.c:192
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define INVALID_HANDLE_VALUE
Definition: compat.h:400
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ZeroMemory
Definition: winbase.h:1648
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
static BOOLEAN bSuccess
Definition: drive.cpp:419
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL LaunchProcess(IN LPCWSTR lpszProcessName)
Definition: process.c:117
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
BOOL WINAPI Process32FirstW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
Definition: toolhelp.c:984
#define STARTF_USESHOWWINDOW
Definition: winbase.h:472
HANDLE hUserToken
Definition: install.c:39
DWORD cb
Definition: winbase.h:830
#define TOKEN_QUERY
Definition: setypes.h:874
__wchar_t WCHAR
Definition: xmlstorage.h:180
static refpint_t pi[]
Definition: server.c:96
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1138
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define TOKEN_ALL_ACCESS
Definition: setypes.h:892
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:3394
#define WAIT_TIMEOUT
Definition: dderror.h:14
#define TOKEN_DUPLICATE
Definition: setypes.h:872
BOOL WINAPI Process32NextW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
Definition: toolhelp.c:1073
WORD wShowWindow
Definition: winbase.h:842
#define SYNCHRONIZE
Definition: nt_native.h:61
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:281
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1227
#define SW_SHOWNORMAL
Definition: winuser.h:764
#define PROCESS_TERMINATE
Definition: pstypes.h:149
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1532
#define TH32CS_SNAPPROCESS
Definition: tlhelp32.h:26
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:296
DWORD WINAPI ExpandEnvironmentStringsW(IN LPCWSTR lpSrc, IN LPWSTR lpDst, IN DWORD nSize)
Definition: environ.c:519
DWORD GetProcessID(IN LPCWSTR lpszProcessName)
Definition: process.c:26
HANDLE WINAPI CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID)
Definition: toolhelp.c:1255
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
BOOL IsProcessRunning(IN LPCWSTR lpszProcessName)
Definition: process.c:71
DWORD dwFlags
Definition: winbase.h:841
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1158