[英]IsProcessRunning function always returns false
IsProcessRunning 總是返回 false,為什么? 記事本已 100% 運行! 我什至嘗試了內置版本,但仍然無法正常工作......很奇怪,我無法通過 Google 找到解決方案:/
#include <iostream>
#include <cstdio>
#include <windows.h>
#include <tlhelp32.h>
using namespace std;
bool IsProcessRunning(const wchar_t *processName)
{
bool exists = false;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry))
while (Process32Next(snapshot, &entry))
if (!wcsicmp(entry.szExeFile, processName))
exists = true;
CloseHandle(snapshot);
return exists;
}
int main()
{
if(IsProcessRunning(L"notepad"))
{
cout << "Notepad running!";
}
else
{
cout << "Notepad not running!";
}
cin.get();
return 0;
}
應該是“Notepad.exe”
#include <iostream>
#include <cstdio>
#include <windows.h>
#include <tlhelp32.h>
using namespace std;
bool IsProcessRunning(const wchar_t* processName) {
bool exists = false;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry))
while (Process32Next(snapshot, &entry)) {
wcout << entry.szExeFile << endl;
if (!wcsicmp(entry.szExeFile, processName))
exists = true;
}
CloseHandle(snapshot);
return exists;
}
int main() {
if (IsProcessRunning(L"notepad.exe")) {
cout << "Notepad running!";
} else {
cout << "Notepad not running!";
}
cin.get();
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.