如何获取网卡的MAC地址?

diecode 2003-08-12 11:11:49
如何获取本机网卡的MAC地址和网上其他机器的MAC地址呢?
...全文
25 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
suny_2001 2003-08-12
  • 打赏
  • 举报
回复
读取网卡上的mac 地址应该

unit MacAdress;

interface


uses SysUtils,NB30, Classes, Controls, StdCtrls,windows;

function Getmac:string;

implementation

type
PASTAT = ^TASTAT;
TASTAT = record
adapter : TAdapterStatus;
name_buf : TNameBuffer;
end;





function Getmac:string;
var
ncb : TNCB;
s:string;
adapt : TASTAT;
lanaEnum : TLanaEnum;
i, j, m : integer;
strPart, strMac : string;
begin
result:='';
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Char(NCBEnum);
ncb.ncb_buffer := PChar(@lanaEnum);
ncb.ncb_length := SizeOf(TLanaEnum);
s:=Netbios(@ncb);
m:=1;
if (Win32Platform = VER_PLATFORM_WIN32_WINDOWS) then
m:=0;
for i := 0 to integer(lanaEnum.length)-1 do
begin
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Char(NCBReset);
ncb.ncb_lana_num := lanaEnum.lana[i];
Netbios(@ncb);
Netbios(@ncb);
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Chr(NCBAstat);
ncb.ncb_lana_num := lanaEnum.lana[i];
ncb.ncb_callname := '* ';
ncb.ncb_buffer := PChar(@adapt);
ncb.ncb_length := SizeOf(TASTAT);

if m=1 then
begin
if Netbios(@ncb) = Chr(0) then
strMac := '';
for j := 0 to 5 do
begin
strPart := IntToHex(integer(adapt.adapter.adapter_address[j]), 2);
strMac := strMac + strPart ;
end;
SetLength(strMac, Length(strMac));
end;
if m=0 then
if Netbios(@ncb) <> Chr(0) then
begin
strMac := '';
for j := 0 to 5 do
begin
strPart := IntToHex(integer(adapt.adapter.adapter_address[j]), 2);
strMac := strMac + strPart ;
end;
SetLength(strMac, Length(strMac));
end;
end;
result:=trim(strmac);
end;
lastshrill 2003-08-12
  • 打赏
  • 举报
回复
ipconfig /all
可以分析一下哦。
楼上都说了,可是楼上的程序似乎对多网卡有点问题。
DWGZ 2003-08-12
  • 打赏
  • 举报
回复
找到一个

Procedure TForm1.Button1Click(Sender : TObject);
Begin
label1.Caption:=NBGetAdapterAddress(StrtoInt(Edit1.Text));
End;

Function NBGetAdapterAddress(a:Integer) : String;
Var

NCB : TNCB; // Netbios control block //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[in
tIdx]),2);
Result := strTemp;
Finally
End;
End;
DWGZ 2003-08-12
  • 打赏
  • 举报
回复
找到一个

Procedure TForm1.Button1Click(Sender : TObject);
Begin
label1.Caption:=NBGetAdapterAddress(StrtoInt(Edit1.Text));
End;

Function NBGetAdapterAddress(a:Integer) : String;
Var

NCB : TNCB; // Netbios control block //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[in
tIdx]),2);
Result := strTemp;
Finally
End;
End;

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧