#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助手)函数使得这一过程变得更简单。