I am trying to inherit a Pipe Handle from a C# parent process to a C++ Child process.
I create the Parent in the C# Process in the following way:
AnonymousPipeServerStream pipe = AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);
string pipeName = pipe.GetClientHandleAsString();
I then pass the handle to the Client Process like this:
ProcessStartInfo clientProcessStartInfo = new ProcessStartInfo("cpp_process.exe", pipeName);
startInfo.UseShellExecute = false;
Process process = new Process {
StartInfo = startInfo
}
process.Start();
pipe.DisposeLocalCopyOfClientHandle();
In the Child C++ Process, i get the Pipe Handle like this:
std::string pipeHandleString = argv[1];
int pipeHandleInt = std::stoi(pipeHandleString);
HANDLE pipeHandle = (void*) pipeHandleInt;
But when i try to use the Pipe Handle in the Child Process like this:
std::array<char, 256> buffer = {};
DWORD numberOfBytesRead;
BOOL result = ReadFile(pipeHandle, &buffer, 256, &numberOfBytesRead, nullptr);
Result is FALSE
and GetLastError()
returns This handle is invalid
.
As far as i understand it, the Child Process should inherit the pipe handle automatically?
Removing pipe.DisposeLocalCopyOfClientHandle()
does not change the Result.
Also, using the pipe handle in a C# Client Process like this:
AnonymousPipeClientStream pipe = new AnonymousPipeClientStream(PipeDirection.In, args[1]);
Works just fine, so im guessing the C# implementation does something to the Handle that i'm missing in my C++ implementation, but i cant figure out what that is.