我目前正在使用 EnumProcesses 函数来获取正在运行的进程的列表。然而,由于我的应用程序在用户空间中运行,因此它无法获取不在用户下运行的进程(包括系统进程)的句柄。是否有其他方法可以让我访问这些内容?我所需要的只是进程名称。
只是为了补充这个答案,我为您只查找一个特定进程而不是整个列表的情况构建了这个。
bool FindRunningProcess(AnsiString process) {
/*
Function takes in a string value for the process it is looking for like ST3Monitor.exe
then loops through all of the processes that are currently running on windows.
If the process is found it is running, therefore the function returns true.
*/
AnsiString compare;
bool procRunning = false;
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
procRunning = false;
} else {
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hProcessSnap, &pe32)) { // Gets first running process
if (pe32.szExeFile == process) {
procRunning = true;
} else {
// loop through all running processes looking for process
while (Process32Next(hProcessSnap, &pe32)) {
// Set to an AnsiString instead of Char[] to make compare easier
compare = pe32.szExeFile;
if (compare == process) {
// if found process is running, set to true and break from loop
procRunning = true;
break;
}
}
}
// clean the snapshot object
CloseHandle(hProcessSnap);
}
}
return procRunning;
}
我应该注意到,这是在 Embarcadero RAD Studio (C++ Builder) 中编写的,并且 @Remy_Lebeau System::AnsiString 是一个 C++Builder 字符串类,用于其 VCL/FMX 框架中的 8 位 ANSI 字符数据。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)