How to reregister for IO Completion ports for a handle

John Seif

I have a Windows named pipe that I create with CreateFile (the server side was created using CreateNamedPipe). I use IO completion ports to read/write data asynchronously on both ends.

I need to send these handles to other processes after they've been opened. I tried to call CloseHandle on the handle returned from CreateIoCompletionPort, and then in the other process call CreateIoCompletionPort again. However it always fails and GetLastError returns 87 (ERROR_INVALID_PARAMETER).

I can also reproduce this in just one process, see below. Note there are no outstanding reads/write to the object before I send it.

std::wstring pipe_name = L"\\\\.\\pipe\\test.12345";
HANDLE server = CreateNamedPipeW(
    pipe_name.c_str(),
    PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
    PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
    1,
    4096,
    4096,
    10000,
    NULL);
SECURITY_ATTRIBUTES security_attributes = {
    sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
HANDLE client = CreateFileW(
    pipe_name.c_str(), GENERIC_READ | GENERIC_WRITE,
    0,
    &security_attributes,
    OPEN_EXISTING,
    SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS | FILE_FLAG_OVERLAPPED,
    NULL);
ULONG_PTR key = 1;
HANDLE comp_port = CreateIoCompletionPort(client, NULL, key, 1);

BOOL b1 = CloseHandle(comp_port);

comp_port = CreateIoCompletionPort(client, NULL, key, 1);
if (comp_port == NULL) {
  int last_err = GetLastError();
}
Harry Johnston

Referring to the documentation for CreateIoCompletionPort:

A handle can be associated with only one I/O completion port, and after the association is made, the handle remains associated with that I/O completion port until it [the handle] is closed.

[...] The I/O completion port handle and every file handle associated with that particular I/O completion port are known as references to the I/O completion port. The I/O completion port is released when there are no more references to it.

In other words, closing the I/O completion port handle doesn't achieve anything. The I/O completion port still exists and is permanently associated with the pipe handle. What you're attempting simply isn't possible; you will need to rearchitecture.

Note also:

It is best not to share a file handle associated with an I/O completion port by using either handle inheritance or a call to the DuplicateHandle function. Operations performed with such duplicate handles generate completion notifications. Careful consideration is advised.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

IO Completion Ports (IOCP)

From Dev

IO Completion Ports and socket send()

From Dev

IO Completion Ports and socket WSARecv()

From Dev

IO Completion Ports and OVERLAPPED management

From Dev

IO Completion Ports and socket send()

From Dev

IO Completion Ports and socket WSARecv()

From Dev

Asynchronous action methods and IO completion ports

From Dev

socket.io need to reregister handlers on reconnect

From Dev

io completion ports issue with calling multiple wsarecv or wsasend per GetQueuedCompletionStatus

From Dev

Java/Tomcat: how to handle WebSocket setup completion

From Dev

How to handle AutoSearch completion using Selenium webdriver

From Dev

How to handle the result of a completion block between classes

From Dev

How to handle with a multiple errors in a completion closure

From Dev

Swift - How to handle completion block in for-loop?

From Dev

How does the operating system post io completion messages for async io

From Dev

How does the operating system post io completion messages for async io

From Dev

How to use catch with IO Handle

From Dev

Multiple I/O Completion Ports

From Dev

How to handle ports in URI.Builder class in Android

From Dev

How to handle several ports of my own GitHub project?

From Dev

C# How to perform Asynchrounus I/O using Completion Ports with APM-TAP Patterns on WCF Callback?

From Dev

How to handle null value in realm.io?

From Dev

How to handle this simple IO exception in Haskell

From Dev

How to handle java.io.InvalidClassException?

From Dev

How to handle this simple IO exception in Haskell

From Dev

select() equivalence in I/O Completion Ports

From Dev

AVR IO Ports can not be global

From Dev

IO Completion port Linux equivalent

From Dev

Business logic in IO completion Port

Related Related

  1. 1

    IO Completion Ports (IOCP)

  2. 2

    IO Completion Ports and socket send()

  3. 3

    IO Completion Ports and socket WSARecv()

  4. 4

    IO Completion Ports and OVERLAPPED management

  5. 5

    IO Completion Ports and socket send()

  6. 6

    IO Completion Ports and socket WSARecv()

  7. 7

    Asynchronous action methods and IO completion ports

  8. 8

    socket.io need to reregister handlers on reconnect

  9. 9

    io completion ports issue with calling multiple wsarecv or wsasend per GetQueuedCompletionStatus

  10. 10

    Java/Tomcat: how to handle WebSocket setup completion

  11. 11

    How to handle AutoSearch completion using Selenium webdriver

  12. 12

    How to handle the result of a completion block between classes

  13. 13

    How to handle with a multiple errors in a completion closure

  14. 14

    Swift - How to handle completion block in for-loop?

  15. 15

    How does the operating system post io completion messages for async io

  16. 16

    How does the operating system post io completion messages for async io

  17. 17

    How to use catch with IO Handle

  18. 18

    Multiple I/O Completion Ports

  19. 19

    How to handle ports in URI.Builder class in Android

  20. 20

    How to handle several ports of my own GitHub project?

  21. 21

    C# How to perform Asynchrounus I/O using Completion Ports with APM-TAP Patterns on WCF Callback?

  22. 22

    How to handle null value in realm.io?

  23. 23

    How to handle this simple IO exception in Haskell

  24. 24

    How to handle java.io.InvalidClassException?

  25. 25

    How to handle this simple IO exception in Haskell

  26. 26

    select() equivalence in I/O Completion Ports

  27. 27

    AVR IO Ports can not be global

  28. 28

    IO Completion port Linux equivalent

  29. 29

    Business logic in IO completion Port

HotTag

Archive