GetOverlappedResults()完全不返回。
我运行了一个简单的示例,当网络接口中的IP地址发生更改时,将设置手动重置事件,并且我可以看到“ IP地址表已更改。”输出,但是即使bWait为false,GetOverlappedResults()也不会返回。即使使用bWait = true,它也应返回,因为已设置了事件,因此I / O操作已完成。

到底是怎么回事?

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <windows.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

void main()
{
    OVERLAPPED overlap;
    DWORD ret, nr;
    HANDLE hand = NULL;
    overlap.hEvent = CreateEvent(NULL, true, false, NULL);

    ret = NotifyAddrChange(&hand, &overlap);

    for (;;)
    {
        if ( WaitForSingleObject(overlap.hEvent, INFINITE) == WAIT_OBJECT_0 )
        {
            printf("IP Address table changed..\n");
            ret = GetOverlappedResult(hand, &overlap, &nr, false);
            scanf_s("%d %d\n", ret, nr);
            printf("done\n");
            NotifyAddrChange(&hand, &overlap);
            ResetEvent(overlap.hEvent);
        }
    }
}

最佳答案

等待是由scanf_s()引起的。我想你是要printf返回值不读它。

关于c++ - 具有bWait = false的GetOverlappedResults块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26327037/

10-12 18:05