Win32 Stream Handles - Changing To Binary Mode

2019-07-16 11:55发布

问题:

I'm using CreatePipe to create handles that I then pass on when launching a child process.

The child process writes binary information to its standard out, which is redirected into the pipe I've created.

However, when it comes to writing the number '10', things go haywire and I get too many characters of output - I'm assuming this is because the stream is open in text mode and it's automatically adding a 13.

Is there any way in Win32 that I can take the HANDLE returned from CreatePipe and change the stream mode to binary, the same way I could with the function _setmode, if I had a FILE*? Or is there a way to translate he handle into a FILE* so I can use _setmode?

Sample Code:

HANDLE hOutputReadTmp,hInputWriteTmp;

SECURITY_ATTRIBUTES sa;

bool Binary = true;

// Set up the security attributes struct.
sa.nLength= sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;

if (!CreatePipe(&hInputRead,&hInputWriteTmp,&sa,0))
{
    Error = "Unable to Create Input Pipe";
    return false;
};

if (Binary == true) 
{
    //Put a magic something here to change the hInputRead stream to Binary.
    //_setmode(_fileno(hInputRead,_O_BINARY);
}

EDIT

This is probably more to do with the CHILD PROCESS writing to stdout as a text stream. Give me a minute to take a look at this and I might come back and delete this whole post!

标签: c winapi pipe