Is it possible to change the ULONG_PTR completion key associated to an HANDLE in Windows I/O Completion Ports?

StackOverflow https://stackoverflow.com/questions/19196920

Question

I associate an HANDLE "h_server" that I create with CreateNamedPipe() with an I/O Completion Port, and for the completion key I use a function pointer: namedpipe_server_completion_routine().

Now, when a named pipe client connection request triggers, that HANDLE "h_server" became the client endpoint in the named pipe server application, so I should change its I/O Completion Key to another kind of routine pointer, the one that completes I/O, Read, Write etc, io_arrival_completion_routine(), which is different from the named pipe server completion routine.

I thought to call again CreateIoCompletionPort() with the new completion key on that HANDLE, but it seems not working, so there is a way to change that key? Maybe with some hacks with DuplicateHandle or something?

If not, how I could solve this?

NOTE: this problem is only present with named pipe servers, because AcceptEx() works in a different way: with tcp servers, when a connection arrives, you basically got a new HANDLE for the client endpoint on the server, so you can associate that new HANDLE with the io completion port and the correct completion key as the correct io_arrival_completion_routine(), while the HANDLE of the tcp server associated with the tcp_server_completion_routine() will remain correct, and its unique role will be to listen for new connections.

Was it helpful?

Solution

Change the completion key from being a pointer to a function to being a pointer to a data block. This data block can then include whatever status information you need.

In the simplest case the data block would just contain the function pointer you're currently trying to use as the completion key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top