如何用delphi获得网卡的物理地址

  • 主题发起人 主题发起人 import
  • 开始时间 开始时间
I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
function GetMacAddresses(const Machine: string; const Addresses: TStrings): Integer; var
NCB: TNCB;
Enum: TLanaEnum;
I, L, NameLen: Integer;
Adapter: ASTAT;
MachineName: string;
begin
Result := -1;
Addresses.Clear;
MachineName := UpperCase(Machine);
if MachineName = '' then
MachineName := '*';
NameLen := Length(MachineName);
L := NCBNAMSZ - NameLen;
if L > 0 then
begin
SetLength(MachineName, NCBNAMSZ);
FillChar(MachineName[NameLen + 1], L, ' ');
end;
FillChar(NCB, SizeOf(NCB), #0);
NCB.ncb_command := NCBENUM;
NCB.ncb_buffer := Pointer(@Enum);
NCB.ncb_length := SizeOf(Enum);
if NetBios(@NCB) = NRC_GOODRET then
begin
Result := Enum.Length;
for I := 0 to Ord(Enum.Length) - 1 do
begin
FillChar(NCB, SizeOf(NCB), #0);
NCB.ncb_command := NCBRESET;
NCB.ncb_lana_num := Enum.lana;
if NetBios(@NCB) = NRC_GOODRET then
begin
FillChar(NCB, SizeOf(NCB), #0);
NCB.ncb_command := NCBASTAT;
NCB.ncb_lana_num := Enum.lana;
Move(MachineName[1], NCB.ncb_callname, SizeOf(NCB.ncb_callname));
NCB.ncb_buffer := PChar(@Adapter);
NCB.ncb_length := SizeOf(Adapter);
if NetBios(@NCB) = NRC_GOODRET then
Addresses.Add(AdapterToString(Adapter.adapt));
end;
end;
end;
end;
************************
//获取网卡地址,使用netbios 3.0调用 uses nb30.pas shipped with delphi 4.0
function _NetCardAddress : String
var
NCB : TNCB // Netbios control block
ADAPTER : TADAPTERSTATUS // Netbios adapter status
LANAENUM : TLANAENUM // Netbios lana
intIdx : Integer // Temporary work value
CRC : Char // Netbios return code
strTemp : String // Temporary string
begin
Result := ''
try
ZeroMemory(@NCB,SizeOf(NCB)) // Zero control blocl
NCB.ncb_command := Chr(NCBENUM) // Issue enum command
cRC := NetBios(@NCB)
if Ord(cRC)>0 then begin
result:=_NBReturnMSG(crc);
exit
end;
 
 
NCB.ncb_buffer := @LANAENUM // Reissue enum command
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB)
if Ord(cRC)>0 then begin
result:=_NBReturnMSG(crc);
exit
end;
ZeroMemory(@NCB,SizeOf(NCB)) // Reset adapter
NCB.ncb_command := Chr(NCBRESET)
NCB.ncb_lana_num := LANAENUM.lana[0]
cRC := NetBios(@NCB)
if Ord(cRC)>0 then begin
result:=_NBReturnMSG(crc);
exit
end;
ZeroMemory(@NCB,SizeOf(NCB)) // Get adapter address
NCB.ncb_command := Chr(NCBASTAT)
NCB.ncb_lana_num := LANAENUM.lana[0]
StrPCopy(NCB.ncb_callname,'*')
NCB.ncb_buffer := @ADAPTER
NCB.ncb_length := SizeOf(ADAPTER)
cRC := NetBios(@NCB)
if Ord(cRC)>0 then begin
result:=_NBReturnMSG(crc);
exit
end; // Convert Netcard address it to string
strTemp := ''
for intIdx := 0 to 5 do strTemp := strTemp + _Char2Hex(ADAPTER.adapter_address[intIdx])
Result := strTemp
finally
end
end
不知你指的是在Window3.2(16Bit Delphi)还是Windows95&98&2000(32Bit Delphi),如果是32位Delphi,使用Netbios(nb30.pas),如果是16位Delphi,使用NetBiosCall(汇编语言)。
 

Similar threads

后退
顶部