Search code examples
c++asynchronousoverlapped-io

GetOverlappedResults blocks with bWait = false


GetOverlappedResults() does not return at all.
I ran the simple example bellow and when there is an IP address change in a network interface the manual reset event gets set and I can see "IP Address table changed.." output, but GetOverlappedResults() does not return even though bWait is false. Even with bWait = true it should return because the event is set therefore the I/O operation is complete.

What is going on?

#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);
        }
    }
}

Solution

  • The wait is caused by scanf_s(). I guess you meant to printf the return value not read it.