0

When I want a C# process to wait for any key press from Standart Input (stdin), I can use Console.In.ReadLine. I use it for a parent c# console application can send writeline event to a child console process. Using:

process.StandartInput.WriteLine("some_content");

And the child process can catch the writing, and do something.

Everything is OK when the child is C#, but when it's C++, I can't find some thing to replace Console.In. Is there an equivalent in C++ for Console.In.ReadLine. I did many search about C++ read from stdin, like here, they said about getch, getchar, system("pause")..., but seem like they cannot "catch" the parent write event.

Community
  • 1
  • 1
Andiana
  • 1,912
  • 5
  • 37
  • 73
  • 1
    Do you mean you have multiple processes, and want to send some text to a child process, so the child process can read it from its standard input? Then look into *pipes*. Pipes are platform dependent, but since you mention C# I assume you're on Windows? Then read more about `CreateProcess` and the `hStdInput` and `hStdOutput` handles in one of the structures passed to it (can't reach MSDN right now so can't provide links). – Some programmer dude Feb 18 '16 at 06:34
  • Can reach MSDN now. If you want to create child-processes in C++ on Windows, you have to use [`CreateProcess`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx). You need to pass a [`STARTUPINFO`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331%28v=vs.85%29.aspx) structure to the function, and that structure contains file handles you can use for the standard input and output for the child process. Use [`CreatePipe`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365152%28v=vs.85%29.aspx) to create a pipe that can be use for this. – Some programmer dude Feb 18 '16 at 06:53
  • Also see the article [Creating a Child Process with Redirected Input and Output](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx) for more information. – Some programmer dude Feb 18 '16 at 06:53
  • @JoachimPileborg That's right, but not only send parent send content to child process using standart input, I want the process P1 to wait (something like getch or getchar) from the process P2. P1 will pause and wait until it receive something from P2. – Andiana Feb 18 '16 at 10:48
  • Then create *two* pipes: One where you read output from the child, one where you write input to the child. You *did* check the example in [Creating a Child Process with Redirected Input and Output](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx)? – Some programmer dude Feb 18 '16 at 11:02
  • Yes I had a look in the tutorial, my P1 (child) process writed in C++ (MinGW), I am not very familiar with C++ too. I don't know how to implement it to my case :) But the code seem like just redirect standard I/O between two processes, but not have something like "pause and wait until....". I can compile the second code with CL command, but not the first code, so I can't test the demo completely. – Andiana Feb 19 '16 at 00:45

0 Answers0