#include <IPHlpApi.h>
#pragma comment(lib, "Iphlpapi.lib")
std::wstring Encode16(const BYTE *buf, int len)
{
const WCHAR strHex[] = L"0123456789ABCDEF";
std::wstring wstr;
wstr.resize(len*2);
for(int i=0; i<len; ++i)
{
wstr[i*2+0] = strHex[buf[i] >> 4];
wstr[i*2+1] = strHex[buf[i] & 0xf];
}
return wstr;
}
std::wstring getWindowMacAddress()
{
ULONG ulBufferSize = 0;
DWORD dwResult = ::GetAdaptersInfo(NULL, &ulBufferSize);
if (ERROR_BUFFER_OVERFLOW != dwResult) {
return QString();
}
PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO) new BYTE[ulBufferSize];
if(!pAdapterInfo)
{
return QString();
}
dwResult = ::GetAdaptersInfo(pAdapterInfo, &ulBufferSize);
if (ERROR_SUCCESS != dwResult)
{
delete [] pAdapterInfo;
return QString();
}
BYTE pMac[MAX_ADAPTER_ADDRESS_LENGTH] = {0};
int nLen = MAX_ADAPTER_ADDRESS_LENGTH;
if (NULL != pAdapterInfo)
{
PIP_ADDR_STRING pAddTemp = &(pAdapterInfo->IpAddressList);
if (NULL != pAddTemp)
{
for(int i=0; i < (int)pAdapterInfo->AddressLength; ++i)
{
pMac[i] = pAdapterInfo->Address[i];
}
nLen = pAdapterInfo->AddressLength;
}
}
delete [] pAdapterInfo;
std::wstring strMac = Encode16(pMac, nLen);
return strMac;
}