获得网卡MAC地址 .

#include "nb30.h"

#pragma comment(lib,"netapi32.lib")

 

 

void GetNetcarMAC(void)

{

       typedef struct _ASTAT_

       {

              ADAPTER_STATUS adapt;

              NAME_BUFFER    NameBuff [30];

       }ASTAT, * PASTAT;

 

       CString strMAC ;

       ASTAT Adapter;

       memset( &Adapter , 0 , sizeof( ASTAT ) ) ;

       NCB ncb ;

       LANA_ENUM lana_enum ;

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

      

       //

       // 获取当前计算机的网卡信息,有多少个网卡

       //

       ncb.ncb_command = NCBENUM ;

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

       ncb.ncb_length = sizeof(LANA_ENUM);

 

       UCHAR uRetCode = ::Netbios( &ncb ) ;

       if( uRetCode == 0 )

       {

        //

        // lana_enum.length 是网卡的数量,虚似网卡也算

        //

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

              {

                     //

                     // 初始化该网卡

                     //

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

                     ncb.ncb_command = NCBRESET ;

                     ncb.ncb_lana_num = lana_enum.lana[0] ;

 

                     uRetCode = ::Netbios( &ncb ) ;

 

 

                     //

                     // 初始化完毕 , 获取网卡信息,MAC地址被保存于Adapter中

                     //

                     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 == 0 )

                     {

                //

                // 将MAC地址以字符串方式保存在strMAC中

                //

                            strMAC.Format( _T("%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] );

                     }

                    

              }

       }

 

}

 

在95和NT系统下,这是通过程序查得机器MAC的唯一方法,但是98和2000以后,新的

IP Helper(IP助手)函数使得这一过程变得更简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值