SimpleButPerfect
SimpleButPerfect

Reputation: 1639

Asynchronous Completion Routines I/O, Pointer to routine encapsulated in class

I was wondering if there was anyway to use functions like ReadFileEx that require a pointer to a function in a class WITHOUT marking the function as static? Thanks in advance. SBP.

Upvotes: 2

Views: 329

Answers (2)

Kosta
Kosta

Reputation: 141

You can always extend the OVERLAPPED struct that you pass to include a pointer to your object. Then, pass a function that calls a member function on that object. Somewhat like this:

typedef struct _MYOVERLAPPED
{
    OVERLAPPED ol;
    MyObject *obj;
} MYOVERLAPPED, *LPMYOVERLAPPED;

void ReadCompleted(DWORD err, DWORD read, LPMYOVERLAPPED overlap)
{
  overlap->obj->foo();
}

Sorry if there's some slight syntax errors in above code, it's been a while since I actually wrote some C++...

Upvotes: 2

John Knoeller
John Knoeller

Reputation: 34148

No, non-static class functions have an implied first argument (this) which is incompatible with their use as a callback for ReadFileEx etc.

Upvotes: 1

Related Questions