4

is there an equivalent to the function kill() on Windows?

int kill(pid_t pid, int sig);

If not, would it be possible to test if a process is running based on its PID?

Thanks

Ted Gueniche
  • 812
  • 9
  • 20

2 Answers2

3

Windows doesn't have signals in the unix sense.

You can use OpenProcess to check if a process exists - If it succeeds, or fails with an access error, then the process exists.

bool processExists(DWORD ProcessID) {
  HANDLE hProcess = OpenProcess(SYNCHRONIZE, FALSE, ProcessID);
  if (hProcess != NULL) {
    CloseHandle(hProcess);
    return true;
  }
  // If the error code is access denied, the process exists but we don't have access to open a handle to it.
  return GetLastError() == ERROR_ACCESS_DENIED;
}
Erik
  • 88,732
  • 13
  • 198
  • 189
1

No signals in Windows. If true killing is intended then use TerminateProcess(). You need a handle to the process, get that from OpenProcess(). You'll need to ask for the PROCESS_TERMINATE access right. CloseHandle() to close the handle.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • One side comment: Using TerminateProcess is like using a sledgehammer - it does exactly what you'd think it does, it terminates the process. The process doesn't get to do any cleanup, it's terminated. – Larry Osterman Mar 13 '11 at 15:59