以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  如何用VC++来获取本机的MAC地址?  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=87911)


--  作者:葛靖青001
--  发布时间:11/24/2010 3:14:00 PM

--  如何用VC++来获取本机的MAC地址?
【转自互联网】

有些时候程序需要获得主机的mac地址,进行一些主机信息搜集的工作,以下是一个短小的mac地址获取程序:

   注意:必须在project->Setting里添加库文件“netapi32.lib”

    #include "stdafx.h"

  #include   <stdio.h>

  #include   <stdlib.h>

  #include   <httpext.h>

  #include   <windef.h>

  #include   <Nb30.h>

  int getMAC(char * mac)

  {

  NCB ncb;

  typedef struct _ASTAT_

  {

  ADAPTER_STATUS   adapt;

  NAME_BUFFER   NameBuff   [30];

  }ASTAT,   *   PASTAT;

  ASTAT Adapter;

  typedef struct _LANA_ENUM

  {   //   le

  UCHAR   length;

  UCHAR   lana[MAX_LANA];

  }LANA_ENUM   ;

  LANA_ENUM lana_enum;

  UCHAR uRetCode;

  memset(&ncb, 0, sizeof(ncb));

  memset(&lana_enum, 0, sizeof(lana_enum));

  ncb.ncb_command = NCBENUM;

  ncb.ncb_buffer = (unsigned char *)&lana_enum;

  ncb.ncb_length = sizeof(LANA_ENUM);

  uRetCode = Netbios(&ncb);

  if(uRetCode != NRC_GOODRET)

  return uRetCode;

  for(int lana=0; lana&l
t;lana_enum.length; lana++)
  {

  ncb.ncb_command = NCBRESET;

  ncb.ncb_lana_num = lana_enum.lana[lana];

  uRetCode = Netbios(&ncb);

  if(uRetCode == NRC_GOODRET)

  break;

  }

  if(uRetCode != NRC_GOODRET)

  return uRetCode;

  memset(&ncb, 0, sizeof(ncb));

  ncb.ncb_command = NCBASTAT;

  ncb.ncb_lana_num = lana_enum.lana[0];

  strcpy((char*)ncb.ncb_callname, "*");

  ncb.ncb_buffer = (unsigned char *)&Adapter;

  ncb.ncb_length = sizeof(Adapter);

  uRetCode = Netbios(&ncb);

  if(uRetCode != NRC_GOODRET)

  return uRetCode;

  sprintf(mac,"%02X-%02X-%02X-%02X-%02X-%02X",

  Adapter.adapt.adapter_address[0],

  Adapter.adapt.adapter_address[1],

  Adapter.adapt.adapter_address[2],

  Adapter.adapt.adapter_address[3],

  Adapter.adapt.adapter_address[4],

  Adapter.adapt.adapter_address[5]

  );

  return 0;

  }

  int main(int argc, char* argv[])

  {

  char   mac[200];

  getMAC(mac);

  printf("   mac   :   %s   \n",mac);

  return 0;

  }
注意:必须在project->Setting里添加库文件“netapi32.lib”。
  #include "stdafx.h"

  #include   <stdio.h>

  #include   <stdlib.h>

  #include   <httpext.h>

  #include   <windef.h>

  #include   <Nb30.h>

  int getMAC(char * mac)

  {

  NCB ncb;

  typedef struct _ASTAT_

  {

  ADAPTER_STATUS   adapt;

  NAME_BUFFER   NameBuff   [30];

  }ASTAT,   *   PASTAT;

  ASTAT Adapter;

  typedef struct _LANA_ENUM

  {   //   le

  UCHAR   length;

  UCHAR   lana[MAX_LANA];

  }LANA_ENUM   ;

  LANA_ENUM lana_enum;

  UCHAR uRetCode;

  memset(&ncb, 0, sizeof(ncb));

  memset(&lana_enum, 0, sizeof(lana_enum));

  ncb.ncb_command = NCBENUM;

  ncb.ncb_buffer = (unsigned char *)&lana_enum;

  ncb.ncb_length = sizeof(LANA_ENUM);

  uRetCode = Netbios(&ncb);

  if(uRetCode != NRC_GOODRET)

  return uRetCode;

  for(int lana=0; lana<lana_enum.length; lana++)

  {

  ncb.ncb_command = NCBRESET;
>  ncb.ncb_lana_num = lana_enum.lana[lana];

  uRetCode = Netbios(&ncb);

  if(uRetCode == NRC_GOODRET)

  break;

  }

  if(uRetCode != NRC_GOODRET)

  return uRetCode;

  memset(&ncb, 0, sizeof(ncb));

  ncb.ncb_command = NCBASTAT;

  ncb.ncb_lana_num = lana_enum.lana[0];

  strcpy((char*)ncb.ncb_callname, "*");

  ncb.ncb_buffer = (unsigned char *)&Adapter;

  ncb.ncb_length = sizeof(Adapter);

  uRetCode = Netbios(&ncb);

  if(uRetCode != NRC_GOODRET)

  return uRetCode;

  sprintf(mac,"%02X-%02X-%02X-%02X-%02X-%02X",

  Adapter.adapt.adapter_address[0],

  Adapter.adapt.adapter_address[1],

  Adapter.adapt.adapter_address[2],

  Adapter.adapt.adapter_address[3],

  Adapter.adapt.adapter_address[4],

  Adapter.adapt.adapter_address[5]

  );

  return 0;

  }

  int main(int argc, char* argv[])

  {

  char   mac[200];

  getMAC(mac);

  printf("   mac   :   %s   \n",mac);

  return 0;

  }


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
93.750ms