unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
// MAC Address
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Types, WinSock, Nb30;
// MAC Address
type
TMacAddress = array [0..5] of Byte;
TMacAddresses = array of TMacAddress;
function ToHex(Value: TByteDynArray; const Separator: string): string;
var I: Integer;
begin
Result := '';
for I := 0 to Length(Value) - 1 do
begin
if I > 0 then
Result := Result + Separator;
Result := Result + IntToHex(Value[I], 2);
end;
end;
function MacAddresses: TMacAddresses;
var
Ncb: TNCB;
LanaEnum: TLanaEnum;
I, Index: Integer;
AdapterStatus: TAdapterStatus;
begin
Result := nil;
Index := 0;
FillChar(Ncb, SizeOf(Ncb),0);
Byte(Ncb.ncb_command) := NCBENUM;
Ncb.ncb_buffer := @LanaEnum;
Ncb.ncb_length := SizeOf(LanaEnum);
if Ord(NetBios(@Ncb)) = NRC_GOODRET then
begin
SetLength(Result, Ord(LanaEnum.length));
for I := 0 to Length(Result) - 1 do
begin
FillChar(Ncb, SizeOf(Ncb),0);
Byte(Ncb.ncb_command) := NCBRESET;
Ncb.ncb_lana_num := LanaEnum.lana[I];
if Ord(NetBios(@Ncb)) = NRC_GOODRET then
begin
FillChar(Ncb, SizeOf(Ncb),0);
Byte(Ncb.ncb_command) := NCBASTAT;
Ncb.ncb_lana_num := LanaEnum.lana[I];
FillChar(Ncb.ncb_callname, SizeOf(Ncb.ncb_callname), ' ');
AnsiChar(Ncb.ncb_callname[0]) := '*';
FillChar(AdapterStatus, SizeOf(AdapterStatus), 0);
Ncb.ncb_buffer := @AdapterStatus;
Ncb.ncb_length := SizeOf(AdapterStatus);
if Ord(NetBios(@Ncb)) = NRC_GOODRET then
begin
Move(AdapterStatus.adapter_address[0], Result[Index], SizeOf(TMacAddress));
Inc(Index);
end;
end;
end;
SetLength(Result, Index);
end;
end;
function Catenate(const S1, S2: string; const Separator: string = ', '): string; overload;
begin
if S1 = '' then
Result := S2
else if S2 = '' then
Result := S1
else
Result := S1 + Separator + S2;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Text: string;
Addresses: TMacAddresses;
Address: TByteDynArray;
I: Integer;
begin
Text := '';
SetLength(Address, Sizeof(TMacAddress));
Addresses := MacAddresses;
for I := 0 to Length(Addresses) - 1 do
begin
Move(Addresses[I], Address[0], SizeOf(TMacAddress));
Text := Catenate(Text, 'Adapter ' + IntToStr(I + 1) + ': ' + ToHex(Address, '-'));
end;
Memo1.Lines.Add(Text);
end;
end.