svět, kde se rodí nová budoucnost   
 
Delphi headDelphi Tips

How to Get MacAddress of LAN card

unit MACAddress;

interface

uses SysUtils;

type
  TMACAddress = packed array[0..5] of Byte;
 
  ENetBiosError = class(Exception);

function LanAdapterCount : Integer;

function GetMacAddress(AdapterNum: Integer):TMACAddress;


implementation

uses NB30, Windows;

resourcestring
  sNetBiosError = 'NetBIOS error %d';

type
  TAStat = record
    Adapt    : TAdapterStatus;
    NameBuff : array[0..30] of TNameBuffer;
  end;

function LanAdapterCount : Integer;
var
  Ncb : TNCB;
  uRetCode : Char;
  lEnum : TLanaEnum;
begin
  FillChar( NCB, SizeOf(NCB), 0 );
  with NCB do begin
    ncb_command := Char( NCBENUM );
    ncb_buffer  := @lEnum;
    ncb_length  := SizeOf(lEnum);
  end;
  uRetCode := Netbios( @Ncb );
  if uRetCode <> #0 then 
   raise Exception.CreateFmt( sNetBIOSError,
                                      [Ord(uRetCode)]);
  Result := Ord(lenum.length);
end;
function GetMacAddress(AdapterNum:Integer):TMACAddress;
var
  Ncb : TNCB;
  uRetCode : Char;
  J : Integer;
  Adapter : TAStat;
begin
  FillChar( NCB, SizeOf(NCB), 0 );
  with NCB do begin
    ncb_command := Char(NCBRESET);
    ncb_lana_num := Char( AdapterNum );
  end;
  uRetCode := Netbios( @Ncb );
  if uRetCode <> #0 then 
   raise Exception.CreateFmt(sNetBIOSError,
                                     [Ord(uRetCode)]);
  FillChar( NCB, SizeOf(NCB), 0 );
  with NCB do begin
    ncb_command := Char(NCBASTAT);
    ncb_lana_num := Char( AdapterNum );
    StrCopy( ncb_callname,  '*                ' );
    ncb_buffer := @Adapter;
    ncb_length := sizeof(Adapter);
  end;
  uRetCode := Netbios( @Ncb );
  if uRetCode <> #0 then 
   raise Exception.CreateFmt(sNetBIOSError,
                                     [Ord(uRetCode)]);
  for J := 0 to 5 do
   Result[J] := Ord( Adapter.Adapt.Adapter_address[J]);
end;


Back to Index of Tips

 

 

Send mail to radek.novak@infojet.cz with questions or comments about this web site.
Copyright © 1999-2002 Infojet.cz
Last modified: 26.07.2002