• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

VC获取本机网卡地址

OC/C/C++ 水墨上仙 3163次浏览

VC获取本机网卡地址

如何获取网卡地址(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)       
 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控制,&nbsp在我的资源里有封装好的DLL与ActiveX控制下载,&nbsp直接调用接口使用即可


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明VC获取本机网卡地址
喜欢 (0)
加载中……