GetAdaptersInfo and GetAdaptersAddressess BufferLength Param

前端 未结 3 1148
误落风尘
误落风尘 2021-01-07 07:18

I\'ve got some legacy code in C++ here that does some things I don\'t understand. I\'m running it in Visual C++ 2008 Express Edition on a machine running Windows XP.

<
3条回答
  •  难免孤独
    2021-01-07 07:54

    Of course, the example code in @RichieHindle's answer contains a race condition.... if the size of the structure Windows wants to return grows after the first call to GetAdaptersInfo() but before the second call to GetAdaptersInfo(), the second call to GetAdaptersInfo() will fail with ERROR_BUFFER_OVERFLOW as well, and your function won't work.

    Yes, that does happen in real life -- I've had it happen to me. If you want the code to be reliable, you need to call GetAdaptersInfo() in a loop, increasing your buffer's size as many times as necessary until the call succeeds.

    There has to be a less error-prone way to construct an API... unfortunately Microsoft hasn't yet found it. :^P

提交回复
热议问题