Skip to content

Instantly share code, notes, and snippets.

@MareArts
Last active November 20, 2020 00:45
Show Gist options
  • Save MareArts/7c2c59a65044ab7beeaee0264f53128e to your computer and use it in GitHub Desktop.
Save MareArts/7c2c59a65044ab7beeaee0264f53128e to your computer and use it in GitHub Desktop.
Get MAC Address in MFC
http://study.marearts.com/2017/01/get-mac-address-in-mfc.html
CString GetMACAddress()
{
CString strGateWay = _T("");
CString strMACAddress = _T("");
IP_ADAPTER_INFO ipAdapterInfo[5];
DWORD dwBuflen = sizeof(ipAdapterInfo);
DWORD dwStatus = GetAdaptersInfo(ipAdapterInfo, &dwBuflen);
if (dwStatus != ERROR_SUCCESS)
{
strMACAddress.Format(_T("Error for GetAdaptersInfo : %d"), dwStatus);
AfxMessageBox(strMACAddress);
return _T("");
}
PIP_ADAPTER_INFO pIpAdapterInfo = ipAdapterInfo;
do{
strGateWay = (CString)pIpAdapterInfo->GatewayList.IpAddress.String;
if (strGateWay[0] == '0')
{
pIpAdapterInfo = pIpAdapterInfo->Next;
}
else
{
strMACAddress.Format(_T("%02X-%02X-%02X-%02X-%02X-%02X"),
pIpAdapterInfo->Address[0],
pIpAdapterInfo->Address[1],
pIpAdapterInfo->Address[2],
pIpAdapterInfo->Address[3],
pIpAdapterInfo->Address[4],
pIpAdapterInfo->Address[5]
);
break;
}
} while (pIpAdapterInfo);
return strMACAddress;
}
@MareArts
Copy link
Author

MareArts commented Jan 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment