c++windowsasynchronousoverlapped-ioio-completion-ports

Using overlapped structure for callback?


I have my own overlapped structure for asynchronous IO using IO Completion ports.

Now i get notification for read / write completions. Can i pass a CALLBACK function as a parameter in the overlapped structure?

This will allow me to specify various callback functions based on the type of overlapped structure i passed

Has anybody had any luck with this?


Solution

  • Create you own structure derived from OVERLAPPED:

    struct MyOverlapped : OVERLAPPED
    {
      CALLBACK MyCallback;
    };
    

    Now use this instead:

    MyOverlapped *o=new MyOverlapped;
    o->MyCallback=CallbackHandler;
    
    WriteFile(..,..,MyOverlapped);
    

    Then when you get the OVERLAPPED back you can cast it to your derived version:

    MyOverlapped *o=static_cast<MyOverlapped*>(overlapped);
    

    And now you can access the callback. I'm guessing you're getting the OVERLAPPED instance back from a call to GetQueuedCompletionStatus where the pointer you get back will actually point to your derived structure.