I was trying to get Network Adapter Information from the Windows platform. So I have used an old API GetAdaptersInfo API. I was not sure whether it will work for Windows 10 but I have written a small sample to run on my Windows 10 PC and to my surprise, it still works. Though Microsoft recommended using GetAdaptersAddresses API over GetAdaptersInfo API. It's a very small piece of code but the order headers are very important. If we do not follow the proper order the program won't compile.
<Code>
#include <iostream>
#include <winsock2.h>
#include <iphlpapi.h>
#include <cassert>
#include <winsock2.h>
#include <iphlpapi.h>
#include <cassert>
#pragma comment(lib, "iphlpapi.lib")
void PrintMACAddress()
{
DWORD _macAddress = 0;
IP_ADAPTER_INFO _adapterInfo[16];
DWORD dwBufLen = sizeof(_adapterInfo);
DWORD dwStatus = GetAdaptersInfo(_adapterInfo, &dwBufLen);
assert(dwStatus == ERROR_SUCCESS);
PIP_ADAPTER_INFO _pAdapterInfo = _adapterInfo;
char string[32];
do
{
sprintf_s(string, ("%02X-%02X-%02X-%02X-%02X-%02X"),
_pAdapterInfo->Address[0], _pAdapterInfo->Address[1],
_pAdapterInfo->Address[2], _pAdapterInfo->Address[3],
_pAdapterInfo->Address[4], _pAdapterInfo->Address[5]);
printf("Adapter Name: %s\n", _pAdapterInfo->AdapterName);
printf("MACAddress: %s\n", string);
printf("Index: %d\n", _pAdapterInfo->Index);
switch (_pAdapterInfo->Type) {
case MIB_IF_TYPE_OTHER:
printf("Type: Other\n");
break;
case MIB_IF_TYPE_ETHERNET:
printf("Type: Ethernet\n");
break;
case MIB_IF_TYPE_TOKENRING:
printf("Type: Token Ring\n");
break;
case MIB_IF_TYPE_FDDI:
printf("Type: FDDI\n");
break;
case MIB_IF_TYPE_PPP:
printf("Type: PPP\n");
break;
case MIB_IF_TYPE_LOOPBACK:
printf("Type: Lookback\n");
break;
case MIB_IF_TYPE_SLIP:
printf("Type: Slip\n");
break;
default:
printf("Unknown type %ld\n", _pAdapterInfo->Type);
break;
}
printf("IP Address: %s\n\n", _pAdapterInfo->IpAddressList.IpAddress.String);
_pAdapterInfo = _pAdapterInfo->Next;
} while (_pAdapterInfo);
}
</Code>
Comments