Here’s a code snippet that enumerates the currently enabled network adapters and shows some info for each adapter :-
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG OutBufLen = 0;
//Get the required size of the buffer
if( GetAdaptersInfo(NULL, &OutBufLen) == ERROR_BUFFER_OVERFLOW )
{
int divisor = sizeof IP_ADAPTER_INFO;
/*** Uncomment for VC++ 2005 Beta 2
if( sizeof time_t == 8 )
divisor -= 8;
***/
pAdapterInfo = new IP_ADAPTER_INFO[OutBufLen/divisor];
//Get info for the adapters
if( GetAdaptersInfo(pAdapterInfo, &OutBufLen) != ERROR_SUCCESS )
{
//Call failed
delete[] pAdapterInfo;
pAdapterInfo = NULL;
}
else
{
int index = 0;
while(pAdapterInfo)
{
cout << pAdapterInfo->Description << endl;
cout << pAdapterInfo->AdapterName << endl;
cout << "MAC Address : ";
for(int i=0; i < (int)pAdapterInfo->AddressLength; i++)
cout << setfill('0') << setw(2) << uppercase << hex
<< (int)pAdapterInfo->Address[i] << ' ';
cout << endl;
cout << "Default Gateway : ";
cout << pAdapterInfo->GatewayList.IpAddress.String << endl;
cout << "IP Address list : ";
PIP_ADDR_STRING pIpStr = &pAdapterInfo->IpAddressList;
while(pIpStr)
{
cout << pIpStr->IpAddress.String << endl;
pIpStr = pIpStr->Next;
}
pAdapterInfo = pAdapterInfo->Next;
cout << endl << endl;
//...
delete[] pAdapterInfo;
}
}
}
I have only used some of the fields in the IP_ADAPTER_INFO structure, so make sure you look it up on MSDN.
BTW I used code similar to the above snippet in my MAC Address Changer for Windows XP/2003 application available as a free tool with source code from this Code Project article.