首页  编辑  

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

Tags: /超级猛料/Network.网络通讯/网卡、硬件/   Date Created:
uses
 NB30;
function GetAdapterInfo(Lana: Char): String;
var
 Adapter: TAdapterStatus;
 NCB: TNCB;
begin
 FillChar(NCB, SizeOf(NCB), 0);
 NCB.ncb_command := Char(NCBRESET);
 NCB.ncb_lana_num := Lana;
 if Netbios(@NCB) <> Char(NRC_GOODRET) then
 begin
   Result := 'mac not found';
   Exit;
 end;
 FillChar(NCB, SizeOf(NCB), 0);
 NCB.ncb_command := Char(NCBASTAT);
 NCB.ncb_lana_num := 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 := 'mac not found';
   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;
function GetMACAddress: string;
var
 AdapterList: TLanaEnum;
 NCB: TNCB;
begin
 FillChar(NCB, SizeOf(NCB), 0);
 NCB.ncb_command := Char(NCBENUM);
 NCB.ncb_buffer := @AdapterList;
 NCB.ncb_length := SizeOf(AdapterList);
 Netbios(@NCB);
 if Byte(AdapterList.length) > 0 then
   Result := GetAdapterInfo(AdapterList.lana[0])
 else
   Result := 'mac not found';
end;
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[I];
if NetBios(@NCB) = NRC_GOODRET then
begin
FillChar(NCB, SizeOf(NCB), #0);
NCB.ncb_command := NCBASTAT;
NCB.ncb_lana_num := Enum.lana[I];
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(汇编语言)。