用C++实现,获取所在局域网的所有mac地址
导师给布置一个小程序,用C++实现,求大牛人帮忙。
[解决办法]
include "stdio.h"
#include "winsock2.h"
#include "iphlpapi.h"
// #pragma comment(lib,"C:\Program Files\Borland\CBuilder6\Lib\Psdk\iphlpapi.lib")
#pragma comment(lib,"iphlpapi.lib")
int Stat[255];
String IPs[255];
String MACs[255];
#define maxThread 60
Mthread *thread[maxThread]; // 线程地址
String GetIP() // 取本地IP
{
WSAData wsaData;
WSAStartup(MAKEWORD(2,0),&wsaData);
char HostName[64];
gethostname(HostName,sizeof(HostName));
HOSTENT *pHost=gethostbyname(HostName);
TStringList *ips=new TStringList;
in_addr **ppAddr=(in_addr **)pHost->h_addr_list;
in_addr *pAddr;
while(pAddr=*(ppAddr++),pAddr!=NULL)
{
String ip=inet_ntoa(*pAddr);
ips->Add(ip);
}
WSACleanup();
String ip=ips->Text;
delete ips;
return(ip);
}
String GetMAC(String IP,int & rs) // 取IP地址对应的MAC地址
{
unsigned char mac[6];
ULONG MacLen=6;
ULONG DestIP=inet_addr(IP.c_str());
rs=SendARP(DestIP,(ULONG)NULL,(PULONG)mac,(PULONG)&MacLen);
String MACs="";
if (rs==0)
{
char buf[32];
sprintf(buf,"%02X-%02X-%02X-%02X-%02X-%02X",
(unsigned int)mac[0],
(unsigned int)mac[1],
(unsigned int)mac[2],
(unsigned int)mac[3],
(unsigned int)mac[4],
(unsigned int)mac[5]);
MACs=buf;
}
return(MACs);
}