检查给定进程是否正在运行
Checking if given process is running
当我使用以下函数作为isRunning("example.exe")时;无论进程是否正在运行,它总是返回0。
我尝试将其设置为std::cout<<pe.szExeFile;在do-while循环中,它以与我尝试传递函数相同的格式输出所有进程。
该项目是多字节字符集,以防万一。
bool isRunning(CHAR process_[])
{
HANDLE pss = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
PROCESSENTRY32 pe = { 0 };
pe.dwSize = sizeof(pe);
if (Process32First(pss, &pe))
{
do
{
if (pe.szExeFile == process_) // if(!strcmp(pe.szExeFile, process_)) is the correct line here
return true; // If you use this remember to close the handle here too with CloseHandle(pss);
} while (Process32Next(pss, &pe));
}
CloseHandle(pss);
return false;
}
似乎找不到我的错误。谢谢你的时间。
您正在使用比较指针值的if (pe.szExeFile == process_)
。 您应该使用 strcmp
或 _stricmp
之类的东西来比较实际的字符串值。
例如
if(strcmp (pe.szExeFile, process_) == 0)
return true;
相关文章:
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 不同/较旧的处理器运行c++代码的方式是否不同
- 在运行时检查继承是否只有一种类型和 void*
- 扩展类中的可选 vir 函数,测试它在运行时是否存在
- 编写了一个C++代码来检查表达式是否具有平衡括号并且我的代码未运行.我已经卡了一天了
- 在实践中,在运行时为零的乘法中是否有任何"lazy"评估
- 是否可以编译 Gtk+ 应用程序以同时在 Gtk 2 和 3 上运行
- 在 c++ 中是否允许创建具有运行时边界的数组?
- 是否可以在运行时强制转换模板参数?
- 在 C++ 中运行时调用模板时,是否可以切换模板的参数类型?
- MinGW是否需要在Visual Studio上运行C++
- 使用 int32_t 而不是双精度运行矢量点积是否更快?
- 如何检测窗口当前是否正在运行模式对话框?
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- C++如何检查程序是否首次运行?
- 确定C++应用程序是否作为具有旧版支持的 UWP 应用运行
- 如何检查 ntpd 是否仍在运行和同步
- 如果事件在仍在执行时再次设置,RegisterWaitForSingleObject 是否会并行运行回调?
- 是否可以在运行时调用用于选择要调用的用户定义文本的逻辑?
- 是否需要在 Timer.5 boost::asio 教程中运行新线程?