[KILL] ExecuteKill(): Return '1' on failures

and add a related 'CloseHandle()' call.

Also, enforce PID 'unsigned' type.
This commit is contained in:
Serge Gautherie 2024-02-27 22:42:54 +01:00 committed by Hermès BÉLUSCA - MAÏTO
parent 5a30c71e70
commit 3e3594e3aa

View file

@ -35,25 +35,26 @@
#include <stdlib.h> #include <stdlib.h>
int int
ExecuteKill(char *lpPid) ExecuteKill(const char *lpPid)
{ {
HANDLE hProcess; HANDLE hProcess;
DWORD dwProcessId; DWORD dwProcessId;
dwProcessId = (DWORD)atol(lpPid); dwProcessId = strtoul(lpPid, NULL, 10);
fprintf(stderr, "Killing PID %ld...\n", dwProcessId); fprintf(stderr, "Killing the process with PID %lu...\n", dwProcessId);
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId); hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
if (hProcess == NULL) if (hProcess == NULL)
{ {
fprintf(stderr, "Could not open the process with PID = %ld\n", dwProcessId); fprintf(stderr, "Could not open the process with PID %lu\n", dwProcessId);
return 0; return 1;
} }
if (!TerminateProcess(hProcess, 0)) if (!TerminateProcess(hProcess, 0))
{ {
fprintf(stderr, "Could not terminate the process with PID = %ld\n", dwProcessId); fprintf(stderr, "Could not terminate the process with PID %lu\n", dwProcessId);
return 0; CloseHandle(hProcess);
return 1;
} }
CloseHandle(hProcess); CloseHandle(hProcess);