使用C#FileStream创建Windows命名管道

FileStream类的docs中,Remarks部分显示:

  

使用FileStream类读取,写入,打开和关闭文件系统上的文件,并操纵其他与文件相关的操作系统句柄,包括管道,标准输入和标准输出。

我知道System.IO.Pipes可以在Windows中通过管道创建,读取和写入,但是由于我目前正在从事的项目中的限制,我无权访问该程序包。看来FileStreams可以与管道交互,但是我似乎找不到任何有关如何执行此操作的示例。

在Windows中,如何直接使用FileStream通过命名管道进行基本I / O?

gajba 回答:使用C#FileStream创建Windows命名管道

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Test
{

    class Program
    {
        [DllImport("kernel32.dll",SetLastError = true)]
        static extern IntPtr CreateNamedPipe(string lpName,uint dwOpenMode,uint dwPipeMode,uint nMaxInstances,uint nOutBufferSize,uint nInBufferSize,uint nDefaultTimeOut,IntPtr lpSecurityAttributes);
        static uint PIPE_ACCESS_DUPLEX = 0x00000003;
        static uint PIPE_ACCESS_INBOUND = 0x00000001;
        static uint PIPE_ACCESS_OUTBOUND = 0x00000002;
        static uint FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000;
        static uint FILE_FLAG_WRITE_THROUGH = 0x80000000;
        static uint FILE_FLAG_OVERLAPPED = 0x40000000;
        static uint WRITE_DAC = 0x00040000;
        static uint WRITE_OWNER = 0x00080000;
        static uint ACCESS_SYSTEM_SECURITY = 0x01000000;
        //One of the following type modes can be specified. The same type mode must be specified for each instance of the pipe.
        static uint PIPE_TYPE_BYTE = 0x00000000;
        static uint PIPE_TYPE_MESSAGE = 0x00000004;
        //One of the following read modes can be specified. Different instances of the same pipe can specify different read modes
        static uint PIPE_READMODE_BYTE = 0x00000000;
        static uint PIPE_READMODE_MESSAGE = 0x00000002;
        //One of the following wait modes can be specified. Different instances of the same pipe can specify different wait modes.
        static uint PIPE_WAIT = 0x00000000;
        static uint PIPE_NOWAIT = 0x00000001;
        //One of the following remote-client modes can be specified. Different instances of the same pipe can specify different remote-client modes.
        static uint PIPE_ACCEPT_REMOTE_CLIENTS = 0x00000000;
        static uint PIPE_REJECT_REMOTE_CLIENTS = 0x00000008;


        static void Main(string[] args)
        {

            Console.WriteLine("Hello World!");
            var name = "test";
            var p = CreateNamedPipe(@"\\.\pipe\" + name,PIPE_ACCESS_OUTBOUND,PIPE_TYPE_MESSAGE | PIPE_WAIT,255,4096,0xffffffff,IntPtr.Zero);
            if (p.ToInt32() == -1)
            {
                throw new Exception("Error creating named pipe " + name + " . Internal error: " + Marshal.GetLastWin32Error().ToString());
            }
            var fs = new FileStream(new Microsoft.Win32.SafeHandles.SafeFileHandle(p,true),FileAccess.Write);
            fs.Close();
        }
    }
}

这似乎工作正常。比我希望的要难看些,但并非没有可能。感谢@David Browne向我指出正确的方向。

更完整的示例:https://www.pinvoke.net/default.aspx/kernel32.createnamedpipe

本文链接:https://www.f2er.com/2919986.html

大家都在问