我想创建一个仅用于出站的命名管道,并且我希望其他地方的其他代码稍后能够检查该句柄是否是管道。这适用于所有管道句柄(客户端,服务器,出站,入站),除仅出站管道以外的服务器端句柄。
如果打开带有访问标志GENERIC_WRITE | FILE_READ_ATTRIBUTES
的仅服务器入站管道的客户端的句柄,我稍后可以通过调用GetNamedPipeInfo(handle,NULL,NULL,NULL,NULL)
来验证该句柄是一个管道,该过程返回true。但是,服务器端缺少仅出站管道的特权-如果我用CreateNamedPipe
调用PIPE_ACCESS_OUTBOUND
,则GetNamedPipeInfo
返回false,GetLastError
返回ERROR_ACCESS_DENIED
。
想通了。
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
}