Process^ pipeClient = gcnew Process();
pipeClient->StartInfo->FileName = "pipeClient.exe";
AnonymousPipeServerStream^ pipeServer =
gcnew AnonymousPipeServerStream(PipeDirection::Out,
HandleInheritability::Inheritable);
// Show that anonymous pipes do not support Message mode.
try
{
Console::WriteLine("[SERVER] Setting ReadMode to \"Message\".");
pipeServer->ReadMode = PipeTransmissionMode::Message;
}
catch (NotSupportedException^ e)
{
Console::WriteLine("[SERVER] Exception:\n {0}", e->Message);
}
Console::WriteLine("[SERVER] Current TransmissionMode: {0}.", pipeServer->TransmissionMode);
// Pass the client process a handle to the server.
pipeClient->StartInfo->Arguments = pipeServer->GetClientHandleAsString();
pipeClient->StartInfo->UseShellExecute = false;
pipeClient->Start();
pipeServer->DisposeLocalCopyOfClientHandle();
try
{
// Read user input and send that to the client process.
StreamWriter^ sw = gcnew StreamWriter(pipeServer);
sw->AutoFlush = true;
// Send a 'sync message' and wait for client to receive it.
sw->WriteLine("SYNC");
pipeServer->WaitForPipeDrain();
// Send the console input to the client process.
Console::Write("[SERVER] Enter text: ");
sw->WriteLine(Console::ReadLine());
sw->Close();
}
I'm not sure I understand your question. Difference between what and what?
Here's some code I've used in the past to run a hidden console program in the background and monitor its standard output stream. This can be useful when writing a utility program that does some work with the assistance of another console program.
Your solution is to simply write to process input or to read from process output through the StreamWriter and StreamReader. That is one way to do. A second way would be more like in native C/C++ applications, through anonymous pipes. Create pipes and pass them to the new process. Now, I'm interested if there is any substantial difference? Is there any hidden condition, like pipe size, etc.?
emadsen wrote:I'm not sure I understand your question. Difference between what and what?
Here's some code I've used in the past to run a hidden console program in the background and monitor its standard output stream. This can be useful when writing a utility program that does some work with the assistance of another console program.
Your question is how to "redirect standard input and output" in .NET. Standard input and output is a specialized case of inter-process communication. For this specialized case, the recommended solution in .NET is to use a StreamReader on StandardInput and a StreamWriter on StandardOutput.
OK, let's consider the more general question of how to communicate between processes using .NET.
Pipes are available in .NET, and I believe they're used for inter-process communication on a single machine or between machines. I admit I don't know much about them. I've never used pipes. See this article for details:
The recommend solution in .NET for inter-process communication is to use Windows Communication Foundation (WCF). WCF supports binary or XML message formats and uni-directional or bi-directional channels. See this article for details:
I won't attempt to talk you out of using pipes (because I have no experience with them). My impression is they were added in .NET 3.5 to support advanced scenarios. The more common approach is to use WCF.
Karlo, this is not an answer to your question but I like how java/c# hide all the details behind inter-process communication. Programming a GUI becomes so much more enjoyable with that. My code for connecting to an engine, remote shell, database of positions, ics server etc all do their own stuff and then redirect their ios later. So you shouldn't be writing anything like that in C unless you absolutely have to