Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a list of all open named pipes in Windows and avoiding possible exceptions?

Getting the list of named pipes is in ideal case pretty simple and can be found here: How can I get a list of all open named pipes in Windows?

But mentioned solution

var namedPipes = Directory.GetFiles(@"\\.\pipe\");

has occasionally unpredictable results. One of them was mentioned in the link above (Invalid character in path exception). Today I met my own exception:

ArgumentException "The second path fragment must not be a drive or UNC name. Parameter name: path2".

Question is whether there is any really working solution in .net to get the list of all opened named pipes? Thanks

like image 613
user2126375 Avatar asked Aug 03 '14 21:08

user2126375


People also ask

Where are named pipes on Windows?

Every pipe is placed in the root directory of the named pipe filesystem (NPFS), mounted under the special path \\. \pipe\ (that is, a pipe named "foo" would have a full path name of \\. \pipe\foo ).

What are Windows named pipes?

A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client/server communication.

How do you open a named pipe?

A name-pipe can be opened with either open() or fopen() by a single process. Should you require bidirectional communi- cation between two processes then two FIFO files have to be established witch each one implementing a unidirectional channel of communication.

What is Powershell named pipe IPC?

Named pipes provide one-way or duplex pipes for communication between a pipe server and one or more pipe clients. Named pipes can be used for interprocess communication locally or over a network. A single pipe name can be shared by multiple NamedPipeClientStream objects.


1 Answers

I dug into Directory class source code and found an inspiration. Here is a working solution which gives you list of all opened named pipes. My result does not contain \\.\pipe\ prefix as it can be seen in result of Directory.GetFiles. I tested my solution on WinXp SP3, Win 7, Win 8.1.

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    struct WIN32_FIND_DATA
    {
        public uint dwFileAttributes;
        public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
        public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
        public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
        public uint nFileSizeHigh;
        public uint nFileSizeLow;
        public uint dwReserved0;
        public uint dwReserved1;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string cFileName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
        public string cAlternateFileName;
    }

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);


    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA
       lpFindFileData);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool FindClose(IntPtr hFindFile);

    private static void Main(string[] args)
    {
        var namedPipes = new List<string>();
        WIN32_FIND_DATA lpFindFileData;

        var ptr = FindFirstFile(@"\\.\pipe\*", out lpFindFileData);
        namedPipes.Add(lpFindFileData.cFileName);
        while (FindNextFile(ptr, out lpFindFileData))
        {
            namedPipes.Add(lpFindFileData.cFileName);
        }
        FindClose(ptr);

        namedPipes.Sort();

        foreach (var v in namedPipes)
            Console.WriteLine(v);

        Console.ReadLine();
     }
like image 148
user2126375 Avatar answered Nov 14 '22 22:11

user2126375