2019-04-24 14:06:05 +00:00
|
|
|
/*
|
2020-04-17 10:42:47 +00:00
|
|
|
* PROJECT: ReactOS Utility Manager Resources DLL (UManDlg.dll)
|
2019-04-24 14:06:05 +00:00
|
|
|
* LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
|
|
|
|
* PURPOSE: Process handling functions
|
2020-04-17 10:42:47 +00:00
|
|
|
* COPYRIGHT: Copyright 2019-2020 Bișoc George (fraizeraust99 at gmail dot com)
|
2019-04-24 14:06:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
2020-04-17 10:42:47 +00:00
|
|
|
#include "umandlg.h"
|
2019-04-24 14:06:05 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS ******************************************************************/
|
|
|
|
|
|
|
|
/**
|
2020-02-27 17:45:21 +00:00
|
|
|
* @GetProcessID
|
2019-04-24 14:06:05 +00:00
|
|
|
*
|
2020-02-27 17:45:21 +00:00
|
|
|
* Returns the process executable ID based on the given executable name.
|
2019-04-24 14:06:05 +00:00
|
|
|
*
|
2020-04-26 12:14:17 +00:00
|
|
|
* @param[in] lpszProcessName
|
2019-04-24 14:06:05 +00:00
|
|
|
* The name of the executable process.
|
|
|
|
*
|
|
|
|
* @return
|
2020-02-27 17:45:21 +00:00
|
|
|
* Returns the ID number of the process, otherwise 0.
|
2019-04-24 14:06:05 +00:00
|
|
|
*
|
|
|
|
*/
|
2020-04-26 12:14:17 +00:00
|
|
|
DWORD GetProcessID(IN LPCWSTR lpszProcessName)
|
2019-04-24 14:06:05 +00:00
|
|
|
{
|
2020-02-27 17:45:21 +00:00
|
|
|
PROCESSENTRY32W Process;
|
2019-04-24 14:06:05 +00:00
|
|
|
|
2020-02-27 17:45:21 +00:00
|
|
|
/* Create a snapshot and check if the given process name matches with the one from the process entry structure */
|
2019-04-24 14:06:05 +00:00
|
|
|
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
|
|
|
|
if (hSnapshot == INVALID_HANDLE_VALUE)
|
2020-02-27 17:45:21 +00:00
|
|
|
return 0;
|
2019-04-24 14:06:05 +00:00
|
|
|
|
2020-02-27 17:45:21 +00:00
|
|
|
/* Get the whole size of the structure */
|
2019-04-24 14:06:05 +00:00
|
|
|
Process.dwSize = sizeof(Process);
|
|
|
|
|
|
|
|
/* Enumerate the processes */
|
|
|
|
if (Process32FirstW(hSnapshot, &Process))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2020-04-26 12:14:17 +00:00
|
|
|
if (_wcsicmp(Process.szExeFile, lpszProcessName) == 0)
|
2019-04-24 14:06:05 +00:00
|
|
|
{
|
2020-02-27 17:45:21 +00:00
|
|
|
/* The names match, return the process ID we're interested */
|
|
|
|
CloseHandle(hSnapshot);
|
|
|
|
return Process.th32ProcessID;
|
2019-04-24 14:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (Process32NextW(hSnapshot, &Process));
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(hSnapshot);
|
2020-02-27 17:45:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsProcessRunning
|
|
|
|
*
|
|
|
|
* Checks if a process is running.
|
|
|
|
*
|
2020-04-26 12:14:17 +00:00
|
|
|
* @param[in] lpszProcessName
|
2020-02-27 17:45:21 +00:00
|
|
|
* The name of the executable process.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Returns TRUE if the given process' name is running,
|
|
|
|
* FALSE otherwise.
|
|
|
|
*
|
|
|
|
*/
|
2020-04-26 12:14:17 +00:00
|
|
|
BOOL IsProcessRunning(IN LPCWSTR lpszProcessName)
|
2020-02-27 17:45:21 +00:00
|
|
|
{
|
|
|
|
DWORD dwReturn, dwProcessID;
|
|
|
|
HANDLE hProcess;
|
|
|
|
|
|
|
|
/* Get the process ID */
|
2020-04-26 12:14:17 +00:00
|
|
|
dwProcessID = GetProcessID(lpszProcessName);
|
2020-02-27 17:45:21 +00:00
|
|
|
if (dwProcessID == 0)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Synchronize the process to get its signaling state */
|
|
|
|
hProcess = OpenProcess(SYNCHRONIZE, FALSE, dwProcessID);
|
|
|
|
if (!hProcess)
|
|
|
|
{
|
|
|
|
DPRINT("IsProcessRunning(): Failed to open the process! (Error: %lu)", GetLastError());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the process */
|
|
|
|
dwReturn = WaitForSingleObject(hProcess, 0);
|
|
|
|
if (dwReturn == WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
/* The process is still running */
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
return FALSE;
|
2019-04-24 14:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
*/
|
2020-04-26 12:14:17 +00:00
|
|
|
BOOL LaunchProcess(IN LPCWSTR lpszProcessName)
|
2019-04-24 14:06:05 +00:00
|
|
|
{
|
|
|
|
STARTUPINFOW si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
HANDLE hUserToken, hProcessToken;
|
|
|
|
BOOL bSuccess;
|
|
|
|
WCHAR ExpandedCmdLine[MAX_PATH];
|
|
|
|
|
|
|
|
/* Expand the process path string */
|
2020-04-26 12:14:17 +00:00
|
|
|
ExpandEnvironmentStringsW(lpszProcessName, ExpandedCmdLine, ARRAYSIZE(ExpandedCmdLine));
|
2019-04-24 14:06:05 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2020-04-26 12:14:17 +00:00
|
|
|
* @param[in] lpszProcessName
|
2019-04-24 14:06:05 +00:00
|
|
|
* The name of the executable process.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Returns TRUE if the process has been terminated successfully,
|
|
|
|
* FALSE otherwise.
|
|
|
|
*
|
|
|
|
*/
|
2020-04-26 12:14:17 +00:00
|
|
|
BOOL CloseProcess(IN LPCWSTR lpszProcessName)
|
2019-04-24 14:06:05 +00:00
|
|
|
{
|
2020-02-27 17:45:21 +00:00
|
|
|
HANDLE hProcess;
|
|
|
|
DWORD dwProcessID;
|
2019-04-24 14:06:05 +00:00
|
|
|
|
2020-02-27 17:45:21 +00:00
|
|
|
/* Get the process ID */
|
2020-04-26 12:14:17 +00:00
|
|
|
dwProcessID = GetProcessID(lpszProcessName);
|
2020-02-27 17:45:21 +00:00
|
|
|
if (dwProcessID == 0)
|
|
|
|
{
|
2019-04-24 14:06:05 +00:00
|
|
|
return FALSE;
|
2020-02-27 17:45:21 +00:00
|
|
|
}
|
2019-04-24 14:06:05 +00:00
|
|
|
|
2020-02-27 17:45:21 +00:00
|
|
|
/* Make sure that the given process ID is not ours, the parent process, so that we do not kill ourselves */
|
|
|
|
if (dwProcessID == GetCurrentProcessId())
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-04-24 14:06:05 +00:00
|
|
|
|
2020-02-27 17:45:21 +00:00
|
|
|
/* Open the process so that we can terminate it */
|
|
|
|
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID);
|
|
|
|
if (!hProcess)
|
2019-04-24 14:06:05 +00:00
|
|
|
{
|
2020-02-27 17:45:21 +00:00
|
|
|
DPRINT("CloseProcess(): Failed to open the process for termination! (Error: %lu)", GetLastError());
|
|
|
|
return FALSE;
|
2019-04-24 14:06:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 17:45:21 +00:00
|
|
|
/* Terminate it */
|
|
|
|
TerminateProcess(hProcess, 0);
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
return TRUE;
|
2019-04-24 14:06:05 +00:00
|
|
|
}
|