VC获取父进程PID
生活随笔
收集整理的這篇文章主要介紹了
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;
}
//
#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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VC程序自删除
- 下一篇: s3c2440移植MQTT