如何做Windows处理inheritance

我有父进程和subprocess,在父进程中,我指出句柄将被inheritance,如http://msdn.microsoft.com/en-us/library/windows/desktop/ms724466%28v=vs.85 %29.aspx :

... SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; // Create a pipe for the Parent process's STDOUT. if ( ! CreatePipe(&hChildReadPipe, &hParentWritePipe, &sa, 0) ) { _tprintf(_T("Error creating Pipe\n")); } ... // Start the child process. if( !CreateProcess( _T("..\\Debug\\Child.exe"), _T("hChildReadPipe"), // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. TRUE, // Set handle inheritance to TRUE. CREATE_NEW_CONSOLE, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION structure. ) { printf( "\nCreateProcess failed (%d).\n", GetLastError() ); return FALSE; } 

我的问题是我如何获得hChildReadPipe处理命令行传递给subprocess,在MSDN他们build议uging GetCommandLine(),但这只返回命令行string,如何将此string转换为可用的句柄?

我尝试CreateFile(),但它不工作…

谢谢

我的第一个建议是阅读创建一个重定向输入和输出的子进程 。

但是,如果您的要求是不触摸标准输入或标准输出(或其他相关的句柄),那么你可以把句柄转换为一个整数值( UINT_PTR value = (UINT_PTR)hChildReadPipe; ),并将其作为字符串传递给命令行( __ultoa() )。 然后,孩子将需要从命令行读取整数值( UINT_PTR value = strtoul() ),并将其转换回HANDLE( HANDLE hChildReadPipe = (HANDLE)value; )。

要记住的重要一点是从Microsoft处理继承的摘录 , "An inherited handle ... has the same value ..."