简体   繁体   English

设置命名管道消息模式

[英]set named pipe message mode

I have the following piece of code: 我有以下代码:

//...
SafeFileHandle handle = NativeMethods.CreateFile(pipeName, FileAccess.ReadWrite, FileShare.None, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
if (handle.IsInvalid)
    continue;
uint mode = (uint)PipeMode.ReadModeMessage; // mode==2
bool result = NativeMethods.SetNamedPipeHandleState(handle, ref mode, IntPtr.Zero, IntPtr.Zero);
//...

The problem is that the call to 'SetNamedPipeHandleState' fails: result is false and GetLastError() returns 5 (ERROR_ACCESS_DENIED). 问题是对“ SetNamedPipeHandleState”的调用失败:结果为false,GetLastError()返回5(ERROR_ACCESS_DENIED)。 Other than that the pipe works just fine - I can read and write data. 除此之外,管道还可以正常工作-我可以读写数据。 Except, of course, that it is not working in message mode - eg. 当然,除了它不在消息模式下工作-例如。 contents of two WriteFile message calls gets read by single ReadFile call. 两个WriteFile消息调用的内容由单个ReadFile调用读取。 What am I doing wrong here? 我在这里做错了什么?

CreateFile opens the client end of a named pipe already created by something else acting as the pipe server. CreateFile打开已由其他人作为管道服务器创建的命名管道的客户端。 You don't tell us in the question how your pipe is created. 您没有在问题中告诉我们如何创建管道。 The pipe will only work in message mode if the PipeMode was specified as PIPE_TYPE_MESSAGE when the pipe was created by the pipe server. 如果在管道服务器创建管道时将PipeMode指定为PIPE_TYPE_MESSAGE ,则管道将仅在消息模式下工作。

If the pipe is created in message mode, then the pipe client can choose whether to read in message mode or in byte mode. 如果管道是在消息模式下创建的,则管道客户端可以选择是以消息模式还是字节模式进行读取。 If the pipe isn't in message mode, no attempt by a pipe client to set the read mode to message ( PIPE_READMODE_MESSAGE ) will have any effect, as it won't change the pipe mode. 如果管道不在消息模式下,则管道客户端不会尝试将读取模式设置为消息( PIPE_READMODE_MESSAGE ),因为它不会更改管道模式,因此不会起作用。

You also don't show us how you have implemented your NativeMethods , but if your CreateFile parameters are mapped directly to the arguments of the Win32 CreateFile function, you are only requesting FILE_READ_DATA and FILE_WRITE_DATA access rights for your pipe handle. 您也没有向我们展示如何实现NativeMethods ,但是,如果将CreateFile参数直接映射到Win32 CreateFile函数的参数,则仅要求管道句柄具有FILE_READ_DATAFILE_WRITE_DATA访问权限。 These rights are not sufficient to allow you to call SetNamedPipeHandleState , which explains the access denied error. 这些权限不足以允许您调用SetNamedPipeHandleState ,它说明了拒绝访问错误。 See the Win32 API documentation : 请参阅Win32 API文档

The handle must have GENERIC_WRITE access to the named pipe for a write-only or read/write pipe, or it must have GENERIC_READ and FILE_WRITE_ATTRIBUTES access for a read-only pipe. 对于只读或读/写管道,句柄必须具有对命名管道的GENERIC_WRITE访问,或者对于只读管道,句柄必须具有GENERIC_READ和FILE_WRITE_ATTRIBUTES访问。

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

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