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:
Console output is:
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.
To answer my own question here. GetFileInformationByHandleEx does exactly this using FileNameInfo for the FileInformationClass parameter and a handle generated by CreateNamedPipe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With