xeo*_*eon 16 winapi process visual-c++
我目前正在使用EnumProcesses函数来获取正在运行的进程列表.但是,由于我的应用程序在用户空间中运行,因此无法获取未在用户下运行的进程(包括系统进程)的句柄.还有其他方法可以让我访问这些吗?我只需要进程名称.
Phi*_*hil 11
只是为了补充这个答案,我为你在寻找一个特定的进程而不是整个列表的情况下构建了这个.
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;
}
Run Code Online (Sandbox Code Playgroud)
我应该注意这里是用Embarcadero RAD Studio(C++ Builder)编写的,每个@Remy_Lebeau System :: AnsiString是一个C++ Builder字符串类,用于VCL/FMX框架中的8bit ANSI字符数据.
如果您只需要进程名称,那么使用WTSEnumerateProcesses如下:
WTS_PROCESS_INFO* pWPIs = NULL;
DWORD dwProcCount = 0;
if(WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, NULL, 1, &pWPIs, &dwProcCount))
{
//Go through all processes retrieved
for(DWORD i = 0; i < dwProcCount; i++)
{
//pWPIs[i].pProcessName = process file name only, no path!
//pWPIs[i].ProcessId = process ID
//pWPIs[i].SessionId = session ID, if you need to limit it to the logged in user processes
//pWPIs[i].pUserSid = user SID that started the process
}
}
//Free memory
if(pWPIs)
{
WTSFreeMemory(pWPIs);
pWPIs = NULL;
}
Run Code Online (Sandbox Code Playgroud)
使用此方法的好处是,您不必单独打开每个进程,然后检索其名称,因为如果使用EnumProcesses则必须执行此操作,如果您尝试打开进程,这也将不起作用以比您的用户帐户更高的权限运行。
此外,此方法也比在循环中调用Process32First()/快得多Process32Next()。
WTSEnumerateProcesses 是一个鲜为人知的 API,自 Windows XP 以来一直可用。
| 归档时间: |
|
| 查看次数: |
36280 次 |
| 最近记录: |