日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

VC获取父进程PID

發布時間:2024/4/11 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VC获取父进程PID 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// tt.cpp : 定義控制臺應用程序的入口點。
//

#include "stdafx.h"
#include <windows.h> ?
#include <stdio.h> ?

typedef enum enumSYSTEM_INFORMATION_CLASS ?
{ ?
?? ?SystemBasicInformation, ?
?? ?SystemProcessorInformation, ?
?? ?SystemPerformanceInformation, ?
?? ?SystemTimeOfDayInformation, ?
}SYSTEM_INFORMATION_CLASS; ?

typedef struct tagPROCESS_BASIC_INFORMATION ?
{ ?
?? ?DWORD ExitStatus; ?
?? ?DWORD PebBaseAddress; ?
?? ?DWORD AffinityMask; ?
?? ?DWORD BasePriority; ?
?? ?ULONG UniqueProcessId; ?
?? ?ULONG InheritedFromUniqueProcessId; ?
}PROCESS_BASIC_INFORMATION; ?

typedef LONG (WINAPI *PNTQUERYINFORMATIONPROCESS)(HANDLE,UINT,PVOID,ULONG,PULONG); ?
PNTQUERYINFORMATIONPROCESS? NtQueryInformationProcess = NULL; ?

#define PRINT_LINE? printf("---------------------------------------------\n") ?

int GetParentProcessID(DWORD dwId) ?
{ ?
?? ?LONG????????????????????? status; ?
?? ?DWORD???????????????????? dwParentPID = 0; ?
?? ?HANDLE??????????????????? hProcess; ?
?? ?PROCESS_BASIC_INFORMATION pbi; ?

?? ?hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,dwId); ?
?? ?if(!hProcess) ?
?? ??? ?return -1; ?

?? ?status = NtQueryInformationProcess(hProcess,SystemBasicInformation,(PVOID)&pbi,sizeof(PROCESS_BASIC_INFORMATION),NULL); ?
?? ?if(!status) ?
?? ??? ?dwParentPID = pbi.InheritedFromUniqueProcessId; ?

?? ?CloseHandle (hProcess); ?
?? ?return dwParentPID; ?
} ?


int _tmain(int argc, _TCHAR* argv[])
{
?? ?NtQueryInformationProcess = (PNTQUERYINFORMATIONPROCESS)GetProcAddress(GetModuleHandle("ntdll"),"NtQueryInformationProcess"); ?
?? ?if (!NtQueryInformationProcess) ?
?? ??? ?return -1; ?

?? ?int nID?? = GetCurrentProcessId(); ?
?? ?int nTemp = 0; ?

?? ?PRINT_LINE; ?
?? ?nTemp = GetParentProcessID(nID); ?
?? ?if(nTemp == -1) ?
?? ?{ ?
?? ??? ?printf(" 獲取失敗!\n"); ?
?? ??? ?return -1; ?
?? ?} ?

?? ?printf("進程:%lu ---->>>>>父進程PID為:%lu\n",nID,nTemp); ?
?? ?while (true) ?
?? ?{ ?
?? ??? ?nID = GetParentProcessID(nTemp); ?
?? ??? ?if(nID == -1) ?
?? ??? ??? ?break; ?

?? ??? ?printf("進程:%lu ---->>>>>父進程PID為:%lu\n",nTemp,nID); ?
?? ??? ?nTemp = nID; ?
?? ?} ?

?? ?PRINT_LINE; ?
?? ?getchar(); ?
?? ???????????????????????????????????? ?
?? ?return 0;
}

總結

以上是生活随笔為你收集整理的VC获取父进程PID的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。