unit Talo_Mac;
interface
uses Classes, SysUtils, Windows;
const MAX_ADAPTER_NAME_LENGTH = 256; MAX_ADAPTER_DESCRIPTION_LENGTH = 128; MAX_ADAPTER_ADDRESS_LENGTH = 8;
type TIPAddressString = array[0..4 * 4 - 1] of Char;
PIPAddrString = ^TIPAddrString; TIPAddrString = record Next: PIPAddrString; IPAddress: TIPAddressString; IPMask: TIPAddressString; Context: Integer; end;
PIPAdapterInfo = ^TIPAdapterInfo; TIPAdapterInfo = record { IP_ADAPTER_INFO } Next: PIPAdapterInfo; ComboIndex: Integer; AdapterName: array[0..MAX_ADAPTER_NAME_LENGTH + 3] of Char; Description: array[0..MAX_ADAPTER_DESCRIPTION_LENGTH + 3] of Char; AddressLength: Integer; Address: array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte; Index: Integer; _Type: Integer; DHCPEnabled: Integer; CurrentIPAddress: PIPAddrString; IPAddressList: TIPAddrString; GatewayList: TIPAddrString; end;
function GetAdaptersInfo(AI: PIPAdapterInfo; var BufLen: Integer): Integer; stdcall; external 'iphlpapi.dll' Name 'GetAdaptersInfo'; function MACToStr(ByteArr: PByte; Len: Integer): string; function IPToStr(Addr: PIPAddrString): string; function GetMacs(): string; function GetIPs(): string;
implementation
function MACToStr(ByteArr: PByte; Len: Integer): string; begin Result := ''; while (Len > 0) do begin Result := Result + IntToHex(ByteArr^, 2) + '-'; ByteArr := Pointer(Integer(ByteArr) + SizeOf(Byte)); Dec(Len); end; SetLength(Result, Length(Result) - 1); { remove last dash } end;
function IPToStr(Addr: PIPAddrString): string; begin Result := ''; while (Addr <> nil) do begin Result := Result + 'A: ' + Addr^.IPAddress + ' M: ' + Addr^.IPMask + #13; Addr := Addr^.Next; end; end;
function GetMacs(): string; var AI, Work: PIPAdapterInfo; Size: Integer; Res: Integer; Macs: string; begin Size := 5120; GetMem(AI, Size); work := ai; Res := GetAdaptersInfo(AI, Size); if (Res <> ERROR_SUCCESS) then begin SetLastError(Res); RaiseLastWin32Error; end; Macs := ''; repeat if Macs = '' then Macs := MACToStr(@Work^.Address, Work^.AddressLength) else Macs := Macs + ',' + MACToStr(@Work^.Address, Work^.AddressLength); work := work^.Next; until (work = nil); Result := Macs; end;
function GetIPs(): string; var AI, Work: PIPAdapterInfo; Size: Integer; Res: Integer; IPs: string; begin Size := 5120; GetMem(AI, Size); work := ai; Res := GetAdaptersInfo(AI, Size); if (Res <> ERROR_SUCCESS) then begin SetLastError(Res); RaiseLastWin32Error; end; IPs := ''; repeat if IPs = '' then IPs := IPToStr(@Work^.IPAddressList) else IPs := IPs + ',' + IPToStr(@Work^.IPAddressList); work := work^.Next; until (work = nil); Result := IPs; end;
end.
|