#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
#include "Iphlpapi.h"
#pragma comment(lib, "Iphlpapi.lib")
#define UNICODE
#define snprintf _snprintf
bool GetLoaclMac()
{
DWORD err;
DWORD adapterinfosize=0;
PIP_ADAPTER_INFO padapterinfo;
PIP_ADAPTER_INFO pAdapter = NULL;
PIP_ADAPTER_INFO pNextAdapter = NULL;
if((err=GetAdaptersInfo(NULL,&adapterinfosize))!=0)
{
if(err!=ERROR_BUFFER_OVERFLOW)
{
printf("GetAdapterInfo 大小错误: %d\n",GetLastError());
return false;
}
}
if((padapterinfo=(PIP_ADAPTER_INFO)GlobalAlloc(GPTR,adapterinfosize))==NULL)
{
printf("内存分配错误: %d\n",GetLastError());
return false;
}
if((err=GetAdaptersInfo(padapterinfo,&adapterinfosize))!=0)
{
printf("GetAdaptersInfo 错误: %d\n",GetLastError());
GlobalFree(padapterinfo);
return false;
}
pNextAdapter = padapterinfo;
pAdapter = pNextAdapter;
bool FindRealAdapter = false;
char *macaddr[10];
char temp[100];
for (int i = 1;pNextAdapter;i++)
{
pAdapter = pNextAdapter;
pNextAdapter = pNextAdapter->Next;
printf("找到网卡: %s\n", pAdapter->Description);
BYTE* hostmac = pAdapter->Address;
// printf("网卡地址: %.2X-%.2X-%.2X-%.2X-%.2X-%.2X\n",
//hostmac[0],hostmac[1],hostmac[2],hostmac[3],hostmac[4],hostmac[5]);
snprintf(temp,sizeof(temp),"%.2X-%.2X-%.2X-%.2X-%.2X-%.2X",hostmac[0],hostmac[1],
hostmac[2],hostmac[3],hostmac[4],hostmac[5]);
// memcpy(macaddr[i],temp,sizeof(macaddr[i]));
//memset(temp,'\0',sizeof(temp));
printf("网卡地址:%s\n",temp);
}
GlobalFree(padapterinfo);
return true;
}
void main(){
GetLoaclMac();
}
附件为所程序所需要的库文件,拷到对应的include和lib目录即可。
|
文件: | iphlpapi.h%E5%92%8Clib%E4%BB%A5%E5%8F%8A%E5%BF%85%E9%A1%BB%E6%96%87%E4%BB%B6.rar |
大小: | 17KB |
下载: | 下载 |
|