95/NT下编程根据网卡名称取得MAC

ypos 2005-02-22 10:24:47
网卡名称例子:{628C7006-2854-460E-9724-2435C7A6E33E}
98/Me/2000/XP/2003下的通过GetAdaptersInfo的方法我已经知道了
...全文
75 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
月吻长河 2005-02-25
用程序也可以
netbios.cpp

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;
#define bzero(thing,sz) memset(thing,0,sz)

bool GetAdapterInfo(int adapter_num, string &mac_addr)
{
// 重置网卡,以便我们可以查询
NCB Ncb;
memset(&Ncb, 0, sizeof(Ncb));
Ncb.ncb_command = NCBRESET;
Ncb.ncb_lana_num = adapter_num;
if (Netbios(&Ncb) != NRC_GOODRET) {
mac_addr = "bad (NCBRESET): ";
mac_addr += string(Ncb.ncb_retcode);
return false;
}

// 准备取得接口卡的状态块
bzero(&Ncb,sizeof(Ncb);
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = adapter_num;
strcpy((char *) Ncb.ncb_callname, "*");
struct ASTAT
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff[30];
} Adapter;
bzero(&Adapter,sizeof(Adapter));
Ncb.ncb_buffer = (unsigned char *)&Adapter;
Ncb.ncb_length = sizeof(Adapter);

// 取得网卡的信息,并且如果网卡正常工作的话,返回标准的冒号分隔格式。
if (Netbios(&Ncb) == 0)
{
char acMAC[18];
sprintf(acMAC, "%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]));
mac_addr = acMAC;
return true;
}
else
{
mac_addr = "bad (NCBASTAT): ";
mac_addr += string(Ncb.ncb_retcode);
return false;
}
}

int main()
{
// 取得网卡列表
LANA_ENUM 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);

// 取得本地以太网卡的地址
string mac_addr;
for (int i = 0; i < AdapterList.length - 1; ++i)
{
if (GetAdapterInfo(AdapterList.lana, mac_addr))
{
cout << "Adapter " << int (AdapterList.lana) <<
"'s MAC is " << mac_addr << endl;
}
else
{
cerr << "Failed to get MAC address! Do you" << endl;
cerr << "have the NetBIOS protocol installed?" << endl;
break;
}
}

return 0;
}
回复
月吻长河 2005-02-25
简单得很!
“开始”,“运行”COMMAND 回车
ping一下该网卡的IP,然后arp -PRINT就看到了。
回复
galaxy_fxstar 2005-02-22
有个取巧的方法:
使用exec()调用ipconfig/all,获得其输出,然后从其中抽取相应的MAC地址字符串
回复
master_ttch 2005-02-22
http://www.csdn.com.cn/program/4475.htm
回复
galaxy_fxstar 2005-02-22
-------ipconfig不能在95/NT下运行
难道95/nt下面没有用来查看网络信息的程序,类似ipconfig的?
没有用过nt,不太清楚,不过既然98/me/xp等都有GetAdaptersInfo方法,那么我想95/nt也应该有的,希望搂住查一下他们的sdk,应该会有收获
回复
kugou123 2005-02-22
同意galaxy_fxstar(阿星)

建立管道,获取ipconfig/all的输出信息,然后抽取MAC地址串即可。
回复
用Netbios API
回复
ypos 2005-02-22
ipconfig不能在95/NT下运行
回复
发动态
发帖子
网络编程
创建于2007-09-28

1.8w+

社区成员

VC/MFC 网络编程
申请成为版主
社区公告
暂无公告