简体   繁体   中英

No bytes in named pipe

Service: Creates std out,err,in named pipes. Attaches them to a process that it creates.

HANDLE hStdOut = CreateNamedPipe(szStdOutPipeName,
            PIPE_ACCESS_DUPLEX,
            PIPE_TYPE_BYTE,
            1,
            100,
            100,
            15000,
            pSa);
yStartupInfo.hStdOutput = hStdOut;
CreateProcessAsUserW( ..., yStartupInfo, ... );

This part works. The created process's output is being redirected into the pipes.

Client: Connection to named pipe, Successful. Check if there are bytes to read with a peek (at this point bytes are pushed into the pipe!). Then read the bytes from the pipe.

hStdOutPide = CreateFileW(szPipeNameStdOut,
                          GENERIC_READ|GENERIC_WRITE,
                          FILE_SHARE_READ|FILE_SHARE_WRITE,
                          NULL, 
                          OPEN_EXISTING, 
                          0, 
                          NULL );
PeekNamedPipe(hStdOutPide,
              szBuffer,
              kunBufferSize,
              &ulBytesRead,
              &ulBytesAvailable,
              &ulRemainingBytes);
if( ulBytesAvailable > 0)
      ReadFile(hStdOutPide, szBuffer, 1000, &ulBytesRead, NULL)

I have removed the surrounding code that makes sure the handles are valid and the process is running and so on.

The Peek reveals that ulRemainingBytes is ALWAYS 0 . Does anyone see where my error could be? I have been trying to get this to work for some time and don't know what the proper flags for anything is anymore. Please ask if you need more information!

Security Attributes in the CreateNamesPipe are generated from a method. It is used in many other places in the code so I do not believe the problem to be there.

Thanks.

PeekNamedPipe() returns a BOOL that should be checked.

If it fails ( FALSE or 0 ), use GetLastError() to figure out the reason.

And before that you should also check the return value of CreateFile() ( hStdOutPide ) against INVALID_HANDLE_VALUE . If the returned handle is invalid that could well be a reason for PeekNamedPipe() to fail.

All in all you seem to be taking too many (good) results for taken. Don't! Check your return codes and never rely on luck.

Just one more thing: it's possible that the sharing flags are set improperly. Make sure that they don't conflict with what you want to do. Either way, GetLastError() will again tell you about such issue in case CreateFile() failed.

I suspect you are trying to read from the stdout rather than stdin in your child process. But from the limited code I cannot confirm this. Refer to this question for IPC using pipes

How do I take the output of one program and use it as the input of another on C++?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM