Reputation: 207
I have parent and child processes, in the parent process i'm stating that handles are to be inherited as in 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;
}
My question is how do i get the hChildReadPipe handle passed by command line to the child process, in MSDN they recommend uging GetCommandLine() but this only returns the command line string, how do i convert this string to a usable HANDLE?
I tried CreateFile() but it doesn't work...
Thanks
Upvotes: 2
Views: 1635
Reputation: 4439
My first suggestion is to read Creating a Child Process with Redirected Input and Output.
However if your requirements are to not touch stdin or stdout (or other related HANDLEs) then you can just cast the handle to an integer value (UINT_PTR value = (UINT_PTR)hChildReadPipe;
) and pass that to the command line as a string (__ultoa()
). Then the child will need to read the integer value off of the command line (UINT_PTR value = strtoul()
) and cast that back to a HANDLE (HANDLE hChildReadPipe = (HANDLE)value;
).
The important thing to remember is an excerpt on handle inheritance from Microsoft, "An inherited handle ... has the same value ..."
.
Upvotes: 6