获取网卡地址

如何获取网卡地址(MAC地址):

VC++编写的代码,适用于windows环境,API实现

/*char *pMACAdr,返回网卡地址的buff,

 int *nBuffLen前一个参数的长度,

 int nAdapterID = 0网卡号,针对多网卡问题,有的机器不止一个网卡,默认为0

*/

 

#include <NB30.H>
#pragma comment(lib, "C:\\Program Files\\Microsoft Visual Studio\\VC98\\Lib\\Netapi32.lib")//自己找安装目录

BOOL GetMACAdress(char *pMACAdr, int *nBuffLen, int nAdapterID = 0)
{
 if (NULL == pMACAdr || *nBuffLen <= 0 || nAdapterID < 0) {
  *nBuffLen = 0;
  return FALSE;
 }
 memset(pMACAdr, 0, *nBuffLen);

 typedef struct _ASTAT_  
 {  
  ADAPTER_STATUS   adapt;  
  NAME_BUFFER      NameBuff   [30];  
 }ASTAT, *PASTAT; 

 NCB  ncb;  
 UCHAR   uRetCode;  
 ASTAT   Adapter;
 
 memset(&ncb, 0, sizeof(ncb));  
 ncb.ncb_command  = NCBRESET;  
 //网卡序号,一般从0开始
 ncb.ncb_lana_num = nAdapterID; 
 
 //send one NCBRESET command to the adapter car, for initialzing
 uRetCode = Netbios(&ncb);     
 memset(&ncb, 0, sizeof(ncb));  
 ncb.ncb_command  = NCBASTAT;  
 ncb.ncb_lana_num = nAdapterID;//specify the adapter car ID 
   
 strcpy((char*)ncb.ncb_callname, "* ");  
 ncb.ncb_buffer = (PUCHAR)&Adapter;      
 ncb.ncb_length = sizeof(Adapter);  
   
 //send the NCBRESET command to get the adapter car msg 
 uRetCode = Netbios(&ncb);
 CString szMacAdr; 
 if(0 == uRetCode) {   //获得网卡信息成功
  szMacAdr.Format("%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]  
   );
 }
 if (szMacAdr.IsEmpty())  {
  *nBuffLen = 0;
  return FALSE;
 }
 else{
  int nMacLen = szMacAdr.GetLength();
  *nBuffLen = nMacLen <= *nBuffLen ? nMacLen : *nBuffLen;
  memcpy(pMACAdr, szMacAdr.GetBuffer(*nBuffLen), *nBuffLen);
  return TRUE;
 }
}

 

如果想现存的DLL或者ActiveX控制, 在我的资源里有封装好的DLL与ActiveX控制下载, 直接调用接口使用即可

 

 

你可能感兴趣的:(获取网卡地址)