Skip to content

Commit

Permalink
[UTILMAN] Small refactor on process management code
Browse files Browse the repository at this point in the history
Implement a small helper, GetProcessID(), that will be used to return the process ID which we are interested in. This largely removes some duplicated code. The following patch addresses the Jansen's comment on reactos#1608 PR.
  • Loading branch information
GeoB99 committed Feb 23, 2020
1 parent dee7718 commit cda5a13
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 48 deletions.
1 change: 1 addition & 0 deletions base/applications/utilman/precomp.h
Expand Up @@ -59,6 +59,7 @@ INT ListBoxRefreshContents(VOID);
VOID CheckUtilityState(BOOL bUtilState);

/* process.c */
DWORD GetProcessID(IN LPCWSTR lpProcessName);
BOOL IsProcessRunning(IN LPCWSTR lpProcessName);
BOOL LaunchProcess(LPCWSTR lpProcessName);
BOOL CloseProcess(IN LPCWSTR lpProcessName);
Expand Down
119 changes: 71 additions & 48 deletions base/applications/utilman/process.c
Expand Up @@ -12,29 +12,28 @@
/* FUNCTIONS ******************************************************************/

/**
* @IsProcessRunning
* @GetProcessID
*
* Checks if a process is running.
* Returns the process executable ID based on the given executable name.
*
* @param[in] ProcName
* @param[in] lpProcessName
* The name of the executable process.
*
* @return
* Returns TRUE if the given process' name is running,
* FALSE otherwise.
* Returns the ID number of the process, otherwise 0.
*
*/
BOOL IsProcessRunning(IN LPCWSTR lpProcessName)
DWORD GetProcessID(IN LPCWSTR lpProcessName)
{
BOOL bIsRunning = FALSE;
PROCESSENTRY32W Process = {0};
PROCESSENTRY32W Process;

/* Create a snapshot and check whether the given process' executable name is running */
/* 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 FALSE;
return 0;

/* Get the whole size of the structure */
Process.dwSize = sizeof(Process);

/* Enumerate the processes */
Expand All @@ -44,17 +43,56 @@ BOOL IsProcessRunning(IN LPCWSTR lpProcessName)
{
if (_wcsicmp(Process.szExeFile, lpProcessName) == 0)
{
/* The process we are searching for is running */
bIsRunning = TRUE;
break;
/* The names match, return the process ID we're interested */
CloseHandle(hSnapshot);
return Process.th32ProcessID;
}
}
}
while (Process32NextW(hSnapshot, &Process));
}

/* Free the handle and return */
CloseHandle(hSnapshot);
return bIsRunning;
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;
}

/**
Expand Down Expand Up @@ -147,43 +185,28 @@ BOOL LaunchProcess(LPCWSTR lpProcessName)
*/
BOOL CloseProcess(IN LPCWSTR lpProcessName)
{
BOOL bSuccess = FALSE;
PROCESSENTRY32W Process = {0};
HANDLE hProcess;
DWORD dwProcessID;

/* Create a snapshot and check if the given process' executable name is running */
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hSnapshot == INVALID_HANDLE_VALUE)
/* Get the process ID */
dwProcessID = GetProcessID(lpProcessName);
if (dwProcessID == 0)
{
return FALSE;
}

Process.dwSize = sizeof(Process);
/* Open the process so that we can terminate it */
hProcess = OpenProcess(PROCESS_TERMINATE, 0, dwProcessID);

/* Enumerate the processes */
if (Process32FirstW(hSnapshot, &Process))
/* Make sure that we do not terminate ourselves of course */
if ((hProcess != NULL) && (dwProcessID != GetCurrentProcessId()))
{
do
{
if (_wcsicmp(Process.szExeFile, lpProcessName) == 0)
{
/*
* We have found the process. However we must make
* sure that we DO NOT kill ourselves (the process ID
* matching with the current parent process ID).
*/
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, Process.th32ProcessID);
if ((hProcess != NULL) && (Process.th32ProcessID != GetCurrentProcessId()))
{
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
bSuccess = TRUE;
break;
}
}
while (Process32NextW(hSnapshot, &Process));
/* Terminate it */
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
return TRUE;
}

/* Free the handle and return */
CloseHandle(hSnapshot);
return bSuccess;
CloseHandle(hProcess);
return FALSE;
}

0 comments on commit cda5a13

Please sign in to comment.