Reputation: 14974
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.
Upvotes: 1
Views: 854
Reputation: 41
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.
Upvotes: 2
Reputation: 54148
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:
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
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.
Upvotes: 0
Reputation: 754665
It looks like you're missing this part of the code sample.
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
}
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 the ERROR_BUFFER_OVERFLOW
return it changes it's allocation the the size specified in ulOutBufLen
(updated by the function call).
This is the line which changes the amount of memory allocated in the case of more than one adapter.
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);
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.
Upvotes: 0