C++ 控制台判断程序是否正在运行 - www.exe 示例
可以使用 Windows API 函数 EnumProcesses 来枚举系统中正在运行的进程,然后通过进程名称判断 www.exe 是否在运行。
以下是一个示例代码:
#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
bool IsProcessRunning(const char* processName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return false;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return false;
}
bool found = false;
do {
if (_stricmp(pe32.szExeFile, processName) == 0) {
found = true;
break;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return found;
}
int main()
{
if (IsProcessRunning('www.exe')) {
std::cout << 'www.exe is running\n';
} else {
std::cout << 'www.exe is not running\n';
}
return 0;
}
注意:在使用 Windows API 函数时,需要在代码中包含 Windows.h 头文件,并且需要链接 Kernel32.lib 库。
原文地址: https://www.cveoy.top/t/topic/owGv 著作权归作者所有。请勿转载和采集!