How to verify an outbound-only server-end pipe handle is a pipe?

狂风中的少年 提交于 2021-01-27 06:57:09

问题


I want to create an outbound-only named pipe, and I want other code elsewhere to later be able to check if that handle is a pipe. This works for all cases of pipe handles (client, server; outbound, inbound), EXCEPT server-end handles to outbound-only pipes.

If I open a handle to the client end of an inbound-to-server-only pipe with access flags GENERIC_WRITE | FILE_READ_ATTRIBUTES, I can later verify the handle is a pipe by calling GetNamedPipeInfo(handle,NULL,NULL,NULL,NULL), which returns true. However, the server end lacks this privilege for outbound-only pipes — if I call CreateNamedPipe with PIPE_ACCESS_OUTBOUND, GetNamedPipeInfo returns false and GetLastError returns ERROR_ACCESS_DENIED.


回答1:


Figured it out.

bool IsPipe(HANDLE maybePipe)
{
#if 1
    ULONG junk;
    return
        //This returns a false negative for server-side pipe endpoints opened as PIPE_ACCESS_OUTBOUND.
        //I tried or-ing that with FILE_READ_ATTRIBUTES, but CreateNamedPipe doesn't like that.
        ::GetNamedPipeInfo(maybePipe, nullptr, nullptr, nullptr, nullptr)
            //So we fall back onto this one, which returns true in that case,
            // and false for non-pipes.
            || ::GetNamedPipeServerSessionId(maybePipe, &junk);
#elif 0
    return
        //THIS RETURNS TRUE FOR NON-PIPE FILES X-(
        ::GetNamedPipeHandleState(maybePipe, nullptr, nullptr, nullptr, nullptr, nullptr, 0);
#endif
}


来源:https://stackoverflow.com/questions/60378615/how-to-verify-an-outbound-only-server-end-pipe-handle-is-a-pipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!