C++ Sending a simple signal in Windows

只愿长相守 提交于 2019-12-01 04:55:23

问题


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


回答1:


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;
}



回答2:


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.



来源:https://stackoverflow.com/questions/5289549/c-sending-a-simple-signal-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!