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.
Hi Nish!
Why not replace:
/*** Uncomment for VC++ 2005 Beta 2
if( sizeof time_t == 8 )
divisor -= 8;
***/
with
#if _MSC_VER >= 1400
if( sizeof time_t == 8 )
divisor -= 8;
#endif
?
Hey Jochen,
Excellent suggestion. I just didn’t think of it at that time. Thank you.
BTW I am wondering what happens when MS updates Iphlpapi.dll – then any code compiled with pre-VC8 compilers would break!
i am hoping to get some luck here, i like your mac address changer application very much. Only 1 thing i find annoying. The space between the numbers, eg “00 11 22 33 44 55” Could you plz replace the code for the standard mac address output as eg “00-11-22-33-44-55. Plz tell me what to do thx in advance.
Change the ‘ ‘ to a ‘-‘ and you should get what you are looking for.
The blog engine messed up my comment, what I meant is replace the space char with a dash char in the code.