Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simply way of getting all open named pipes

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.

like image 401
schoche Avatar asked Dec 05 '25 06:12

schoche


1 Answers

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);
}
like image 194
Remy Lebeau Avatar answered Dec 07 '25 18:12

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!