获取所有打开的命名管道的简单方法
Simply way of getting all open named pipes
在 C++ 中是否有一种简单的方法来获取所有打开的命名管道,就像在 C# 中一样?
String[] listOfPipes = System.IO.Directory.GetFiles(@"\.\pipe\");
我发现这个 article 其中建议使用不同的方法来获取所有打开的命名管道,不幸的是 c++ c0x 没有。
由于您不能使用 C++17,因此您需要使用 WinAPI 方法遍历目录。那是 FindFirstFile
/ FindNextFile
。尽管名称如此,但如果您查看 \.\pipe\
.
也会找到管道
.NET 的大部分源代码都可以在 https://referencesource.microsoft.com 上公开获得。
如果您查看 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()
函数。
所以,声明:
String[] listOfPipes = System.IO.Directory.GetFiles(@"\.\pipe\");
大致相当于直接使用 Win32 API 的以下 C++ 代码:
#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);
}
在 C++ 中是否有一种简单的方法来获取所有打开的命名管道,就像在 C# 中一样?
String[] listOfPipes = System.IO.Directory.GetFiles(@"\.\pipe\");
我发现这个 article 其中建议使用不同的方法来获取所有打开的命名管道,不幸的是 c++ c0x 没有。
由于您不能使用 C++17,因此您需要使用 WinAPI 方法遍历目录。那是 FindFirstFile
/ FindNextFile
。尽管名称如此,但如果您查看 \.\pipe\
.
.NET 的大部分源代码都可以在 https://referencesource.microsoft.com 上公开获得。
如果您查看 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()
函数。
所以,声明:
String[] listOfPipes = System.IO.Directory.GetFiles(@"\.\pipe\");
大致相当于直接使用 Win32 API 的以下 C++ 代码:
#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);
}