Redirecting I/O to console using pipes

Started by
3 comments, last by LevyDee 12 years, 6 months ago
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 (Toggle Plain Text)
  1. HANDLE _pipeRead;
  2. HANDLE _pipeWrite;
  3. SECURITY_ATTRIBUTES sa;
  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. }
  12. PROCESS_INFORMATION pi;
  13. STARTUPINFO si;
  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?
Advertisement
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.
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
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.
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.

This topic is closed to new replies.

Advertisement