我正在寻找一种方法来检测具有相同进程名称的运行进程数量。例如,我运行了三次记事本,那么它将返回3。
我目前有这些代码来检测一个正在运行的进程,但不计算其运行进程数量。
#include <iostream> #include <windows.h> #include <tlhelp32.h> #include <tchar.h>任何帮助都将不胜感激 :) 谢谢。
bool IsProcessRunning(const char *ProcessName);
int main() { char *notepadRunning = (IsProcessRunning("notepad.exe")) ? "是" : "否"; std::cout << "记事本是否在运行? " << notepadRunning; return 0; }
bool IsProcessRunning(const char *ProcessName) { PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) }; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(Process32First(hSnapshot, &pe32)) { do { if(_tcsicmp(pe32.szExeFile, ProcessName) == 0) { CloseHandle(hSnapshot); return true; } } while(Process32Next(hSnapshot, &pe32)); }
CloseHandle(hSnapshot); return false; }