unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
function GetWinMacAddress: String;
public
{ public declarations }
end;
function GetIfTable( pIfTable : Pointer;
VAR pdwSize : LongInt;
bOrder : LongInt ): LongInt; stdcall;
var
Form1: TForm1;
implementation
{$R *.lfm}
function GetIfTable( pIfTable : Pointer;
VAR pdwSize : LongInt;
bOrder : LongInt ): LongInt; stdcall; external 'IPHLPAPI.DLL';
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
// Below is necessary to be able to see form on the mini device screen
Left := 0;
Top := 25;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Lines.Add(EmptyStr);
Memo1.Lines.Add(GetWinMacAddress);
end;
function TForm1.GetWinMacAddress: String;
const
MAX_INTERFACE_NAME_LEN = $100;
ERROR_SUCCESS = 0;
MAXLEN_IFDESCR = $100;
MAXLEN_PHYSADDR = 8;
MIB_IF_TYPE_ETHERNET = 6;
_MAX_ROWS_ = 20;
type
MIB_IFROW = Record
wszName : Array[0 .. (MAX_INTERFACE_NAME_LEN * 2 - 1)] of char;
dwIndex : LongInt;
dwType : LongInt;
dwMtu : LongInt;
dwSpeed : LongInt;
dwPhysAddrLen : LongInt;
bPhysAddr : Array[0 .. (MAXLEN_PHYSADDR-1)] of Byte;
dwAdminStatus : LongInt;
dwOperStatus : LongInt;
dwLastChange : LongInt;
dwInOctets : LongInt;
dwInUcastPkts : LongInt;
dwInNUcastPkts : LongInt;
dwInDiscards : LongInt;
dwInErrors : LongInt;
dwInUnknownProtos : LongInt;
dwOutOctets : LongInt;
dwOutUcastPkts : LongInt;
dwOutNUcastPkts : LongInt;
dwOutDiscards : LongInt;
dwOutErrors : LongInt;
dwOutQLen : LongInt;
dwDescrLen : LongInt;
bDescr : Array[0 .. (MAXLEN_IFDESCR - 1)] of Char;
end;
_IfTable = Record
nRows : LongInt;
ifRow : Array[1.._MAX_ROWS_] of MIB_IFROW;
end;
var
pIfTable : ^_IfTable;
TableSize : LongInt;
tmp : String;
i,j : Integer;
ErrCode : LongInt;
begin
pIfTable := nil;
//------------------------------------------------------------
Result := '';
try
//-------------------------------------------------------
// First: just get the buffer size.
// TableSize returns the size needed.
TableSize := 0; // Set to zero so the GetIfTabel function
// won't try to fill the buffer yet,
// but only return the actual size it needs.
GetIfTable(pIfTable, TableSize, 1);
if (TableSize < SizeOf(MIB_IFROW) + Sizeof(LongInt)) then
begin
Exit; // less than 1 table entry?!
end; // if-end.
// Second:
// allocate memory for the buffer and retrieve the
// entire table.
GetMem(pIfTable, TableSize);
ErrCode := GetIfTable(pIfTable, TableSize, 1);
if (ErrCode <> ERROR_SUCCESS) then
begin
Exit; // OK, that did not work.
// Not enough memory i guess.
end; // if-end.
// Read the ETHERNET addresses.
for i := 1 to pIfTable^.nRows do
try
if (pIfTable^.ifRow[i].dwType=MIB_IF_TYPE_ETHERNET) and
(pIfTable^.ifRow[i].dwOutOctets <> 0) then
begin
tmp := '';
for j:=0 to pIfTable^.ifRow[i].dwPhysAddrLen-1 do
begin
tmp := tmp + format('%.2x:',
[ pIfTable^.ifRow[i].bPhysAddr[j] ] );
end; // for-end.
//-------------------------------------
if Length(tmp)>0 then
begin
Result := Copy(tmp, 1, Length(tmp) - 1);
Exit;
end;
end; // if-end.
except
Exit;
end; // if-try-except-end.
finally
if Assigned(pIfTable) then FreeMem(pIfTable, TableSize);
end; // if-try-finally-end.
end;
end.