I'm currently passing the pid on the command line to the child, but is there a way to do this in the Win32 API? Alternatively, can someone alleviate my fear that the pid I'm passing might belong to another process after some time if the parent has died?
我目前正在将命令行中的pid传递给子节点,但是在Win32 API中是否有办法做到这一点呢?或者,有人能减轻我的恐惧吗?如果父母去世了,我所传递的pid可能会属于另一个过程。
5 个解决方案
#1
9
Notice that if the parent process terminates it is very possible and even likely that the PID will be reused for another process. This is standard windows operation.
请注意,如果父进程终止它是非常可能的,甚至可能是PID将会被重用来进行另一个过程。这是标准的windows操作。
So to be sure, once you receive the id of the parent and are sure it is really your parent you should open a handle to it and use that.
当然,一旦你接收到父类的id并且确定它是你的父类,你应该打开它的句柄并使用它。
#2
47
Just in case anyone else runs across this question and is looking for a code sample, I had to do this recently for a Python library project I'm working on. Here's the test/sample code I came up with:
为了防止其他人遇到这个问题,并且正在寻找一个代码示例,我最近不得不为我正在开发的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);
}
#3
19
ULONG_PTR GetParentProcessId() // By Napalm @ NetCore2K
{
ULONG_PTR pbi[6];
ULONG ulSize = 0;
LONG (WINAPI *NtQueryInformationProcess)(HANDLE ProcessHandle, ULONG ProcessInformationClass,
PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength);
*(FARPROC *)&NtQueryInformationProcess =
GetProcAddress(LoadLibraryA("NTDLL.DLL"), "NtQueryInformationProcess");
if(NtQueryInformationProcess){
if(NtQueryInformationProcess(GetCurrentProcess(), 0,
&pbi, sizeof(pbi), &ulSize) >= 0 && ulSize == sizeof(pbi))
return pbi[5];
}
return (ULONG_PTR)-1;
}
#4
18
A better way to do this is to call DuplicateHandle()
to create an inheritable duplicate of your process handle. Then create the child process and pass the handle value on the command line. Close
the duplicated handle in the parent process. When the child's done, it will need to Close
its copy as well.
更好的方法是调用副本句柄()来创建进程句柄的可继承副本。然后创建子进程并在命令行上传递句柄值。关闭父进程中的重复句柄。当孩子完成时,它也需要关闭它的副本。
#5
2
"Alternatively, can someone alleviate my fear that the pid I'm passing might belong to another process after some time if the parent has died?"
“或者,有人能减轻我的恐惧吗?如果父母去世了,我所传递的pid可能会属于另一个过程。”
Yes, the PID can be reused. Unlike UNIX, Windows does not maintain a strong parent-child relationship tree.
是的,PID可以重复使用。与UNIX不同,Windows不维护强大的父子关系树。
#1
9
Notice that if the parent process terminates it is very possible and even likely that the PID will be reused for another process. This is standard windows operation.
请注意,如果父进程终止它是非常可能的,甚至可能是PID将会被重用来进行另一个过程。这是标准的windows操作。
So to be sure, once you receive the id of the parent and are sure it is really your parent you should open a handle to it and use that.
当然,一旦你接收到父类的id并且确定它是你的父类,你应该打开它的句柄并使用它。
#2
47
Just in case anyone else runs across this question and is looking for a code sample, I had to do this recently for a Python library project I'm working on. Here's the test/sample code I came up with:
为了防止其他人遇到这个问题,并且正在寻找一个代码示例,我最近不得不为我正在开发的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);
}
#3
19
ULONG_PTR GetParentProcessId() // By Napalm @ NetCore2K
{
ULONG_PTR pbi[6];
ULONG ulSize = 0;
LONG (WINAPI *NtQueryInformationProcess)(HANDLE ProcessHandle, ULONG ProcessInformationClass,
PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength);
*(FARPROC *)&NtQueryInformationProcess =
GetProcAddress(LoadLibraryA("NTDLL.DLL"), "NtQueryInformationProcess");
if(NtQueryInformationProcess){
if(NtQueryInformationProcess(GetCurrentProcess(), 0,
&pbi, sizeof(pbi), &ulSize) >= 0 && ulSize == sizeof(pbi))
return pbi[5];
}
return (ULONG_PTR)-1;
}
#4
18
A better way to do this is to call DuplicateHandle()
to create an inheritable duplicate of your process handle. Then create the child process and pass the handle value on the command line. Close
the duplicated handle in the parent process. When the child's done, it will need to Close
its copy as well.
更好的方法是调用副本句柄()来创建进程句柄的可继承副本。然后创建子进程并在命令行上传递句柄值。关闭父进程中的重复句柄。当孩子完成时,它也需要关闭它的副本。
#5
2
"Alternatively, can someone alleviate my fear that the pid I'm passing might belong to another process after some time if the parent has died?"
“或者,有人能减轻我的恐惧吗?如果父母去世了,我所传递的pid可能会属于另一个过程。”
Yes, the PID can be reused. Unlike UNIX, Windows does not maintain a strong parent-child relationship tree.
是的,PID可以重复使用。与UNIX不同,Windows不维护强大的父子关系树。