如何得到网卡的Mac地址?(50分)

  • 主题发起人 主题发起人 duguqiubai
  • 开始时间 开始时间
D

duguqiubai

Unregistered / Unconfirmed
GUEST, unregistred user!
那位大侠知道如何得到网卡的Mac地址?
 
只能得到网络适配器的Mac地址,但是并不一定是硬件本身的,也可以是注册表中设置的。<br><br>const<br>&nbsp; sNetBiosError = 'NetBIOS错误%d';<br><br>type<br>&nbsp; TMACAddress = packed array[0..5] of Byte;<br>&nbsp; ENetBiosError = class(Exception);<br>&nbsp; TAStat = record Adapt: TAdapterStatus;<br>&nbsp; &nbsp; NameBuff: array[0..30] of TNameBuffer;<br>&nbsp; end;<br><br>function GetMacAddress(AdapterNum: Integer): TMACAddress;<br>var<br>&nbsp; Ncb: TNCB;<br>&nbsp; uRetCode: Char;<br>&nbsp; J: Integer;<br>&nbsp; Adapter: TAStat;<br>begin<br>&nbsp; FillChar(NCB, SizeOf(NCB), 0);<br>&nbsp; with NCB do<br>&nbsp; begin<br>&nbsp; &nbsp; ncb_command := Char(NCBRESET);<br>&nbsp; &nbsp; ncb_lana_num := Char(AdapterNum);<br>&nbsp; end;<br>&nbsp; uRetCode := Netbios(@Ncb);<br>&nbsp; if uRetCode &lt;&gt; #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]);<br>&nbsp; FillChar(NCB, SizeOf(NCB), 0);<br>&nbsp; with NCB do<br>&nbsp; begin<br>&nbsp; &nbsp; ncb_command := Char(NCBASTAT);<br>&nbsp; &nbsp; ncb_lana_num := Char(AdapterNum);<br>&nbsp; &nbsp; StrCopy(ncb_callname, '*');<br>&nbsp; &nbsp; ncb_buffer := @Adapter;<br>&nbsp; &nbsp; ncb_length := sizeof(Adapter);<br>&nbsp; end;<br>&nbsp; uRetCode := Netbios(@Ncb);<br>&nbsp; if uRetCode &lt;&gt; #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]);<br>&nbsp; for J := 0 to 5 do<br>&nbsp; &nbsp; Result[J] := Ord(Adapter.Adapt.Adapter_address[J]);<br>end;<br><br><br>方法2:<br><br>uses nb30;<br><br>function NBGetAdapterAddress(a: Integer): string;<br>var<br>&nbsp; NCB: TNCB; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Netbios control block //NetBios控制块<br>&nbsp; ADAPTER: TADAPTERSTATUS; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Netbios adapter status//取网卡状态<br>&nbsp; LANAENUM: TLANAENUM; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Netbios lana<br>&nbsp; intIdx: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Temporary work value//临时变量<br>&nbsp; cRC: Char; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Netbios return code//NetBios返回值<br>&nbsp; strTemp: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Temporary string//临时变量<br>begin<br>&nbsp; Result := '';<br><br>&nbsp; try<br>&nbsp; &nbsp; ZeroMemory(@NCB, SizeOf(NCB)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Zero control blocl<br><br>&nbsp; &nbsp; NCB.ncb_command := Chr(NCBENUM); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Issue enum command<br>&nbsp; &nbsp; cRC := NetBios(@NCB);<br><br>&nbsp; &nbsp; NCB.ncb_buffer := @LANAENUM; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Reissue enum command<br>&nbsp; &nbsp; NCB.ncb_length := SizeOf(LANAENUM);<br>&nbsp; &nbsp; cRC := NetBios(@NCB);<br>&nbsp; &nbsp; if Ord(cRC) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; exit;<br><br>&nbsp; &nbsp; ZeroMemory(@NCB, SizeOf(NCB)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Reset adapter<br>&nbsp; &nbsp; NCB.ncb_command := Chr(NCBRESET);<br>&nbsp; &nbsp; NCB.ncb_lana_num := LANAENUM.lana[a];<br>&nbsp; &nbsp; cRC := NetBios(@NCB);<br>&nbsp; &nbsp; if Ord(cRC) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; exit;<br><br><br>&nbsp; &nbsp; ZeroMemory(@NCB, SizeOf(NCB)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get adapter address<br>&nbsp; &nbsp; NCB.ncb_command := Chr(NCBASTAT);<br>&nbsp; &nbsp; NCB.ncb_lana_num := LANAENUM.lana[a];<br>&nbsp; &nbsp; StrPCopy(NCB.ncb_callname, '*');<br>&nbsp; &nbsp; NCB.ncb_buffer := @ADAPTER;<br>&nbsp; &nbsp; NCB.ncb_length := SizeOf(ADAPTER);<br>&nbsp; &nbsp; cRC := NetBios(@NCB);<br><br>&nbsp; &nbsp; strTemp := ''; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Convert it to string<br>&nbsp; &nbsp; for intIdx := 0 to 5 do<br>&nbsp; &nbsp; &nbsp; strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);<br>&nbsp; &nbsp; Result := strTemp;<br>&nbsp; finally<br>&nbsp; end;<br>end;
 
[blue]调用nbtstat命令也可以得到MAC地址的。[:D]<br><br>Displays protocol statistics and current TCP/IP connections using NBT<br>(NetBIOS over TCP/IP).<br><br>NBTSTAT [ [-a RemoteName] [-A IP address] [-c] [-n]<br>&nbsp; &nbsp; &nbsp; &nbsp; [-r] [-R] [-RR] [-s] [-S] [interval] ]<br><br>&nbsp; -a &nbsp; (adapter status) Lists the remote machine's name table given its name<br>&nbsp; -A &nbsp; (Adapter status) Lists the remote machine's name table given its<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IP address.<br>&nbsp; -c &nbsp; (cache) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lists NBT's cache of remote [machine] names and their IP<br>&nbsp;addresses<br>&nbsp; -n &nbsp; (names) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lists local NetBIOS names.<br>&nbsp; -r &nbsp; (resolved) &nbsp; &nbsp; &nbsp; Lists names resolved by broadcast and via WINS<br>&nbsp; -R &nbsp; (Reload) &nbsp; &nbsp; &nbsp; &nbsp; Purges and reloads the remote cache name table<br>&nbsp; -S &nbsp; (Sessions) &nbsp; &nbsp; &nbsp; Lists sessions table with the destination IP addresses<br>&nbsp; -s &nbsp; (sessions) &nbsp; &nbsp; &nbsp; Lists sessions table converting destination IP<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addresses to computer NETBIOS names.<br>&nbsp; -RR &nbsp;(ReleaseRefresh) Sends Name Release packets to WINs and then, starts Refr<br>esh<br><br>&nbsp; RemoteName &nbsp; Remote host machine name.<br>&nbsp; IP address &nbsp; Dotted decimal representation of the IP address.<br>&nbsp; interval &nbsp; &nbsp; Redisplays selected statistics, pausing interval seconds<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;between each display. Press Ctrl+C to stop redisplaying<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;statistics.[/blue]
 
难道没有什么方法知道硬件本身的物理地址阿吗?通过Dos命令可以得到本机网卡的物理地址。<br><br>
 
每个网卡自己的维护程序可以读出来他自己的网卡地址。<br>Windows也可以读出来网卡的物理地址。<br>通过程序读取我倒是没有看到过。<br>理论上应该可以,但是使用绝对端口读写不好,似乎只有使用API(如果有)或者驱动了。<br>不过没有听说过。
 
yzhshi:<br>&nbsp; &nbsp;在windows,可以通过ping一台主机的IP地址,然后通过地址解析协议可以得到这台<br>机器的Mac地址。用Windows Api应该能得到主机硬件本身的Mac地址。你的程序我试了试,函数function <br>NBGetAdapterAddress(a: Integer): string;参数a怎么设置?<br><br>&nbsp; <br>
 
a表示是第几个网卡。<br>不过这个得到的不一定是真正的物理mac地址,可能是注册表里面加出来的。
 
酷尔贝塔 不是答了你吗?<br>&nbsp;<br>NBTSTAT -a IP<br><br>
 
库尔贝塔:我需要的是在程序里实现。谢谢,不能给你加分了。
 
[:D]呵呵,没关系,分数老子有的是。<br><br>你可以用函数来调用嘛。<br>比如 ShellExecute &nbsp;来调用外部命令嘛。
 
yzhshi的方法2好使亚,在98、2000、xp下都可以用的,我一直用的这个<br>(呵呵,像说大宝广告似的:)
 
更简单的方法,产生一个GUID,MAC是其中的一部分。
 
多人接受答案了。
 
后退
顶部