Sign in to follow this  
LevyDee

Redirecting I/O to console using pipes

Recommended Posts

Ive been trying to understand this subject for two days now, but can't seem to get it working. My setup is two consoles, a Server console, and a server message board console. The Server message board is a child process of my Server console. The Server will send messages to the Server message board for output.

AKA Server detects error, sends the error message to the server message board to output to console screen.

Server
C++ Syntax ([url="http://www.daniweb.com/software-development/cpp/threads/387164/1667607#"]Toggle Plain Text[/url]) [list=1][*]HANDLE _pipeRead;[*]HANDLE _pipeWrite;[*][*]SECURITY_ATTRIBUTES sa;[*]sa.nLength = sizeof(SECURITY_ATTRIBUTES);[*]sa.bInheritHandle = true;[*]sa.lpSecurityDescriptor = 0;[*][*]if(!CreatePipe(&_pipeRead, &_pipeWrite, &sa, 0))[*]{[*] int error = GetLastError();[*] std::cout << GetLastError();[*]}[*][*]PROCESS_INFORMATION pi;[*][*]STARTUPINFO si;[*]ZeroMemory(&si,sizeof(STARTUPINFO));[*]si.cb = sizeof(STARTUPINFO);[*]si.dwFlags = STARTF_USESTDHANDLES;[*]si.hStdOutput = _pipeRead;[*]si.hStdInput = _pipeWrite;[*]si.hStdError = _pipeWrite;[*][*]if(!CreateProcess("Child.exe", 0, 0, 0, true, CREATE_NEW_CONSOLE, 0, 0, &si, &pi))[*]{[*] int error = GetLastError();[*] std::cout << "Error: " << error;[*]}[*][*]while(true)[*]{[*] DWORD bytesSent = 0;[*] WriteFile(_pipeWrite, "Test Message", strlen("Test Message"), &bytesSent, 0);[*]}[/list]
Server message board
C++ Syntax ([url="http://www.daniweb.com/software-development/cpp/threads/387164/1667607#"]Toggle Plain Text[/url]) [list=1][*]char msgBuffer[25];[*]DWORD bytesRead = 0;[*][*]while(true)[*]{[*] ReadFile(GetStdHandle(STD_INPUT_HANDLE), msgBuffer, 25, &bytesRead, 0);[*][*] printf(msgBuffer);[*]}[/list]
I would expect that I would see the message "Test Message" being repeated on the Server Message Board console.

Can anyone clear things up?

Share this post


Link to post
Share on other sites
You might want to compare your code to the code in [url=http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx] this article[/url]. One thing you should pay attention to in particular is that in your code you've redirected stdout, which is what printf() prints to, to the pipe you created.

Share this post


Link to post
Share on other sites
I have been using MSDN to try and figure this out, but the problem with that article you linked, is that the child takes input from the parent, and then sends a message back to the parent. I just want to send one way, Parent -> Child[b] [/b]not Parent -> Child -> Parent

Share this post


Link to post
Share on other sites
Then don't create the pipe that redirects the child's output and use the regular standard output handle for the child's stdout in the STARTUPINFO structure. Again, currently you've redirected the child's stdout handle to the pipe you created. If you don't want the child's output directed into the pipe, don't set the pipe as the child's stdout.

Share this post


Link to post
Share on other sites
Thank you for the response! I just got a similar response on another forum I was asking on, and they said basically the same thing.
My understanding wasn't 100% correct and I have got the problem fixed. Thank you for your time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this