2024年5月25日发(作者:)
维普资讯
Microcomputer Applications Vo1.18,No.7,2002 技术交流 微型电脑应用 2002年第l8卷第7期
利用N etBIOS获得MAC地址
张 勃
摘要
李铁楠
type
刘振华
本文介绍了获得网络接口卡MAC地址的一种
方法,并给出在Delphi中调用NetBIOS API获得MAC地址
的具体实现。
TForml=class(TForm)
Edit1:Tedit;
关键词MAC地址NetBIOS API
Button1:Tbutton;
procedure ButtonlClick(Sender:TObject);
private
一
、
引言
{Private declarations)public
{Public declarations:
在实际的应用系统中,我们往往会需要在程序运行时获
取当前机器网卡的MAC地址,以便作为某种标识之用,如控
制程序的合法性等。MAC地址即介质访问控制地址,是由
IEEE和制造商给网络接口卡(NIC)的唯一48位码,是全球
Var
end;
Form1:TForm1;
implementation
{¥R*.DFM)
唯一的硬件(物理)地址。网络上不会存在相同MAC地址的
计算机。MAC地址前3个字节是IEEE分配给制造商的,后3
//首先获得本机网卡号,网卡编号一般从0开始。
function GetLanaNum:Byte;
Var
个字节是制造商给定。其中包含制造厂商、产品批次等信息。
MAC地址通常是不能修改的。基于这些特性,MAC地址在计
算机编程中得到广泛应用。
“网络基本输入/输出系统”(Network Basic Input/Out—
LanaEnumNCB:PNCB;
LanaEnum:PlanaEnum;
I:Integer;
begin
put System,NetBIOS)是1983年由Sytex公司为IBM公司开
发的一种标准应用程序接VI,是与“协议无关”的编程接VI。我
们通过网络输入输出系统提供的用户访问接口API来获得
MAc地址,即通过Windows9x/NT/win2000中内置的Ne—
tApi32.DLL的功能来实现。
New(LanaEnumNCB);
ZeroMemory(LanaEnumNCB,Size0f(TNCB));
try
with LanaEnumNCB do
begin
ncb—buffer:=PChar(LanaEnum);
ncb—length:=SizeOf(TLanaEnum);
二、NetBIOS编程的实现
具体的方法为:设置不同的NetBIOS()参数来完成各种
任务。首先通过发送NCBENUM命令获取网卡的数目和每个
网卡的内部编号.然后对每个网卡标号发送NCBASTAT命
令获取其MAC地址。下面用Delphi实现编程。
unit macunit;
//向网卡发送NCGENUM命令,以获取网卡的编号
ncb command:=Char(NCBENUM):
NetBios(LanaEnumNCB);
If Byte(ncb—cmd—cplt =NRC GoodRET then
Result:=Byte(LanaEnum.Lana[O]);
end;
finally
Dispose(LanaEnumNCB);
end;
end;
interface
u C
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Di—
alogs,StdCtrls,NB30;
//此函数获得指定网卡号的MAC地址,并以16进制格式显示,如
0050BAAAEDE3
//注意;在uses中添加nb30,因为它包含了NetBIOS()的所有函数
及常数的声明。
type
//若有多个网卡,则先用函数GetLanaNum获得网卡号
function GetMACAddress(LanaNum:Byte):string;//LanaNum为指
定网卡号
Var
PMACAddress=’TMACAddresS:
TMACAddress:array[O..5]of Byte;
AdapterStatus:PadatpterStatu
张 劫 石家庄邮政高等专科学校计算中心 讲师 硕士石家庄050021
李铁楠石家庄邮政高等专科学校计算中心助教石家庄050021
・
49・
维普资讯
Microcomputer Applications Vo1.18,No.7,2002
MACAddress:PMACAddress;
StatNCB:PNCB;
技术交流 微型电脑应用 2002年第l8卷第7期
result:=Format( 2.2x 2.2x 2.2x 2.2x 2.2x 2.2x
[MACAddress[O],MACAddress[1].MAcAddress[2],
MACAddress[3].MAcAddress[4],MACAddress[5]].);
end;
end;
begin
New(StatNCB):
ZeroMemory(StatNCB,SizeOf(TNCB)):
StatNCB.ncb—length:=Size0f(TAdapterStatus)+
(TNameBuffer):
GetMem(AdapferSfafus.StatNCB.ncb length);
try
255*SizeOf
finally
FreeMem(AdapterStatus);
Dispose(StatNCB);
end;
with StatNCB do
begin
end;
procedure TForm1.ButtonlClick(Sender:TObject);
begin
ZeroMemory(MACAddress,SizeOf(TMACAddress));
ncb~buffer:=PChar(AdapterStatus);//指定返回信息存放的
edit1.text:=GetMACAddress(GetLanaNum);
end;
变量
ncb callname:= * +#0;
end.
ncb~lana num:=Char(LanaNum);//指定网卡号
此代码在Windows98+Delphi 5环境运行通过。
ncb..command:=Char();//对选定的网卡发送NCBASTAT命
令 以获得网卡的信息
NetBios(StatNCB);
参考文献
[1]Charlie Calvert,Delphi 4编程内幕,机械工业出版社.
1999.6
//nob cmd—cplt存放命令完成的标志,如果成功.则返回NRC—
GO()DRET
if Byte(ncb—cmd—cph)=NRC—GOODRET then
begin
-I2]王忠、迟忠先.Delphi 5开发指南.电子工业出版社,2001.
1
MoveMemory(MACAddress.AdapterStat LIN.SizeOf(TMACAd—
dress));
//,'te ̄MAC地址转化成1 6进制
鎏 !匠
篁 望望罐
(收稿日期:2001年lO月29日)
篁 望
(上接第30页)
Response.Write HREF Noext~file </A><BR>”
else return:
el—e1.children[pos3:
if( UL”==e1.tagName)』
if( ==e1.style.display)f
e1.style.display= block”:
』
Response.Write”%/td></tr ̄”
End if
Nexl
Response.write”<f,talbe ̄”
(9)定义层叠样式表
%style TYPE=”text/css”>
UL{margin—left:1 8pt}
UI UL{display:nolle:margin--left:1Opt
A:link{color:#00000;;
else{
e1.style.display=
,
l
f
event.canceBubble—true:
A:visited{color:#800080;
A active{color:#006600;}
A:hover{text—decoration:underline;color:#FF6666;
function chenckParent(src.dest){
while(src!一nul1){
if(src.tagName一=dest)return src
src src.parent Element:
</style ̄
(10)点击上一层的条目.展开或收起它的下一层
function outline()
var open—e、 ent.srcElement:
var el—checkParent(open, LI );
if(null!=el_I{
var pos 0:
return nul1;
l
』
document.onclick=outline
参考文献
[1]陈会安,ASP3.0与4/5网站架设彻底研究.中国青年再版
社.200l
for(var pos=0;pos%e1.children.1ength;pos++)
if( uL ==e1.children[pos].tagName)break;
f(pos==e1.children.1ength)return
・
(收稿El期:2001年11月2O日)
5O・
发布评论