简体   繁体   English

命名管道中没有字节

[英]No bytes in named pipe

Service: Creates std out,err,in named pipes. 服务:在命名管道中创建std out,err。 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 . 窥视显示ulRemainingBytes 始终为 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. CreateNamesPipe中的安全属性是从方法生成的。 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. PeekNamedPipe()返回应该检查的BOOL

If it fails ( FALSE or 0 ), use GetLastError() to figure out the reason. 如果失败( FALSE0 ),请使用GetLastError()找出原因。

And before that you should also check the return value of CreateFile() ( hStdOutPide ) against INVALID_HANDLE_VALUE . 在此之前,您还应该对照INVALID_HANDLE_VALUE检查CreateFile()hStdOutPide )的返回值。 If the returned handle is invalid that could well be a reason for PeekNamedPipe() to fail. 如果返回的句柄无效,则很可能是PeekNamedPipe()失败的原因。

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. 无论哪种方式,如果CreateFile()失败, GetLastError()都会再次告诉您有关此类问题的信息。

I suspect you are trying to read from the stdout rather than stdin in your child process. 我怀疑您是在尝试从子进程读取stdout而不是stdin。 But from the limited code I cannot confirm this. 但是从有限的代码我无法确认这一点。 Refer to this question for IPC using pipes 有关使用管道的IPC,请参阅此问题

How do I take the output of one program and use it as the input of another on C++? 如何在C ++上将一个程序的输出用作另一个程序的输入?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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