很多和获取handle有关的api(如OpenXXXX之类)都会有bInheritHandles的flag,可是inherited handle到底有什么用处,应该怎么用呢?比如CreateProcess,如果bInheritHandles给TRUE的话,传说子进程可以继承父进程的handle,可是这些handle怎么访问呢?这个问题google了很久都没搞明白,请明白的出来解释下,或者给个reference也行。

解决方案 »

  1.   

    你想怎么访问呢?
    比方说在管道通信中,父进程定义了管道通信句柄,如果bInheritHandles给TRUE,子进程那个就可以利用管道通信句柄进行通信
      

  2.   

    我的意思就是子进程怎么知道它继承了那些handle呢?
    子进程又怎么知道每个handle是什么?还有最关键的,子进程里面用这些handle的代码怎么写?
    比如父进程 HANDLE handle = OpenXXXX(....);
    父进程里用到的时候就可以UseHandle(handle);
    子进程里要用到的时候 UseHandle(????);这个参数怎么抓到?
      

  3.   

    参考WINDOWS VIA C++
    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. The child process' initialization code parses the command line (usually by calling _stscanf_s) and extracts the handle value. Once the child has the handle value, it has the same access to the object as its parent. Note that the only reason handle inheritance works is because the handle value of the shared kernel object is identical in both the parent process and the child process. This is why the parent process is able to pass the handle value as a command-line argument.
    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 (using the WaitForInputIdle function discussed in Chapter 9, "Thread Synchronization with Kernel Objects"); then the parent can send or post a message to a window created by a thread in the child process.Another technique is for the parent process to add an environment variable to its environment block. The variable's name would be something that the child process knows to look for, and the variable's value would be the handle value of the kernel object to be inherited. Then when the parent spawns the child process, the child process inherits the parent's environment variables and can easily call GetEnvironmentVariable to obtain the inherited object's handle value. This approach is excellent if the child process is going to spawn another child process, because the environment variables can be inherited again. The special case of a child process inheriting its parent console is detailed in the Microsoft Knowledge Base at http://support.microsoft.com/kb/190351.1> 传递命令行
    2> 子进程初始化完后, 发送消息
    3> 环境变量