winapi - 如何验证仅出站服务器端管道句柄是管道?
问题描述
我想创建一个仅限出站的命名管道,并且我希望其他地方的其他代码稍后能够检查该句柄是否是管道。这适用于管道句柄(客户端、服务器;出站、入站)的所有情况,除了仅出站管道的服务器端句柄。
如果我使用访问标志打开一个到仅入站到服务器的管道的客户端的句柄GENERIC_WRITE | FILE_READ_ATTRIBUTES
,我稍后可以通过调用来验证该句柄是一个管道GetNamedPipeInfo(handle,NULL,NULL,NULL,NULL)
,这将返回 true。但是,对于仅出站管道,服务器端缺少此特权-如果我调用CreateNamedPipe
with 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
}
推荐阅读
- html - 物化工具提示卡在页面顶部
- openshift - Chaos Toolkit 是否支持基于 CRI-O 的运行时?例如它可以在 OpenShift Container Platform v4.3 上运行吗?
- c++ - glUniform() 的无效位置试图做点阴影
- nginx - nginx - 如何使用 stub_status 创建 /status
- python-3.8 - Python 3.8.3 ModuleNotFoundError:没有名为“_sqlite3”的模块
- javascript - 正则表达式调试
- daemon - 来自守护进程的 Docker 错误响应:获取 https://registry-1.docker.io/v2/: dial tcp:lookup registry-1.docker.io on 192.168.65.1:53
- firebase - 使用 2 个参数创建 Firebase 动态链接
- python - 使用条件修改列表列表中的值
- unity3d - 在 Unity3D 中使用 MSAL .NET 库时未显示窗口提示