I am trying to call a FastCGI application from .Net - this means that I need to pass a handle to a socket to the child process.
However what I'm seeing is that if I use the STARTF_USESTDHANDLES
flag with CreateProcess()
then the child application fails when it attempts to read from the socket.
I've worked out that I get around this by not specifying STARTF_USESTDHANDLES
, but I'd like to understand why this is happening., especially as my understanding of the MSDN documentation is that I should use this flag when redirecting standard inputs.
This is my C# application (error checking etc... removed for brevity)
string command = @"FastCGI.exe";
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 8221));
// Duplicate the socket handle so that it is inheritable
SafeFileHandle childHandle;
NativeMethods.DuplicateHandle(NativeMethods.GetCurrentProcess(), new SafeFileHandle(listener.Handle, false), NativeMethods.GetCurrentProcess(), out childHandle, 0, true, NativeMethods.DUPLICATE_SAME_ACCESS);
NativeMethods.STARTUPINFO startupInfo = new NativeMethods.STARTUPINFO();
startupInfo.hStdInput = childHandle;
// Uncommenting the following line causes the problem
//startupInfo.dwFlags = NativeMethods.STARTF_USESTDHANDLES;
// Start the child process
NativeMethods.PROCESS_INFORMATION processInformation;
NativeMethods.CreateProcess(null, command, null, null, true, 0, IntPtr.Zero, null, startupInfo, out processInformation);
// To prevent the process closing the socket handle
Console.ReadKey();
My child process is the sample FastCGI application contained in the Windows Azure VS2010 C# code samples, the line that fails is:
BOOL ret = ReadFile( hStdin, pBuffer, dwSize, &nNumberOfBytes, NULL );
if(!ret)
{
// dw = 87 (ERROR_INVALID_PARAMETER)
DWORD dw = GetLastError();
}
I'm relatively new to both socket and handle programming, so any insight into why this happens would be much appreciated.