GetAdaptersInfo and GetAdaptersAddressess BufferLength Param

前端 未结 3 1151
误落风尘
误落风尘 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:49

    Your code needs to look something like this:

    // First get the desired size.
    unsigned long outBufLen = 0;
    DWORD dwResult = GetAdaptersInfo(NULL, &outBufLen);
    if (dwResult == ERROR_BUFFER_OVERFLOW)  // This is what we're expecting
    {
        // Now allocate a structure of the requried size.
        PIP_ADAPTER_INFO pIpAdapterInfo = (PIP_ADAPTER_INFO) malloc(outBufLen);
        dwResult = GetAdaptersInfo(pIpAdapterInfo, &outBufLen);
        if (dwResult == ERROR_SUCCESS)
        {
            // Yay!
    

    Edit: See also Jeremy Friesner's answer for why this code isn't quite enough.

提交回复
热议问题