windows GetAdaptersInfo 和 GetAdaptersAddressess BufferLength 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1070351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:44:44  来源:igfitidea点击:

GetAdaptersInfo and GetAdaptersAddressess BufferLength Param

c++windowsvisual-c++windows-xp

提问by RCC

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.

我在 C++ 中有一些遗留代码,它们做了一些我不明白的事情。我在运行 Windows XP 的机器上以 Visual C++ 2008 Express Edition 运行它。

The code uses some Windows functions: GetAdaptersInfoand GetAdaptersAddressess. I realize that the final parameter for both of these is a pointer to the size of the buffer and since it's in_out, it can be changed within the function.

该代码使用了一些 Windows 函数:GetAdaptersInfo和 GetAdaptersAddressess。我意识到这两个的最后一个参数是一个指向缓冲区大小的指针,因为它是 in_out,它可以在函数内更改。

My question is: are these functions supposed to change the buffer length?

我的问题是:这些函数是否应该改变缓冲区长度?

In the code I have, every time these functions are called the buffer length variable is initialized to zero, and after the function is called, it's still 0.

在我的代码中,每次调用这些函数时,缓冲区长度变量都被初始化为零,而在调用该函数后,它仍然为0。

回答by Jeremy Friesner

Of course, the example code in @RichieHindle's answercontains 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.

当然,@RichieHindle 答案中的示例代码包含竞争条件...到 GetAdaptersInfo() 也会因 ERROR_BUFFER_OVERFLOW 而失败,并且您的函数将无法工作。

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.

是的,这在现实生活中确实发生过——我也遇到过。如果您希望代码可靠,则需要在循环中调用 GetAdaptersInfo(),根据需要多次增加缓冲区的大小,直到调用成功。

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

必须有一种不太容易出错的方法来构建 API……不幸的是,微软还没有找到它。:^P

回答by RichieHindle

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.

编辑:另请参阅 Jeremy Friesner 的回答,了解为什么此代码不够用。

回答by Olivier Bertrand

Indeed, Using Visual studio 6, I used to get the number of adapters by:

事实上,使用 Visual Studio 6,我曾经通过以下方式获得适配器的数量:

DWORD drc = GetAdaptersInfo(NULL, &(Buflen = 0L));

if (drc == ERROR_BUFFER_OVERFLOW)
  n = Buflen / sizeof(IP_ADAPTER_INFO);

It was all right, for instance for 2 adapters Buflen was set to 1280 and sizeof(IP_ADAPTER_INFO)was 640.

没关系,例如对于 2 个适配器,Buflen 设置为 1280 和sizeof(IP_ADAPTER_INFO)640。

Now I am using Visual C++ 2008 Express and my result is truncated because the function still sets Buflen to 1280 but the value of sizeof(IP_ADAPTER_INFO)is now 648!

现在我正在使用 Visual C++ 2008 Express 并且我的结果被截断,因为该函数仍然将 Buflen 设置为 1280 但sizeof(IP_ADAPTER_INFO)现在的值是 648!

Is this a bug or am I missing something?

这是一个错误还是我错过了什么?