简体   繁体   English

C ++在Windows中发送简单信号

[英]C++ Sending a simple signal in Windows

is there an equivalent to the function kill() on Windows? Windows上的函数kill()是否等效?

int kill(pid_t pid, int sig);

If not, would it be possible to test if a process is running based on its PID? 如果没有,是否可以根据其PID测试进程是否正在运行?

Thanks 谢谢

Windows doesn't have signals in the unix sense. Windows没有unix意义上的信号。

You can use OpenProcess to check if a process exists - If it succeeds, or fails with an access error, then the process exists. 您可以使用OpenProcess检查进程是否存在 - 如果成功或因访问错误而失败,则该进程存在。

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

No signals in Windows. Windows中没有信号。 If true killing is intended then use TerminateProcess(). 如果要进行真正的查杀,则使用TerminateProcess()。 You need a handle to the process, get that from OpenProcess(). 您需要一个进程句柄,从OpenProcess()获取。 You'll need to ask for the PROCESS_TERMINATE access right. 您需要请求PROCESS_TERMINATE访问权限。 CloseHandle() to close the handle. CloseHandle()关闭句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM