Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of a named pipe given handle

I'm trying to find the name of a named pipe given it's handle. I found a solution where the named pipe handle is duplicated using NtDuplicateObject and then the name is extracted using NtQueryObject but it's unstable so that's out of the question.

Currently I'm attempting to do so with GetFinalPathNameByHandle but having no luck. I'm not even sure if it's possible to do this but it was mentioned as a potential solution so I'm going with it. The following is adapted from the example code at:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364962(v=vs.85).aspx

void __cdecl _tmain(int argc, TCHAR *argv[]){
   TCHAR Path[BUFSIZE];
   HANDLE pipe;
   DWORD dwRet;

   printf("\n");
   if (argc != 2)
   {
      printf("ERROR:\tIncorrect number of arguments\n\n");
      printf("%s <file_name>\n", argv[0]);
      return;
   }

   pipe = CreateNamedPipe(argv[1], PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND, PIPE_WAIT, 1,
          1024, 1024, 120 * 1000, NULL);

   if (pipe == INVALID_HANDLE_VALUE)
   {
      printf("Could not open file (error %d\n)", GetLastError());
      return;
   }

   dwRet = GetFinalPathNameByHandle(pipe, Path, BUFSIZE, VOLUME_NAME_NT);
   if (dwRet < BUFSIZE)
   {
      _tprintf(TEXT("\nThe final path is: %s\n"), Path);
   }
   else printf("\nThe required buffer size is %d.\n", dwRet);

   CloseHandle(pipe);}

The command line parameter is "\\\\.\\pipe\\mynamedpipe" or "\\.\pipe\mynamedpipe", I've tried both. The output is garbage but more importantly when debugging with Visual Studio 2013 Express while stepping through the program the path variable is garbage directly after the GetFinalPathNameByHandle call.

By garbage I mean:

  • Path 0x0036fab4 L"쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌... wchar_t[100]

Console output is:

  • The final path is: ?????????????????????????????????????????????????????????????????????????????????????????????????????????6?╫☻

So I'm officially stuck. Alternatively and possibly a much better solution would be comparing two named pipe handles to each other to determine if they point to the same named pipe. If there is a way to do that it would solve my problem as well.

like image 492
GQG83 Avatar asked Jan 18 '26 03:01

GQG83


1 Answers

To answer my own question here. GetFileInformationByHandleEx does exactly this using FileNameInfo for the FileInformationClass parameter and a handle generated by CreateNamedPipe.

like image 83
GQG83 Avatar answered Jan 19 '26 19:01

GQG83



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!