c++ - 检查进程是否存在

标签 c++ mfc process

我编写代码并使用 MFC。 我想检查进程是否存在。

例如:

if (myProcess.exe exists)
  do something
else
  do something else

我该怎么做?

谢谢

最佳答案

使用命名互斥体:

在程序的开头:

HANDLE hMutex = CreateMutex(NULL, TRUE, "Your program name");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
    // Process already running.
    CloseHandle(hMutex);
}
else
{
    // No process running.
}

程序结束时:

if (hMutex)
{
    CloseHandle(hMutex);
    hMutex = NULL;
}

关于c++ - 检查进程是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10967618/

相关文章:

c++ - 为什么显示 ListView 图标时背景变黑?

android - 按下 [Home] 后,已终止进程的 Activity 在列表中仍然可见

c++ - C/C++多进程访问同一个变量的方法

unix - 为什么 unix 进程的内核堆栈位于上下文的动态部分

c++ - CMAKE - 调试/交叉构建?

c++ - 固定大小数组的 vector

c++ - C++中 '\0'的解释

c++ - 如何将 LONG 转换为 CString?

c++ - 什么是私有(private)MFC,为什么不能通过普通接口(interface)访问?

c++ - 如何在 mac 中使用 make 和 make install