How to distinguish a Win32 handle from other pipe handles?

I need to determine if the descriptor that my code did not generate is for which GetFileType()==FILE_TYPE_PIPEis a socket or not. There is no API for this.

I have tried the following. The general idea is to use a socket-specific function and treat the failure as a non-socket value.

  • getsockopt()- This was my first attempt. Unfortunately, this is similar to freezing when called by many threads on the same (non-socket) descriptor.
  • WSAEnumNetworkEvents() - this is what Gnulib does, but will have unwanted side effects if the handle is a socket.
  • getpeername()is what cygwin does, but it won’t work for some sockets either. Guess if the error does not mean that the socket does not seem reliable and safe in the future.

I do not mind if the solution only works on some versions of Windows, for example. Vista, I can always return to another method in the general case.

+5
source share
3 answers

I think that perhaps you could try calling GetNamedPipeInfo () on your handle. If the call is completed, you know that the handle is a channel handle, otherwise it must be a socket.

+2
source

Have you tried WSADuplicateSocket. Then just check WSAPROTOCOL_INFOto make sure this is actually a named pipe ...

+1
source

You can also use GetNamedPipeHandleState (), evaluating the result with GetLastError ().

0
source

All Articles