casting issue with linked list item

别等时光非礼了梦想. 提交于 2020-01-03 04:54:22

问题


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?


回答1:


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




回答2:


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;


来源:https://stackoverflow.com/questions/20937334/casting-issue-with-linked-list-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!