簡體   English   中英

如何枚舉進程中所有命名管道的名稱?

[英]How to Enumerate Names of All Named Pipes in a Process?

我需要打開某個命名管道,以便我可以進行模糊測試,但是我的測試代碼無法訪問用於生成命名管道名稱的相同數據。 但是,我可以識別管道的名稱,然后使用該名稱打開管道進行模糊測試。

我使用此論壇帖子開始枚舉系統上句柄的名稱: 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM