用vs2012编写c程序,如何获得CPU ID、MAC地址和硬盘序列号
答案:1 悬赏:10 手机版
解决时间 2021-11-20 16:59
- 提问者网友:动次大次蹦擦擦
- 2021-11-20 06:27
用vs2012编写c程序,如何获得CPU ID、MAC地址和硬盘序列号
最佳答案
- 五星知识达人网友:罪歌
- 2021-11-20 06:40
读mac地址:
int get_mac_addr(int* pintrface,char *str_mac_addr)
{
int fd, retn = 0;
int intrface;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;
char mac_addr_max[MAC_ADDR_LEN];
char mac_addr_tmp[MAC_ADDR_LEN];
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
memset(mac_addr_max,0,sizeof(mac_addr_max));
memset(mac_addr_tmp,0,sizeof(mac_addr_tmp));
ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc))
{
intrface = ifc.ifc_len / sizeof (struct ifreq);
*pintrface = intrface;
dprintf(("interface num is intrface=%d\n\n",intrface));
while (intrface-- > 0)
{
dprintf (("net device %s\n", buf[intrface].ifr_name));
arp.arp_pa.sa_family = AF_INET;
arp.arp_ha.sa_family = AF_INET;
((struct sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr =
((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr.s_addr;
if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
{
memset(mac_addr_tmp,0,sizeof(mac_addr_tmp));
sprintf(mac_addr_tmp,"%02x%02x%02x%02x%02x%02x",
(unsigned char)arp.arp_ha.sa_data[0],
(unsigned char)arp.arp_ha.sa_data[1],
(unsigned char)arp.arp_ha.sa_data[2],
(unsigned char)arp.arp_ha.sa_data[3],
(unsigned char)arp.arp_ha.sa_data[4],
(unsigned char)arp.arp_ha.sa_data[5]);
if( strcmp(mac_addr_max,mac_addr_tmp)<0 )
{
memset(mac_addr_max,0,sizeof(mac_addr_max));
strcpy(mac_addr_max,mac_addr_tmp);
}
}//End of if(!(ioctl(fd,SIOCGARP,(char*) &arp)))
else
{
dprintf (("cpm: ioctl device %s\n", buf[intrface].ifr_name));
}
}//End of while (intrface-- > 0)
}//End of if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
else
{
//perror ("cpm: ioctl");
}
}//End of if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
else
{
//perror ("cpm: socket");
}
close (fd);
strcpy(str_mac_addr,mac_addr_max);
return retn;
}追问谢谢你啊,我需要完整的代码,比如需要什么头文件,这个好多数据类型都没有定义
int get_mac_addr(int* pintrface,char *str_mac_addr)
{
int fd, retn = 0;
int intrface;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;
char mac_addr_max[MAC_ADDR_LEN];
char mac_addr_tmp[MAC_ADDR_LEN];
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
memset(mac_addr_max,0,sizeof(mac_addr_max));
memset(mac_addr_tmp,0,sizeof(mac_addr_tmp));
ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc))
{
intrface = ifc.ifc_len / sizeof (struct ifreq);
*pintrface = intrface;
dprintf(("interface num is intrface=%d\n\n",intrface));
while (intrface-- > 0)
{
dprintf (("net device %s\n", buf[intrface].ifr_name));
arp.arp_pa.sa_family = AF_INET;
arp.arp_ha.sa_family = AF_INET;
((struct sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr =
((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr.s_addr;
if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
{
memset(mac_addr_tmp,0,sizeof(mac_addr_tmp));
sprintf(mac_addr_tmp,"%02x%02x%02x%02x%02x%02x",
(unsigned char)arp.arp_ha.sa_data[0],
(unsigned char)arp.arp_ha.sa_data[1],
(unsigned char)arp.arp_ha.sa_data[2],
(unsigned char)arp.arp_ha.sa_data[3],
(unsigned char)arp.arp_ha.sa_data[4],
(unsigned char)arp.arp_ha.sa_data[5]);
if( strcmp(mac_addr_max,mac_addr_tmp)<0 )
{
memset(mac_addr_max,0,sizeof(mac_addr_max));
strcpy(mac_addr_max,mac_addr_tmp);
}
}//End of if(!(ioctl(fd,SIOCGARP,(char*) &arp)))
else
{
dprintf (("cpm: ioctl device %s\n", buf[intrface].ifr_name));
}
}//End of while (intrface-- > 0)
}//End of if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
else
{
//perror ("cpm: ioctl");
}
}//End of if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
else
{
//perror ("cpm: socket");
}
close (fd);
strcpy(str_mac_addr,mac_addr_max);
return retn;
}追问谢谢你啊,我需要完整的代码,比如需要什么头文件,这个好多数据类型都没有定义
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯