使用 GetAdaptersInfo() 时,仅对所有适配器进行一次 Malloc?
我查看了 MSDN 中的 GetAdaptersInfo() 示例:
http: //msdn.microsoft.com/en-us/library/aa365917%28VS.85%29.aspx
并注意到,虽然示例尝试迭代所有适配器,但它只为第一个适配器分配内存。
这是一个错误吗?
如果没有,为什么不呢?所有适配器都具有相同的信息大小吗?
为了进一步澄清我的问题:我理解示例中对 malloc 的双重调用的作用。我不明白的是为什么它在循环之外。如果系统中只有一个适配器,当然没有问题。但是当有多个适配器时会发生什么?这是一个错误吗?
谢谢。
I looked at the GetAdaptersInfo() sample in MSDN:
http://msdn.microsoft.com/en-us/library/aa365917%28VS.85%29.aspx
and noticed that while the sample attempts to iterate through ALL adapters, it only allocates memory for the first one.
Is this a bug?
If not, why not? Do all adapters have the same info size?
To further clarify my question: I understood the role of the double call to malloc in the sample. What I don't understand is why is it outside the loop. If there is only one adapter in the system, of course there is no problem. But what happens when there are multiple adapters? Is this a bug?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此示例代码需要注意的一件事是,如果适配器的数量在对 GetAdaptersInfo 的调用之间发生增加,您将超出缓冲区。这是极不可能的,但代码仍应检查该情况。
One thing to note with this sample code is that if the number of adaptors happens to increase between calls to GetAdaptersInfo, you'll over run your buffer. It's extremely unlikely, but the code should still check for that condition.
这不是一个错误 - 第一次调用是为了找出实际需要多少数据区域。
如果第一次调用失败并出现 ERROR_BUFFER_OVERFLOW,它还会告诉您需要多少数据空间来保存所有结果。然后,该示例继续重新分配,并再次调用 Win32 以获取完整列表。这就是您注意到的迭代之前的这段代码:
在我看来,在最好的情况下,不需要第二次调用 - 从大多数机器中只有一个网络适配器的时代来看,这是一种合理的方法。该示例似乎没有在第一次调用时检查 NO_ERROR,这会出于某种原因避免第二次调用。
It's not a bug - the first call is to find out how much data area is actually needed.
If this first call fails with ERROR_BUFFER_OVERFLOW, it also tells you how much dataspace you need to hold all the results. The example then goes on to reallocate, and call Win32 again to get the full list. That is this code here, before the iteration you noted:
Seems to me that in the best case, this second call is not required - a reasonable approach from a time when there was only one network adapter in most machines. The sample does not seem to check for NO_ERROR on the first call, which would obviate the second, for some reason.
看来您缺少代码示例的这一部分。
此代码使用单个分配的适配器调用
GetAdaptersInfo
。它本质上是针对机器上只有一个适配器的情况进行优化的。如果它收到ERROR_BUFFER_OVERFLOW
返回,它会将其分配更改为ulOutBufLen
中指定的大小(由函数调用更新)。这一行会更改在多个适配器的情况下分配的内存量。
编辑
阅读史蒂夫的评论后,我仔细观察了一下,发现代码错误地调用了
GetAdaptersInfo
函数两次。在有 1 个适配器的情况下,第一次调用可能会成功,从而无需进行第二次调用。It looks like you're missing this part of the code sample.
This code is calling
GetAdaptersInfo
with a single allocated adapter. It's essentially optimizing for the case where there is only one adapter on the machine. In the case it receieves theERROR_BUFFER_OVERFLOW
return it changes it's allocation the the size specified inulOutBufLen
(updated by the function call).This is the line which changes the amount of memory allocated in the case of more than one adapter.
EDIT
After reading Steve's comment I looked a bit closer and it appears that the code is incorrectly calling the
GetAdaptersInfo
function twice. In the case of 1 adapter the first call can potentially result in success removing the need for the second call.