第一:应用 Netbios 函数获取网卡地址(NetAPI32.Lib)
应用心得:
XP2
MS XP Professional 2002 XP2
集成网卡
Intel(R) PRO/100 VE Network Connection
PCI网卡均可使用
Realtek RTL8139 Family PCI Fast Ethernet NIC
#include "nb30.h" //NCB结构定义
#pragma comment(lib, "NetAPI32.Lib") //Netbios 函数
缺点:仅能获取已连接上的网络连接,没有插入网线,连接被禁用的都无法获取
NetBIOS中所用的函数声明、常数等等均是在头文件nb30.h内定义的,nb30.h中一些类型在wtypes.h中定义,因此一般在网络程序先include "wtypes.h"再nb30.h。另外,使用NetBIOS还须连接库NetAPI32.Lib。调用NetBIOS函数时,可使用NetBIOS中提供的一个唯一的函数调用 Netbios(&ncb);
代码实例:
#include "nb30.h" //NCB结构定义
#pragma comment(lib, "NetAPI32.Lib") //Netbios 函数
bool AdaptersInfo(int nAdapter_num, TCHAR * tchMac_addr)
{
//重置网卡
NCB ncb = {0};
memset(&ncb, 0, sizeof(ncb));
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = nAdapter_num;
if( Netbios(&ncb) != NRC_GOODRET )
{
//出错
sprintf(tchMac_addr, "%s0x%0x", "bad (NCBRESET): ", ncb.ncb_retcode);
return 0;
}
//取得网卡的状态块
memset(&ncb, 0, sizeof(ncb));
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = nAdapter_num;
strcpy((char *) ncb.ncb_callname, "*");
struct ASTAT
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff[30];
} Adapter;
memset(&Adapter, 0, sizeof(Adapter));
ncb.ncb_buffer = (unsigned char *)&Adapter;
ncb.ncb_length = sizeof(Adapter);
//仅能获取已连接的网卡信息
if( Netbios(&ncb) == 0 )
{
char chMAC[18] = {0};
sprintf(chMAC, "%02X:%02X:%02X:%02X:%02X:%02X",
int(Adapter.adapt.adapter_address[0]),
int(Adapter.adapt.adapter_address[1]),
int(Adapter.adapt.adapter_address[2]),
int(Adapter.adapt.adapter_address[3]),
int(Adapter.adapt.adapter_address[4]),
int(Adapter.adapt.adapter_address[5]));
memset(tchMac_addr, 0, sizeof(tchMac_addr));
memcpy(tchMac_addr, chMAC, sizeof(chMAC));
return 1;
}
else
{
sprintf(tchMac_addr, "%s%d", "", ncb.ncb_retcode);
tchMac_addr = "bad (NCBASTAT): ";
return 0;
}
}
void GetNICInfo()
{
LANA_ENUM AdapterList;
memset(&AdapterList, 0 ,sizeof(AdapterList));
NCB ncb;
memset(&ncb, 0, sizeof(NCB));
ncb.ncb_command = NCBENUM; //枚举网卡
ncb.ncb_buffer = (unsigned char *)&AdapterList;
ncb.ncb_length = sizeof(AdapterList);
Netbios(&ncb);
for(int i= 0; i<AdapterList.length; i++)
{
TCHAR tchMac_addr[18] = {0};
if(AdaptersInfo((int)AdapterList.lana[i], tchMac_addr))
{
AfxMessageBox(tchMac_addr);
}
else
{
AfxMessageBox(tchMac_addr);
}
}
}
代码应用:调用GetNICInfo()即可,网卡地址存放于 tchMac_addr 变量
附相关Netbios命令简介
NETENUM:枚举系统的网卡数量,不是标准的 NetBIOS 3.0 命令
NCBRESET:重置网卡。网卡在接受新的NCB命令之前必须重置。
NCBASTAT:接受网卡的状态信息。网卡地址详细信息存入于 ADAPTER_STATUS::adapter_address[]的(0-5)中
(a)、NCB结构简介
/*
memset(ncb.ncb_callname, 0x20, NCBNAMSZ-1);
strcpy((char*)ncb.ncb_callname, (LPCTSTR)tchBiosName);
ncb.ncb_callname[strlen(tchBiosName)] = 0x20;
ncb.ncb_callname[NCBNAMSZ] = 0x0;
*/
.ncb_callname:由16个字节组成,前15个字节为ASCII码,若不满,要用空格填满。这里最后一字节为NULL(0x0)