测试平台:win10 专业版 21H2 1904
编译平台:gcc version 12.2.0 (x86_64-posix-seh-rev2, Built by MinGW-W64 project)
代码如下:
#include <stdio.h>
#include <winternl.h>
#include <Shlwapi.h>
#include<psapi.h>
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "shlwapi.lib")
unsigned long GetParentPID( unsigned long ProcessID);
void ShowProcName(unsigned long pid,char*);
int main(int argc, const char* argv[]) {
unsigned long ppid;
char pname[MAX_PATH] = {0};
ppid = GetParentPID(GetCurrentProcessId());
ShowProcName(ppid,pname);
printf("ParentProcessPID: %lu\n", ppid);
printf("ParentName: %s\n", pname);
system("pause");
return 0;
}
unsigned long GetParentPID(unsigned long ProcessID) {
/// 打开给定进程PID
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ProcessID);
/// 下面是获取函数 NtQueryInformationProcess 的函数指