【问题标题】:How to use NamedPipeServerStream in C#?如何在 C# 中使用 NamedPipeServerStream?
【发布时间】:2021-04-03 10:38:28
【问题描述】:

我正在尝试使用 NamedPipeServerStream 与另一个进程接收和发送消息。我正在开发的平台是 UWP(Microsoft.NETCore.UniversalWindowsPlatform 版本 6.2.11)。

我的第一次尝试如下:

using (NamedPipeServerStream pipe = new NamedPipeServerStream(
            "DynamicCabPipe",
            PipeDirection.InOut,
            10,
            PipeTransmissionMode.Byte,
            PipeOptions.Asynchronous,
            32,
            32)) {...}

结果

System.UnauthorizedAccessException: 'Access to the path is denied'

经过一番研究,我找到了这个解决方案:How to set PipeSecurity of NamedPipeServerStream in .NET Core

var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule("Users", PipeAccessRights.ReadWrite, AccessControlType.Allow));
using (NamedPipeServerStream pipe = NamedPipeServerStreamConstructors.New(
            "DynamicCabPipe",
            PipeDirection.InOut,
            10,
            PipeTransmissionMode.Byte,
            PipeOptions.Asynchronous,
            32,
            32, pipeSecurity)) { ... }

结果

System.Resources.MissingManifestResourceException: 'Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "System.Core.resources" was correctly embedded or linked into assembly "NamedPipeServerStream.NetFrameworkVersion" at compile time, or that all the satellite assemblies required are loadable and fully signed.'

现在我被困住了。有什么建议吗?

【问题讨论】:

  • 管道受制于 UWP 应用的沙盒规则。具体来说,它将始终拒绝在两个进程之间建立管道的尝试。所以“拒绝访问”是预期的结果。
  • vuoriov4,我目前正面临这个问题。对于这个问题,您是否找到了一些可以分享的解决方法,或者您有什么建议可以继续进行?
  • 我的“解决方法”是放弃 UWP 和随之而来的沙盒规则。

标签: c# .net named-pipes


【解决方案1】:

您好vuoriov4,我找到了一种在 UWP 应用程序中创建 NamedPipeServerStream 的方法。我在 UWP 应用程序中导入并使用了原生 C++ 函数 CreateNamedPipe。这里我把我使用的代码和我自己的数据放在一起。

    // This class is in App.xaml.cs file
    sealed partial class App : Application
    {
        // DLL where the C++ function is
        [DllImport("kernel32.dll", SetLastError = true)]
        // C++ function
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode,
           uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize,
           uint nDefaultTimeOut, IntPtr lpSecurityAttributes);

        // C++ variables to create the handle for the Named Pipe 
        static uint PIPE_ACCESS_DUPLEX = 0x00000003;
        static uint PIPE_TYPE_BYTE = 0x00000000;
        static uint PIPE_READMODE_BYTE = 0x00000000;
        static uint PIPE_WAIT = 0x00000000;
        static uint PIPE_UNLIMITED_INSTANCES = 255;

        // ... here is the class' other code ...

        // Method that return the NamedPipeServerStream that is created
        private NamedPipeServerStream GetNamedPipe()
        {
            // Named Pipe handle
            var pipe = CreateNamedPipe(
                @"\\.\pipe\LOCAL\pipeName",     // pipe name
                PIPE_ACCESS_DUPLEX,             // read/write access
                PIPE_TYPE_BYTE |                // byte type pipe 
                PIPE_READMODE_BYTE |            // byte-read mode 
                PIPE_WAIT,                      // blocking mode
                PIPE_UNLIMITED_INSTANCES,       // max. instances
                16000,                          // output buffer size
                16000,                          // input buffer size
                0,                              // client time-out 
                IntPtr.Zero);                   // default security attribute 

            if (pipe.ToInt32() == -1)
            {
                int errorNumber = Marshal.GetLastWin32Error();
                Debug.WriteLine("GetLastWin32Error: " + errorNumber.ToString());
            }
            else
            {
                try
                {
                    var safeHandle = new Microsoft.Win32.SafeHandles.SafePipeHandle(pipe, true);
                    NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeDirection.InOut, false, false, safeHandle);
                    return pipeServer;
                }
                catch (Exception exc)
                {
                    Debug.WriteLine("Error launched: " + exc.Message);
                }

            }

            return null;
        }
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多