I thought that I would experiment with GetAdaptersInfo() yesterday (this is GeeksWithBlogs!) Being lazy, I googled to find example code to start with, and found GetAdaptersInfo Function on MSDN . This certainly was a good starting point, but I soon found a problem.
As it turns out, the day before I had been working on Platform Builder: Find the Source of a Data Abort; an Example where I purposely added a Data Abort to my Ethernet driver. As a result, I didn't have any Ethernet adapters to get information for. This meant that when I called GetAdaptersInfo(), it failed returning ERROR_NO_DATA (232) which was not handled well in the sample code.
The original code contained:
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
// Code removed
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
// Code removed
} else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
}
Note that neither of the calls to GetAdapterInfo() checked for ERROR_NO_DATA, although the second call is checking for success. But on failure, the second call outputs a message which seems to suggest that something really unexpected occurred.
A more user friendly way to handle this would be:
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) ;
if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
// Code removed
}
else if( dwRetVal == ERROR_NO_DATA )
{
printf( "No adapters found\n" );
return 0;
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
// Code removed
} else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
}
