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

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);
}

Server message board

char msgBuffer[25];
DWORD bytesRead = 0;

while(true)
{
    ReadFile(GetStdHandle(STD_INPUT_HANDLE), msgBuffer, 25, &bytesRead, 0);

    printf(msgBuffer);
}

I would expect that I would see the message "Test Message" being repeated on the Server Message Board console.

Can anyone clear things up?

Few notes:
1. Child's stdout has been redirected to the pipe (line 21), hence don't expect anything on console.
2. Redirection (line 22) causes child to read from the writable end of the pipe.

Few notes:
1. Child's stdout has been redirected to the pipe (line 21), hence don't expect anything on console.
2. Redirection (line 22) causes child to read from the writable end of the pipe.

It seems through all the examples I have looked at, this is how they set it up, unless I have misinterpreted what I have seen.

Could you explain a little bit more about what is wrong and how I can fix it? Obviously my understanding is incorrect =p. I just want to send a char buffer from the server to the server message board and display the message there. Should be simple but the problem is giving me a headache!

I suppose that you need something like

si.hStdOutput = _PipeRead;
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);

Wow that was a simple fix! Thank you so much. I guess looking back over it, and the fix you gave me, it makes more sense now.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.