When a process is initialized, the system allocates a handle table for it. This handle table is used only for kernel objects, not for User objects or GDI objects. The details of how the handle table is structured and managed are undocumented.
Object handle inheritance can be used only when processes have a parent-child relationship. In this scenario, one or more kernel object handles are available to the parent process, and the parent decides to spawn a child process, giving the child access to the parent's kernel objects.
Because you passed TRUE to CreateProcess' bInheritHandles parameter, the system does one
more thing: it walks the parent process' handle table, and for each entry it finds that contains a valid inheritable handle, the system copies the entry exactly into the child process' handle table. The entry is copied to the exact same position in the child process' handle table as in the parent's handle table. This fact is important because it means that the handle value that identifies a kernel object is identical in both the parent and child processes。
In addition to copying the handle table entry, the system increments the usage count of the kernel object because two processes are now using the object.
Be aware that object handle inheritance applies only at the time the child process is spawned. If the parent process were to create any new kernel objects with inheritable handles, an already-running child process would not inherit these new handles.
By far, the most common way for a child process to determine the handle value of the kernel object that it's expecting is to have the handle value passed as a command-line argument to the child process.
Of course, you can use other forms of interprocess communication to transfer an inherited kernel object handle value from the parent process into the child process. One technique is for the parent to wait for the child to complete initialization, then the parent can send or post a message to a window created by a thread in the child process.
敲黑板讲重点:看第5段。这子进程继承父进程的内核对象句柄的前提是,在子进程创建好之前父进程就得把要共享的内核对象给准备好了,否则,子进程一旦创建,父进程再创建新的内核对象就不能再由子进程所继承。
网友评论