0

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);
        }
    }
}
Chris
  • 1,213
  • 1
  • 21
  • 38
  • 1
    You must verify that ret == ERROR_IO_PENDING. That scanf_s() call is horribly wrong. Try disabling anti-malware etc. – Hans Passant Oct 12 '14 at 16:33
  • 1
    Do you have the same problem with the official sample? http://msdn.microsoft.com/en-us/library/windows/desktop/aa366329(v=vs.85).aspx it does what Hans says. – usr Oct 12 '14 at 16:35
  • You need to zero out the memory of your `OVERLAPPED` instance before passing it around to any functions such as `NotifyAddrChange()`. You're passing it uninitialized garbage memory. – Adam Rosenfield Oct 12 '14 at 18:41

1 Answers1

1

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

Fouad
  • 48
  • 4