蓝山帝景
为了防止其他人遇到这个问题并且正在寻找代码示例,我最近必须为我正在研究的Python库项目执行此操作。这是我提出的测试/示例代码:#include <stdio.h>#include <windows.h>#include <tlhelp32.h>int main(int argc, char *argv[]) {
int pid = -1;
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = { 0 };
pe.dwSize = sizeof(PROCESSENTRY32);
//assume first arg is the PID to get the PPID for, or use own PID
if (argc > 1) {
pid = atoi(argv[1]);
} else {
pid = GetCurrentProcessId();
}
if( Process32First(h, &pe)) {
do {
if (pe.th32ProcessID == pid) {
printf("PID: %i; PPID: %i\n", pid, pe.th32ParentProcessID);
}
} while( Process32Next(h, &pe));
}
CloseHandle(h);}