首页 > 系统相关 >windows 根据父进程pid查找所有子进程id(C++)

windows 根据父进程pid查找所有子进程id(C++)

时间:2022-12-29 15:34:49浏览次数:38  
标签:parent windows pid hProcessSnap vec pe 进程 include id


直接上代码:

大家直接调用即可

#include <iostream>
#include <Windows.h>
#include <tlhelp32.h>
#include <string>
#include <vector>

using namespace std;

vector<DWORD> GetProceeIDfromParentID(DWORD& dwParentProcessId)
{
vector<DWORD> parent_id_vec;
DWORD dwProcessID = 0;

//进行一个进程快照
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
cout << ("进程快照失败");
return parent_id_vec;
}

PROCESSENTRY32 pe;
TCHAR procID[100] = { 0 };
pe.dwSize = sizeof(pe);
BOOL bProcess = Process32First(hProcessSnap, &pe);
//遍历所有进程
while (bProcess)
{
if (pe.th32ParentProcessID == dwParentProcessId)//判断如果父id与其pid相等,
{

dwProcessID = pe.th32ProcessID;
parent_id_vec.push_back(dwProcessID);
}
bProcess = Process32Next(hProcessSnap, &pe);
}
CloseHandle(hProcessSnap);
return parent_id_vec;
}

int main()
{
DWORD parent_id = 17280;
vector<DWORD> child_id_vector = GetProceeIDfromParentID(parent_id);

}

 

标签:parent,windows,pid,hProcessSnap,vec,pe,进程,include,id
From: https://blog.51cto.com/u_15906863/5977832

相关文章