0

Is there a simpel way to get all open named pipes in c++, like there is in c#?

String[] listOfPipes = System.IO.Directory.GetFiles(@"\\.\pipe\");

I found this article where differen methodes where proposed to get all open named pipes, unfortunately nothing for c++ c0x.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
schoche
  • 33
  • 1
  • 2
  • 12
  • 2
    As long as you’re on Windows, the exact same code works in C++. Just use the C++ functions for doing directory listings. – Konrad Rudolph Sep 08 '20 at 13:21
  • C++0x? Not even C++11? You'd need C++17 for this. – MSalters Sep 08 '20 at 13:30
  • 1
    check [MS docs](https://learn.microsoft.com/en-us/windows/win32/ipc/named-pipe-type-read-and-wait-modes) for appropriate API – Sugar Sep 08 '20 at 13:30
  • 1
    Main idea - named pipes is not a c++ feature, but windows specific api. It's pretty outdated and i think doesn't really have any improvements like c# have. So you probably should build something like this yourself using api from documentation I linked above – Sugar Sep 08 '20 at 13:34
  • i have to add an feature to old legacy code, im working with Visualstudio 2008. The server, client and communication part is already working. But at the moment i have to enter the named pipe name/handel hardcoded. – schoche Sep 08 '20 at 13:45
  • 1
    @MSalters Of course you *don’t* need C++17. OP is writing code for Windows. WinAPI is entirely sufficient. – Konrad Rudolph Sep 08 '20 at 13:51

2 Answers2

4

Since you can't use C++17, you'll need the WinAPI way of iterating over a directory. That's FindFirstFile / FindNextFile. Despite the name, that will also find pipes if you look in \\.\pipe\.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • It should be expected. The named-pipe filesystem (NPFS) has a root directory at `"\\Device\\NamedPipe\\"` (i.e. `"\\\\.\\PIPE\\"`) that contains named pipe files and supports directory queries such as `FileDirectoryInformation`. Interestingly, NPFS doesn't support subdirectories, so slashes are allowed in names. Also, in the directory entry, the allocation size is the max number of instances, and the size is current number. But in the standard info of an instance of the pipe, the allocation size is the total quota in bytes, and the size is the number of bytes queued at the queried pipe end. – Eryk Sun Sep 08 '20 at 22:11
3

Much of .NET's source code is openly available on https://referencesource.microsoft.com.

If you look at the source code for the System.IO.Directory class, its GetFiles() method creates a TList<String> using an IEnumerable<String> from FileSystemEnumerableFactory.CreateFileNameIterator(), and then converts that TList<String> to a String[] array, where FileSystemEnumerableIterator internally uses the Win32 API FindFirstFile() and FindNextFile() functions.

So, the statement:

String[] listOfPipes = System.IO.Directory.GetFiles(@"\\.\pipe\");

Would be roughly equivalent to the following C++ code using the Win32 API directly:

#include <windows.h>
#include <string>
#include <vector>

std::vector<std::wstring> listOfPipes;

std::wstring prefix(L"\\\\.\\pipe\\");

WIN32_FIND_DATAW fd;
HANDLE hFind = FindFirstFileW((prefix + L"*").c_str(), &fd);
if (hFind == INVALID_HANDLE_VALUE)
{
    // error handling...
}
else
{
    do
    {
        listOfPipes.push_back(prefix + fd.cFileName);
    }
    while (FindNextFileW(hFind, &fd));

    // error handling...

    FindClose(hFind);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770