Beranda > Delphi > How to Get MacAddress of LAN card

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;
Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: