如何获取本机所有MAC地址

王朝vc·作者佚名  2006-01-08
宽屏版  字体: |||超大  

// need include file: #include <Nb30.h>

// need libary: Netapi32.lib

int GetAllLocalAdapterMacAddr(std::list<std::vector<unsigned char> >& mac)

{

NCB ncb;

LANA_ENUM AdapterList;

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

ncb.ncb_command = NCBENUM;

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

ncb.ncb_length = sizeof(AdapterList);

Netbios(&ncb);

mac.resize(0);

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

{

struct ASTAT

{

ADAPTER_STATUS adapt;

NAME_BUFFER psz_name[30];

} Adapter;

// Reset the LAN adapter so that we can begin querying it

NCB Ncb;

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

Ncb.ncb_command = NCBRESET;

Ncb.ncb_lana_num = AdapterList.lana[i];

if (Netbios(&Ncb) != NRC_GOODRET)

continue;

// Prepare to get the adapter status block

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

Ncb.ncb_command = NCBASTAT;

Ncb.ncb_lana_num = AdapterList.lana[ i ];

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

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

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

Ncb.ncb_length = sizeof (Adapter);

// Get the adapter's info and, if this works, return it in standard,

// colon-delimited form.

if ( Netbios( &Ncb ) == 0 )

{

std::vector<unsigned char> v6;

v6.resize(6);

for (int i=0; i<6; i++)

v6[i] = Adapter.adapt.adapter_address[i];

if (v6[0] == 0)

{

std::list<std::vector<unsigned char> >::iterator i = mac.begin();

for (; i!=mac.end(); i++) if (*i == v6)

break;

if (i==mac.end())

mac.push_back(v6);

}

}

else

break;

}

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有