Question

This is my PER_IO_CONTEXT structure (i stored them in singly linked list):

 typedef struct _PER_IO_CONTEXT 
 {

    SLIST_ENTRY       ItemEntry;
    WSAOVERLAPPED     Overlapped;
    WSABUF                 wsabuf; 
    /* some other data*/

 } PER_IO_CONTEXT, *PPER_IO_CONTEXT;

and below is WSAsend , that use the list for getting WSAOVERLAPPED structure:

...
PSLIST_HEADER pListHead;
...

PSLIST_ENTRY  pListEntry = InterlockedPopEntrySList(pListHead);
PPER_IO_CONTEXT ovl = (PPER_IO_CONTEXT)pListEntry;
WSASend(pTmp1->Socket,..., &(ovl->Overlapped), NULL);

and the last part when GQCS gets notification:

 LPWSAOVERLAPPED lpOverlapped = NULL;
 PPER_IO_CONTEXT lpIOContext = NULL; 
 ....
 GetQueuedCompletionStatus(..... (LPOVERLAPPED *)&lpOverlapped,  INFINITE);
 lpIOContext = (PPER_IO_CONTEXT)lpOverlapped;
 lpIOContext->wsabuf       // this fail

As you can see following cast lpIOContext =(PPER_IO_CONTEXT)lpOverlapped doesn't work because WSAsend was provided with wsaoverlapped - the second member of PER_IO_CONTEXT structure, so dereferences such as lpIOContext-> can't be used in this case.

There is a way to deal with this situation?

Was it helpful?

Solution

To get the address of the corresponding PER_IO_CONTEXT struct you can use this:

lpIOContext = CONTAINING_RECORD(lpOverlapped, PER_IO_CONTEXT, Overlapped);

CONTAINING_RECORD is a macro defined in VC\crt\src\collections.h in such a way:

#define CONTAINING_RECORD(address, type, field) \
    ((type *)((char *)(address) - (ULONG_PTR)(&((type *)0)->field)))

More information: http://msdn.microsoft.com/en-us/library/windows/hardware/ff542043%28v=vs.85%29.aspx

OTHER TIPS

I'm not sure whether there's a supported mechanism to convert a pointer to a member of a struct to a pointer to the struct. You could cast everything to BYTE * and do the arithmetic, which would work in practice, but depending on your needs it might be cleaner to reorganize to avoid the necessity:

typedef struct _PER_IO_CONTEXT 
{
   WSAOVERLAPPED     Overlapped;
   WSABUF                 wsabuf; 
   /* some other data*/
} PER_IO_CONTEXT, *PPER_IO_CONTEXT;

typedef struct _PER_IO_CONTEXT_LIST_ITEM
{
   SLIST_ENTRY       ItemEntry;
   PER_IO_CONTEXT    Item;
} PER_IO_CONTEXT_LIST_ITEM, *PPER_IO_CONTEXT_LIST_ITEM;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top