简体   繁体   English

CallNamedPipe和NamedPipeServerStream,访问被拒绝?

[英]CallNamedPipe & NamedPipeServerStream, access denied?

I'm trying to do some IPC between a managed and unmanaged process. 我正在尝试在托管和非托管进程之间进行一些IPC。 I've settled on named pipes. 我已经确定了命名管道。

I'm spinning up a thread in managed code, using NamedPipeServerStream : 我正在使用NamedPipeServerStream在托管代码中启动一个线程:

using (NamedPipeServerStream stream = new NamedPipeServerStream("MyPipe", PipeDirection.In))
{
  while (true)
  {
    stream.WaitForConnection();
    stream.Read(buffer, 0, size);
    //Handle buffer values
  }
}

On the unmanaged side I'm using CallNamedPipe : 在非托管端我使用CallNamedPipe

CallNamedPipe(TEXT("\\\\.\\pipe\\MyPipe"), NULL, 0, pData, dataSize, NULL, NMPWAIT_WAIT_FOREVER);

However, CallNamedPipe fails with a GetLastError of 5 (Access Denied). 但是,CallNamedPipe失败,GetLastError为5(拒绝访问)。 Any idea why? 知道为什么吗?

Here is winning line of code: 这是获胜的代码行:

NamedPipeServerStream pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None)

It should be both-sided, even you're using it only for outgoing data 它应该是双面的,即使您只将其用于传出数据

My guess would be that you are running the processes under two different accounts. 我的猜测是你在两个不同的帐户下运行流程。 Since you are using the NamedPipeStream constructor that uses default security the other user does not have access. 由于您使用的是使用默认安全性的NamedPipeStream构造函数,因此其他用户无权访问。 This can be solved by using the constructor that takes a PipeSecurity instance. 这可以通过使用带有PipeSecurity实例的构造函数来解决。 Then just give the other account access explicitly. 然后明确地给予其他帐户访问权限。

EDIT: I just noticed that you are creating the Pipe as a one-way pipe with the direction in. But I believe that CallNamedPipe attempts to open the pipe for both reading and writing and will fail when connecting to a one-way pipe. 编辑:我刚刚注意到你正在创建Pipe作为方向输入的单向管道。但我相信CallNamedPipe尝试打开管道进行读写,并且在连接到单向管道时会失败。

EDIT 2: Also that constructor creates a byte type pipe and CallNamedPipe can only connect to message type pipes. 编辑2:此构造函数还创建一个字节类型管道,CallNamedPipe只能连接到消息类型管道。 So you'll have to use another constructor. 所以你必须使用另一个构造函数。

Sometimes an access denied may be received when there is a lot of opening and closing of named pipes, similar to the errors described here: Named Pipe Remoting Exceptions . 有时,当有大量打开和关闭命名管道时,可能会收到拒绝访问,类似于此处描述的错误: 命名管道远程处理异常 The solution seems to be to retry making the named pipe after a short delay. 解决方案似乎是在短暂延迟后重试命名管道。

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

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