uses System.Classes,
Winapi.Windows, System.SysUtils, IpHlpApi, IpTypes;
procedure EnumMacAddresses(
const Dest: TStrings);
const
AF_UNSPEC = 0;
AF_INET = 2;
AF_INET6 = 23;
IF_TYPE_ETHERNET_CSMACD = 6;
var
AdapterAddresses: PIP_ADAPTER_ADDRESSES;
BufSize: ULONG;
i: Cardinal;
SL: TStringList;
ResultList: TStringList;
tmp: PIP_ADAPTER_ADDRESSES;
MaxLen: integer;
begin
Assert(Assigned(Dest), '
Keine Empfängerliste für die MAC-Adressen übergeben');
Dest.BeginUpdate;
try
ResultList :=
nil;
BufSize := SizeOf(IP_ADAPTER_ADDRESSES);
GetMem(AdapterAddresses, BufSize);
BufSize := 0;
SL := TStringList.Create;
SL.Delimiter := '
-';
SL.StrictDelimiter := true;
try
ResultList := TStringList.Create;
ResultList.Sorted := true;
ResultList.Duplicates := dupIgnore;
if GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_UNICAST
or
GAA_FLAG_SKIP_ANYCAST
or GAA_FLAG_SKIP_MULTICAST
or
GAA_FLAG_SKIP_DNS_SERVER,
nil, AdapterAddresses, @BufSize) = ERROR_BUFFER_OVERFLOW
then
begin
ReAllocMem(AdapterAddresses, BufSize);
if GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_UNICAST
or
GAA_FLAG_SKIP_ANYCAST
or GAA_FLAG_SKIP_MULTICAST
or
GAA_FLAG_SKIP_DNS_SERVER,
nil, AdapterAddresses, @BufSize) = ERROR_SUCCESS
then
begin
tmp := AdapterAddresses;
while Assigned(tmp)
do
begin
SL.Clear;
try
MaxLen := tmp^.PhysicalAddressLength;
if (MaxLen <= MAX_ADAPTER_ADDRESS_LENGTH)
and (MaxLen > 0)
then
for i := 0
to MaxLen - 1
do
SL.Add(Format('
%.2x', [tmp^.PhysicalAddress[i]]));
except
SL.Clear;
end;
if SL.Count > 0
then
ResultList.Add(SL.DelimitedText);
tmp := tmp^.Next;
end;
end;
end;
Dest.Assign(ResultList);
finally
FreeMem(AdapterAddresses);
SL.Free;
ResultList.Free;
end;
finally
Dest.EndUpdate;
end;
end;