2013-11-04 149 views
4

我需要打开某个命名管道,以便模糊测试它,但是我的测试代码无法访问用于生成命名管道名称的相同数据。但是,我可以识别管道的名称,然后使用该名称打开管道进行模糊处理。如何枚举进程中所有命名管道的名称?

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

但是似乎不会因为某些原因命名管道的工作。

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

+0

您是否特别需要仅在当前进程中枚举管道?我已经有一个用于Windows的命名管道枚举,但它是系统范围的。 – Keith4G

+0

我只需要遍历当前进程中的命名管道,但我完全可以使用枚举系统上的所有管道。 – mamidon

回答

2

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

在MinGW中使用-fpermissive构建时可以使用。它应该与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; 
} 
+0

我需要一点来验证这适用于我,但它看起来很合理。我会在大约一两天内积极解决这个问题。谢谢! – mamidon

+1

我只会注意到这在Windows上运行良好 - 对目标字符串进行了轻微修改。我不知道为什么OP使用这样的宏。 – mamidon

+0

在该点的所有代码中,“//./pipe/”和L“//./ pipe /”之间的切换很简单。这并不是说它在样本中很重要,因为它很短。 – Keith4G