[fpc-pascal] How to list IP and MAC of network adapters on Windows?

Michael Van Canneyt michael at freepascal.org
Fri Sep 10 10:26:49 CEST 2021



On Fri, 10 Sep 2021, Sven Barth via fpc-pascal wrote:

> Bo Berglund via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
> Fr., 10. Sep. 2021, 09:41:
>
>> Is there a way to enumerate the active adapters on a Windows computer with
>> their
>> IPv4 and MAC addresses?
>>
>
> There is an API for that, but I don't remember that right now.

Windows:

uses {$IFDEF MSWINDOWS}Windows, WinSock,{$ENDIF}

type
   pu_long = ^U_long;

var
   HostEnt : PHostEnt;
   InAddr : winsock.TInAddr;
   namebuf : Array[0..255] of ansichar;
   GinitData: TWSAData;

begin
   Result:='';
   WSAStartup($101, GInitData);
   Try
     GetHostName(namebuf,sizeof(namebuf));
     HostEnt := gethostbyname(namebuf);
     if (HostEnt<>Nil) then
      begin
      InAddr.S_addr := u_long(pu_long(HostEnt^.h_addr_list^)^);
      Result := inet_ntoa(InAddr);
      end;
   Finally
     WSACleanup;
   End;
end;

Michael.


More information about the fpc-pascal mailing list