如何用Delphi开发读取网卡MAC地址的程序?(300分)

  • 主题发起人 主题发起人 superpage
  • 开始时间 开始时间
S

superpage

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样用delphi实现读取网卡的mac地址?
有源代码更好。
shao1@sina.com
望不吝赐教!
 
我给你寄去一个小程序!查收。
 
//加入nb30.pas单元
var
LanaNum: Byte;
MACAddress: PMACAddress;
RetCode: Byte;
begin
LanaNum := StrToInt(ComboBox1.Text);
New(MACAddress);
try
RetCode := GetMACAddress(LanaNum, MACAddress);
if RetCode = NRC_GOODRET then
begin
Edit1.Text := Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%.2x', MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3], MACAddress[4], MACAddress[5]]);
end else
begin
Beep;
Edit1.Text := 'Error';
ShowMessage('GetMACAddress Error! RetCode = $' + IntToHex(RetCode, 2));
end;
finally
Dispose(MACAddress);
end;
 
function GetAdapterInfo:string;//(Lana: Char): String;
var
Adapter: TAdapterStatus;
NCB: TNCB;
begin

FillChar(NCB, SizeOf(NCB), 0);
NCB.ncb_command := Char(NCBRESET);
NCB.ncb_lana_num := #0;//Lana;
if Netbios(@NCB) <> Char(NRC_GOODRET) then

begin
Result := '';
Exit;
end;

FillChar(NCB, SizeOf(NCB), 0);
NCB.ncb_command := Char(NCBASTAT);
NCB.ncb_lana_num := #0;//Lana;
NCB.ncb_callname := '*';

FillChar(Adapter, SizeOf(Adapter), 0);
NCB.ncb_buffer := @Adapter;
NCB.ncb_length := SizeOf(Adapter);
if Netbios(@NCB) <> Char(NRC_GOODRET) then

begin

Result := '';
Exit;
end;
Result :=
IntToHex(Byte(Adapter.adapter_address[0]), 2) +
IntToHex(Byte(Adapter.adapter_address[1]), 2) +
IntToHex(Byte(Adapter.adapter_address[2]), 2) +
IntToHex(Byte(Adapter.adapter_address[3]), 2) +
IntToHex(Byte(Adapter.adapter_address[4]), 2) +
IntToHex(Byte(Adapter.adapter_address[5]), 2);
end;
只要装了netbios就行了
:)
包含头文件为nb30
 
function TForm1.NBGetAdapterAddress(a: integer): String;
//a指定多个网卡适配器中的哪一个0,1,2...
Var
NCB:TNCB;
// Netbios control block file://NetBios控制块
ADAPTER : TADAPTERSTATUS;
// Netbios adapter status//取网卡状态
LANAENUM : TLANAENUM;
// Netbios lana
intIdx : Integer;
// Temporary work value//临时变量
cRC : Char;
// Netbios return code//NetBios返回值
strTemp : String;
// Temporary string//临时变量
begin
// Initialize
Result := '';
Try
// Zero control blocl
ZeroMemory(@NCB, SizeOf(NCB));
// Issue enum command
NCB.ncb_command:=Chr(NCBENUM);
cRC := NetBios(@NCB);
// Reissue enum command
NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB);
If Ord(cRC)0 then
exit;
// Reset adapter
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[a];
cRC := NetBios(@NCB);
If Ord(cRC)0 then
exit;
// Get adapter address
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[a];
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
cRC := NetBios(@NCB);
// Convert it to string
strTemp := '';
For intIdx := 0 To 5 do
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
Result := strTemp;
Finally
end;

最后别忘了uses nb30.pas
经测试,98,2000下皆可用
 
我来晚了
不过我还听说过另一种方法 利用GUID
guid 的第四部分几位你的网卡卡号
令问bigsun: 再windows中我该指定网卡适配器中的哪一个呢
在windows中网卡适配器的个数好像与安装的协议有关
 
老帖里就有。
查查吧,可能还有其他收获。。
上面的代码就是老帖的。
记得是王寒松给的。
 
接受答案了.
 
我给你寄去的小程序对你没用吗?
 
后退
顶部