gpt4 book ai didi

c++ - 链表项的类型转换问题

转载 作者:行者123 更新时间:2023-11-28 00:35:59 28 4
gpt4 key购买 nike

这是我的 PER_IO_CONTEXT 结构(我将它们存储在单链表中):

 typedef struct _PER_IO_CONTEXT 
{

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

} PER_IO_CONTEXT, *PPER_IO_CONTEXT;

下面是 WSAsend ,它使用列表来获取 WSAOVERLAPPED 结构:

...
PSLIST_HEADER pListHead;
...

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

GQCS收到通知的最后部分:

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

如您所见,下面的转换 lpIOContext =(PPER_IO_CONTEXT)lpOverlapped 不起作用,因为 WSAsend 提供了 wsaoverlapped - PER_IO_CONTEXT 结构的第二个成员,因此取消引用如 lpIOContext-> 不能在这种情况下使用。

有办法处理这种情况吗?

最佳答案

要获取相应的 PER_IO_CONTEXT 结构的地址,您可以使用:

lpIOContext = CONTAINING_RECORD(lpOverlapped, PER_IO_CONTEXT, Overlapped);

CONTAINING_RECORD 是在 VC\crt\src\collections.h 中这样定义的一个宏:

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

更多信息:http://msdn.microsoft.com/en-us/library/windows/hardware/ff542043%28v=vs.85%29.aspx

关于c++ - 链表项的类型转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20937334/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com