概述
#include<stdio.h>
#include<TlHelp32.h>
#include <vector>
#pragma comment(lib,"Advapi32.lib")
typedef struct _SUBPROCESS_INFO
{
DWORD Level;
DWORD ProcessId;
std::wstring ProcessName;
}SUBPROCESS_INFO, *PSUBPROCESS_INFO;
bool GetAllSubProcessId(DWORD Level,DWORD ProcessId,std::vector<PSUBPROCESS_INFO> * SubProcessList)
{
PROCESSENTRY32W PE;
memset(&PE, 0, sizeof(PROCESSENTRY32W));
PE.dwSize = sizeof(PROCESSENTRY32W);
auto hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
return 0;
}
if (!Process32FirstW(hSnapshot, &PE))
{
CloseHandle(hSnapshot);
return 0;
}
bool IsContinue = true;
do
{
do
{
if (IsContinue ==false)
{
break;
}
if (PE.th32ProcessID <= 4)
{
break;
}
if (PE.th32ParentProcessID == ProcessId)
{
bool IsInList = false;
for (auto i = SubProcessList->begin(); i != SubProcessList->end(); i++)
{
if ((*i)->ProcessId == PE.th32ProcessID)
{
IsContinue = false;
IsInList = true;
break;
}
}
if (IsInList == false)
{
PSUBPROCESS_INFO SubprocessInfo = new SUBPROCESS_INFO;
SubprocessInfo->ProcessId = PE.th32ProcessID;
SubprocessInfo->Level = Level + 1;
SubprocessInfo->ProcessName = PE.szExeFile;
SubProcessList->push_back(SubprocessInfo);
}
}
} while (false);
} while (Process32NextW(hSnapshot, &PE));
CloseHandle(hSnapshot);
return true;
}
int main()
{
std::vector<PSUBPROCESS_INFO> SubProcessList;
PSUBPROCESS_INFO SubprocessInfo = new SUBPROCESS_INFO;
SubprocessInfo->ProcessId = 7092;
SubprocessInfo->Level = 0;
SubprocessInfo->ProcessName = L"explorer.exe";
SubProcessList.push_back(SubprocessInfo);
//遍历三层子进程
for (size_t i = 0; i < 3; i++)
{
std::vector<PSUBPROCESS_INFO> TmpList = SubProcessList;
auto it = TmpList.begin();
while (it != TmpList.end())
{
GetAllSubProcessId(i,(*it)->ProcessId, &SubProcessList);
it++;
}
}
auto it = SubProcessList.begin();
while (it != SubProcessList.end())
{
printf("进程ID:%d Level:%d 进程名:%S n",(*it)->ProcessId, (*it)->Level, (*it)->ProcessName);
it++;
}
getchar();
return 0;
}
最后
以上就是现实咖啡豆为你收集整理的遍历进程的所有子进程的全部内容,希望文章能够帮你解决遍历进程的所有子进程所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复