diff --git a/base/applications/utilman/precomp.h b/base/applications/utilman/precomp.h index db665e793c231..fba37387efc54 100644 --- a/base/applications/utilman/precomp.h +++ b/base/applications/utilman/precomp.h @@ -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); diff --git a/base/applications/utilman/process.c b/base/applications/utilman/process.c index 45bae49276fc8..3398ac088145d 100644 --- a/base/applications/utilman/process.c +++ b/base/applications/utilman/process.c @@ -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 */ @@ -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; } /** @@ -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; }