免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: zoelakes
打印 上一主题 下一主题

[函数] 获得本机IP、MAC地址、 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2011-08-04 09:38 |只看该作者
回复 10# yulihua49

  1. void GetIpMac(char *_ip, char *_mac)
  2. {
  3.         system("ipconfig/all >config.txt");                /// ipconfig/all

  4.         ///||||||||||||||||||
  5.         /// C++ FILE
  6.         ///||||||||||||||||||
  7.         ifstream        file;
  8.         file.open("config.txt");
  9.         string                contents;
  10.         int                        count        = 0;
  11.         while ( !file.eof() )
  12.         {
  13.                 count++;
  14.                 getline(file,contents);
  15.                 //cout/*<<"文件内容->"*/<<contents.c_str()<<endl;                /// test print
  16.                 int flag_mac = 1;
  17.                 if (flag_mac)
  18.                 {
  19.                         char *pMacE = (char *)strstr(contents.c_str(),"Physical Address");
  20.                         char *pMacC = (char *)strstr(contents.c_str(),"物理地址");
  21.                         if (NULL != pMacC)
  22.                         {
  23.                                 cout<<"Chinese->"<<pMacC<<endl;
  24.                                 char *pMac        = strstr(pMacC,":");
  25.                                 pMac += sizeof(":");
  26.                                 int mac_len = strlen(pMac);
  27.                                 memcpy(_mac,pMac,mac_len);
  28.                         }
  29.                         if (NULL != pMacE)
  30.                         {
  31.                                 cout<<"English->"<<pMacE<<endl;
  32.                                 char *pMac        = strstr(pMacE,":");
  33.                                 pMac += sizeof(":");
  34.                                 int mac_len = strlen(pMacE);
  35.                                 memcpy(_mac,pMac,mac_len);

  36.                         }
  37.                         flag_mac = 0;
  38.                 }
  39.                 int flag_ip = 1;
  40.                 if (flag_ip)
  41.                 {
  42.                         char *pIpE = (char *)strstr(contents.c_str(),"IP Address");
  43.                         char *pIpC = (char *)strstr(contents.c_str(),"IPv4 地址");
  44.                         if (NULL != pIpC)
  45.                         {
  46.                                 char *pIp        = strstr(pIpC,":");
  47.                                 pIp += sizeof(":");
  48.                                 int ip_len = strlen(pIp);
  49.                                 if (ip_len>13)        /// 192.168.1.XXX 纯属扯淡
  50.                                         ip_len = 13;
  51.                                 memcpy(_ip,pIp,ip_len);
  52.                                 //cout<<"_ip "<<pIp<<"ip_len "<<ip_len<<endl;                /// cout
  53.                                 break;
  54.                         }
  55.                         if (NULL != pIpE)
  56.                         {
  57.                                 //cout<<"English->"<<pLocE<<endl;
  58.                                 char *pIp        = strstr(pIpE,":");
  59.                                 pIp += sizeof(":");
  60.                                 int ip_len = strlen(pIp);
  61.                                 if (ip_len>13)        /// 192.168.1.XXX
  62.                                         ip_len = 13;
  63.                                 memcpy(_ip,pIp,ip_len);
  64.                                 break;
  65.                         }
  66.                         flag_ip = 0;
  67.                 }

  68.         }
  69.         cout<<"读文件IP/MAC次数->"<<count<<endl;
  70.         file.close();
  71.         char *str = "config.txt";
  72.         if (0 != remove(str))
  73.                 cout<<"Error."<<endl;
  74. }
复制代码
系统的ipconfig/all命令的输出形式是不一样的、我写到多一半儿、才发现这方法也不对、
那就看看我丑陋的代码吧、
还是遍历网卡的方法可取、

论坛徽章:
0
12 [报告]
发表于 2011-08-04 10:11 |只看该作者
当然首选遍历网卡了

论坛徽章:
0
13 [报告]
发表于 2011-08-05 08:45 |只看该作者
回复 12# chinesedragon


    http://bbs.chinaunix.net/viewthr ... 6amp%3Btypeid%3D135

那就不妨移驾看看喽、

论坛徽章:
0
14 [报告]
发表于 2011-08-09 12:18 |只看该作者
getmac...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP