先获取IP地址,再通过IP地址获取MAC地址,Mark一下.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//获得IP地址 WSADATA wsaData; char name[155]; char * ip; PHOSTENT hostinfo; if(WSAStartup(MAKEWORD(2,0),&wsaData)==0) { if((gethostname(name,sizeof(name))==0)) { if((hostinfo=gethostbyname(name))!=NULL) { ip=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list); } } WSACleanup(); } //获得MAC地址 HRESULT hr; IPAddr ipAddr; ULONG pulMac[2]; ULONG ulLen; ipAddr = inet_addr(ip);//设备自身的ip "192.168.201.183" memset (pulMac, 0xff, sizeof (pulMac)); ulLen = 6; hr = SendARP (ipAddr, 0, pulMac, &ulLen); PBYTE pbHexMac = (PBYTE) pulMac; CString str=_T(""),str1; int i; for(i=0;i<ulLen;i++) { str1.Format(_T("%02X "),pbHexMac[i]); str+=str1; } |
转载请注明:exchen's blog » 获取IP地址和MAC地址