Search code examples
c++socketswinapiwinsockiocp

Pass a direct value to CreateIoCompletionPort()'s CompletionKey parameter


When using CreateIoCompletionPort() to associate a SOCKET with a completion port, can I pass a direct value (i.e. not a pointer) to the CompletionKey parameter, or should I only pass a pointer?

What I want to do is to pass the SOCKET value.


Solution

  • The CompletionKey parameter to CreateIoCompletionPort is of type ULONG_PTR. It is large enough to hold a ULONG or a pointer type, whichever is larger. You can pass any value, that fits in a ULONG_PTR. A SOCKET meets this requirement.

    See winsock2.h:

    typedef UINT_PTR SOCKET;