【问题标题】:How to Enumerate Names of All Named Pipes in a Process?如何枚举进程中所有命名管道的名称?
【发布时间】:2013-11-04 22:57:21
【问题描述】:

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

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

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

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

【问题讨论】:

  • 您是否特别需要仅在当前进程中枚举管道?我已经有一个适用于 Windows 的命名管道枚举,但它是系统范围的。
  • 我只需要遍历当前进程中的命名管道,尽管我完全可以枚举系统上的所有管道。

标签: c++ c windows named-pipes


【解决方案1】:

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

当使用 -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;
}

【讨论】:

  • 我需要一点来验证这对我有用,但它看起来很合理。我会在一两天内投票并解决这个问题。谢谢!
  • 我会注意到这在 Windows 上运行良好 - 对目标字符串进行了轻微的编辑。我不确定为什么 OP 使用这样的宏。
  • 此时所有代码中的“//./pipe/”和L“//./pipe/”之间很容易切换。这在样本中并不重要,因为它很短。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多