如何获得MAC地址
情况是这样的,电脑上有两网卡,
例如:一个IP 192.168.2.3
另外一个网卡上是10.10.1.3
我想如何得到那个IP是10.10.1.3的MAC地址呢
[解决办法]
google "site:csdn.net SendARP"
教会你如何通过搜索解决问题,比直接复制一堆代码更合适。
[解决办法]
netbios获取网卡,有个序号决定获取第一个网卡还是第2个网卡的MAC。
http://hi.baidu.com/qq313619456/blog/item/7b119e813a54f8db9123d98e.html
[解决办法]
WSAData wsaData; WSAStartup(MAKEWORD(2,0),&wsaData); //初始化WINSOCK调用 char HostName[100]; //存放本主机名 HOSTENT *lpHostEnt; //或者:LPHOSTENT lpHostEnt; gethostname(HostName,sizeof(HostName)); //利用得到的主机名去获得主机结构 lpHostEnt=gethostbyname(HostName); //利用主机名去取主机结构 for(int i=0;lpHostEnt->h_addr_list[i]!=NULL;i++) { IN_ADDR *p=(IN_ADDR *)(lpHostEnt-> h_addr_list[i]); String IP=inet_ntoa(*p); } WSACleanup();
[解决办法]
#include <winsock2.h>String GetMac(AnsiString strIPAddr){String MACStr;ULONG pulMac[2];ULONG ulLen;HRESULT hr;unsigned int addr;WORD wVersionRequested;WSADATA wsaData;int nErrorNum; wVersionRequested = MAKEWORD(2, 2); nErrorNum = WSAStartup(wVersionRequested, &wsaData); if(nErrorNum != 0) { ShowMessage( "Error! WSAStartup failed!\n "); return " "; } addr = inet_addr((char FAR*)(strIPAddr.c_str())); memset (pulMac, 0xff, sizeof (pulMac)); ulLen = 6; hr = SendARP (addr, 0, pulMac, &ulLen); if(hr == NO_ERROR) { size_t i, j; char *szMac = new char[ulLen*3]; PBYTE pbHexMac = (PBYTE) pulMac; sprintf(szMac,"%02X%02X%02X%02X%02X%02X",pbHexMac[0], pbHexMac[1], pbHexMac[2], pbHexMac[3], pbHexMac[4], pbHexMac[5]); MACStr = String(szMac); delete [] szMac; }WSACleanup(); return MACStr;}void __fastcall TForm1::Button5Click(TObject *Sender){AnsiString strIPAddr = "192.168.0.104";ShowMessage( GetMac( strIPAddr));}//---------------------------------------