c++ - 如何枚举进程中所有命名管道的名称?

标签 c++ c windows named-pipes

我需要打开某个命名管道以便对其进行模糊测试,但是我的测试代码无法访问用于生成命名管道名称的相同数据。但是我可以识别管道的名称,然后使用该名称打开管道进行模糊测试。

我使用这个论坛帖子开始枚举系统上句柄的名称: http://forum.sysinternals.com/howto-enumerate-handles_topic18892.html

但是由于某些原因,它似乎不适用于命名管道。

TL;DR:我需要使用哪些 API 来列出 Windows 上当前进程中所有命名管道的名称?

最佳答案

这将枚举系统中的所有命名管道,或者至少让您朝着正确的方向迈出一步。

当使用 -fpermissive 构建时,这在 MinGW 中有效。它应该适用于 MSVC 中的类似设置。

#ifndef _WIN32_WINNT
// Windows XP
#define _WIN32_WINNT 0x0501
#endif

#include <Windows.h>
#include <Psapi.h>


// mycreatepipeex.c is at http://www.davehart.net/remote/PipeEx.c
// I created a simple header based on that.    
#include "mycreatepipeex.h"

#include <iostream>
#include <cstdio>
#include <errno.h>

void EnumeratePipes()
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

#define TARGET_PREFIX "//./pipe/"
    const char *target = TARGET_PREFIX "*";

    memset(&FindFileData, 0, sizeof(FindFileData));
    hFind = FindFirstFileA(target, &FindFileData);
    if (hFind == INVALID_HANDLE_VALUE) 
    {
        std::cerr << "FindFirstFileA() failed: " << GetLastError() << std::endl;
        return;
    }
    else 
    {
        do
        {
            std::cout << "Pipe: " << TARGET_PREFIX << FindFileData.cFileName << std::endl;
        }
        while (FindNextFile(hFind, &FindFileData));

        FindClose(hFind);
    }
#undef TARGET_PREFIX

    return;
}

int main(int argc, char**argv)
{
    HANDLE read = INVALID_HANDLE_VALUE;
    HANDLE write = INVALID_HANDLE_VALUE;
    unsigned char pipe_name[MAX_PATH+1];

    BOOL success = MyCreatePipeEx(&read, &write, NULL, 0, 0, 0, pipe_name);

    EnumeratePipes();

    if ( success == FALSE )
    {
        std::cerr << "MyCreatePipeEx() failed: " << GetLastError() << std::endl;
        return 1;
    }

    FILE *f = fopen((const char*)pipe_name, "rwb");
    if ( f == NULL )
    {
        std::cerr << "fopen(\"" << pipe_name << "\") failed: " << (int)errno << std::endl;
    }

    CloseHandle(read);
    CloseHandle(write);

    return 0;
}

关于c++ - 如何枚举进程中所有命名管道的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19778885/

相关文章:

python - 目录存在时 Shutil.copy IO Error2

c - Realloc:重置结构中的灵活结构数组

c - 如何用 C 语言编写一个程序,该程序从用户那里读取一个字,并在用户输入字 "exit"时终止?

python - 我应该在哪里保存应用程序的数据?

c++ - 光线追踪 : Only use single ray instead of both reflection & refraction rays

c - 将数组传递给 C 中的递归函数

windows - -ErrorAction stop之后,脚本处理不会停止

c++ - GetProcAddress 和函数指针 - 这是正确的吗?

c++ - 我如何从 cpp 中的任何给定数字的右边获得第三位数字?

c++ - 即使在C++中包含类头文件,类也没有命名类型错误?