bool GetMacAddress(CString &LanMAC)
{
#define MAX_ADAPTER_NAME_LENGTH 256
#define MAX_ADAPTER_DESCRIPTION_LENGTH 128
#define MAX_ADAPTER_ADDRESS_LENGTH 8
typedef struct _NET_IP_ADDRESS_STRING{
char String[16];
} NET_IP_ADDRESS_STRING, *PNET_IP_ADDRESS_STRING, NET_IP_MASK_STRING, *PNET_IP_MASK_STRING;
typedef struct _NET_IP_ADDR_STRING {
struct _NET_IP_ADDR_STRING* Next;
NET_IP_ADDRESS_STRING IpAddress;
NET_IP_MASK_STRING IpMask;
DWORD Context;
} NET_IP_ADDR_STRING, *PNET_IP_ADDR_STRING;
typedef struct _NET_IP_ADAPTER_INFO {
struct _NET_IP_ADAPTER_INFO* Next;
DWORD ComboIndex;
char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
UINT AddressLength;
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
DWORD Index;
UINT Type;
UINT DhcpEnabled;
PNET_IP_ADDR_STRING CurrentIpAddress;
NET_IP_ADDR_STRING IpAddressList;
NET_IP_ADDR_STRING GatewayList;
NET_IP_ADDR_STRING DhcpServer;
BOOL HaveWins;
NET_IP_ADDR_STRING PrimaryWinsServer;
NET_IP_ADDR_STRING SecondaryWinsServer;
time_t LeaseObtained;
time_t LeaseExpires;
} NET_IP_ADAPTER_INFO, *PNET_IP_ADAPTER_INFO;
// LanMAC.RemoveAll();
HINSTANCE hLib = 0;
if (hLib = LoadLibrary(_T("iphlpapi.dll"))) //加载动态库
{
DWORD(WINAPI*pGetAdaptersInfo) (PNET_IP_ADAPTER_INFO, PULONG) =
(DWORD(WINAPI*) (PNET_IP_ADAPTER_INFO, PULONG))GetProcAddress(hLib, "GetAdaptersInfo");//获取地址
if (pGetAdaptersInfo != 0)
{
DWORD AdapterInfoSize = 0;
if (pGetAdaptersInfo(0, &AdapterInfoSize) == ERROR_BUFFER_OVERFLOW) //获取长度
{
PNET_IP_ADAPTER_INFO pAdapterInfo = (PNET_IP_ADAPTER_INFO) new BYTE[AdapterInfoSize]; //分配内存
if (pGetAdaptersInfo(pAdapterInfo, &AdapterInfoSize) == ERROR_SUCCESS) //获取信息
{
PNET_IP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter)
{
CString strMac;
strMac.Format(_T("%02X-%02X-%02X-%02X-%02X-%02X"),
pAdapter->Address[0], pAdapter->Address[1], pAdapter->Address[2],
pAdapter->Address[3], pAdapter->Address[4], pAdapter->Address[5]);
LanMAC.Format(strMac);
pAdapter = pAdapter->Next; //下一个区域
}
}
delete[] pAdapterInfo; //释放内存
}
}
FreeLibrary(hLib); //释放库
}
return true;
}