Q: How can I programmatically retrieve my device's MAC address?
You can retrieve the on-board adapter's MAC address using the GetAdaptersInfo() API something like this:
PIP_ADAPTER_INFO pAdapterInfo = NULL; ULONG OutBufLen = 0; ULONG ret = 0;
// first get size of AdapterInfo OutBufLen = 0; ret = GetAdaptersInfo(pAdapterInfo, &OutBufLen); if (ret == ERROR_BUFFER_OVERFLOW) { // Allocate memory to get AdatperInfo pAdapterInfo = (PIP_ADAPTER_INFO) malloc(OutBufLen); ret = GetAdaptersInfo(pAdapterInfo, &OutBufLen); if (ret == ERROR_SUCCESS) { do { RETAILMSG(TRUE, (_T(" Adapter Name = %a\r\n"), pAdapterInfo->AdapterName)); RETAILMSG(TRUE, (_T(" IP Address = %a\r\n"), pAdapterInfo->CurrentIpAddress->IpAddress.String)); RETAILMSG(TRUE, (_T(" Gateway = %a\r\n"), pAdapterInfo->GatewayList.IpAddress.String)); RETAILMSG(TRUE, (_T(" DHCP Server = %a\r\n"), pAdapterInfo->DhcpServer.IpAddress.String)); RETAILMSG(TRUE, (_T(" MAC Address = 0x%02x-0x%02x-0x%02x-0x%02x-0x%02x-0x%02x\r\n"), pAdapterInfo->Address[0], pAdapterInfo->Address[1], pAdapterInfo->Address[2], pAdapterInfo->Address[3], pAdapterInfo->Address[4], pAdapterInfo->Address[5])); } while ((pAdapterInfo = pAdapterInfo->Next) != NULL); } free((PVOID)pAdapterInfo); }
|