ReactOS 0.4.15-dev-7942-gd23573b
process.c File Reference
#include "umandlg.h"
Include dependency graph for process.c:

Go to the source code of this file.

Functions

DWORD GetProcessID (IN LPCWSTR lpszProcessName)
 
BOOL IsProcessRunning (IN LPCWSTR lpszProcessName)
 
BOOL LaunchProcess (IN LPCWSTR lpszProcessName)
 
BOOL CloseProcess (IN LPCWSTR lpszProcessName)
 

Function Documentation

◆ CloseProcess()

BOOL CloseProcess ( IN LPCWSTR  lpszProcessName)

@CloseProcess

Closes a process.

Parameters
[in]lpszProcessNameThe name of the executable process.
Returns
Returns TRUE if the process has been terminated successfully, FALSE otherwise.

Definition at line 192 of file process.c.

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}
DWORD GetProcessID(IN LPCWSTR lpszProcessName)
Definition: process.c:26
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
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
unsigned long DWORD
Definition: ntddk_ex.h:95
#define PROCESS_TERMINATE
Definition: pstypes.h:157
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:71
#define DPRINT
Definition: sndvol32.h:71
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
DWORD WINAPI GetCurrentProcessId(void)
Definition: proc.c:1158

Referenced by DlgProc().

◆ GetProcessID()

DWORD GetProcessID ( IN LPCWSTR  lpszProcessName)

@GetProcessID

Returns the process executable ID based on the given executable name.

Parameters
[in]lpszProcessNameThe name of the executable process.
Returns
Returns the ID number of the process, otherwise 0.

Definition at line 26 of file process.c.

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}
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
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
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define TH32CS_SNAPPROCESS
Definition: tlhelp32.h:26

Referenced by CloseProcess(), and IsProcessRunning().

◆ IsProcessRunning()

BOOL IsProcessRunning ( IN LPCWSTR  lpszProcessName)

@IsProcessRunning

Checks if a process is running.

Parameters
[in]lpszProcessNameThe name of the executable process.
Returns
Returns TRUE if the given process' name is running, FALSE otherwise.

Definition at line 71 of file process.c.

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}
#define WAIT_TIMEOUT
Definition: dderror.h:14
#define SYNCHRONIZE
Definition: nt_native.h:61
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82

Referenced by InitUtilsList(), and ListBoxRefreshContents().

◆ LaunchProcess()

BOOL LaunchProcess ( IN LPCWSTR  lpszProcessName)

@LaunchProcess

Executes a process.

Parameters
[in]lpProcessNameThe name of the executable process.
Returns
Returns TRUE if the process has been launched successfully, FALSE otherwise.

Definition at line 117 of file process.c.

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}
HANDLE hUserToken
Definition: install.c:39
#define NULL
Definition: types.h:112
#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 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
static BOOLEAN bSuccess
Definition: drive.cpp:433
unsigned int BOOL
Definition: ntddk_ex.h:94
@ SecurityIdentification
Definition: lsa.idl:56
@ TokenPrimary
Definition: imports.h:273
static refpint_t pi[]
Definition: server.c:96
DWORD cb
Definition: winbase.h:852
DWORD dwFlags
Definition: winbase.h:863
WORD wShowWindow
Definition: winbase.h:864
#define ZeroMemory
Definition: winbase.h:1712
#define STARTF_USESHOWWINDOW
Definition: winbase.h:491
#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

Referenced by DlgProc().