Jump to content
  • Advertisement
Sign in to follow this  

Redirecting I/O to console using pipes

This topic is 2952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.

C++ Syntax (Toggle Plain Text)
  1. HANDLE _pipeRead;
  2. HANDLE _pipeWrite;
  4. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  5. sa.bInheritHandle = true;
  6. sa.lpSecurityDescriptor = 0;
  7. if(!CreatePipe(&_pipeRead, &_pipeWrite, &sa, 0))
  8. {
  9. int error = GetLastError();
  10. std::cout << GetLastError();
  11. }
  14. ZeroMemory(&si,sizeof(STARTUPINFO));
  15. si.cb = sizeof(STARTUPINFO);
  16. si.dwFlags = STARTF_USESTDHANDLES;
  17. si.hStdOutput = _pipeRead;
  18. si.hStdInput = _pipeWrite;
  19. si.hStdError = _pipeWrite;
  20. if(!CreateProcess("Child.exe", 0, 0, 0, true, CREATE_NEW_CONSOLE, 0, 0, &si, &pi))
  21. {
  22. int error = GetLastError();
  23. std::cout << "Error: " << error;
  24. }
  25. while(true)
  26. {
  27. DWORD bytesSent = 0;
  28. WriteFile(_pipeWrite, "Test Message", strlen("Test Message"), &bytesSent, 0);
  29. }

Server message board
C++ Syntax (Toggle Plain Text)
  1. char msgBuffer[25];
  2. DWORD bytesRead = 0;
  3. while(true)
  4. {
  5. ReadFile(GetStdHandle(STD_INPUT_HANDLE), msgBuffer, 25, &bytesRead, 0);
  6. printf(msgBuffer);
  7. }

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 this article. 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 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!