Lazarus

Programming => Networking and Web Programming => Topic started by: pcurtis on August 04, 2022, 02:45:15 pm

Title: IP address
Post by: pcurtis on August 04, 2022, 02:45:15 pm
How do I get the IP addresses of all network interfaces on Windows using API. I have tried the attached demo but it doesn't return  IP information.
Title: Re: IP address
Post by: marcov on August 04, 2022, 02:48:54 pm
Here it does. Note that afaik it only returns them for active network interfaces, so if you have something configured without a cable in it, it might not show?
Title: Re: IP address
Post by: Remy Lebeau on August 04, 2022, 07:28:19 pm
How do I get the IP addresses of all network interfaces on Windows using API. I have tried the attached demo but it doesn't return  IP information.

Try something more like this instead:

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.RetrieveLocalAdapterInformation;
  2. var
  3.   pAdapters, pAdapter: PIP_ADAPTER_INFO;
  4.   pIPAddress: PIP_ADDR_STRING;
  5.   BufLen: DWORD;
  6.   Status: DWORD;
  7.   strMAC: String;
  8.   i: Integer;
  9.   strings: TStrings;
  10. begin
  11.   strings := TStringList.Create;
  12.   try
  13.     BufLen := 1024*15;
  14.     pAdapters := AllocMem(BufLen);
  15.  
  16.     try
  17.       repeat
  18.         Status := GetAdaptersInfo(pAdapters, BufLen);
  19.         if (Status <> ERROR_BUFFER_OVERFLOW) then Break;
  20.         ReallocMem(pAdapters, BufLen);
  21.       until False;
  22.  
  23.       if (Status = ERROR_SUCCESS) and (BufLen = 0) then
  24.         Status := ERROR_NO_DATA;
  25.  
  26.       if (Status <> ERROR_SUCCESS) then
  27.       begin
  28.         case Status of
  29.           ERROR_NOT_SUPPORTED:
  30.             strings.Add('GetAdaptersInfo is not supported by the operating ' +
  31.                         'system running on the local computer.');
  32.           ERROR_NO_DATA:
  33.             strings.Add('No network adapter on the local computer.');
  34.         else
  35.           strings.Add('GetAdaptersInfo failed with error #' + IntToStr(Status));
  36.         end;
  37.         Exit;
  38.       end;
  39.  
  40.       pAdapter := pAdapters;
  41.       repeat
  42.         Memo1.Lines.Add('');
  43.         Memo1.Lines.Add('Description: ------------------------' + string(pAdapter^.Description));
  44.         Memo1.Lines.Add('Name: ' + string(pAdapter^.AdapterName));
  45.  
  46.         strMAC := '';
  47.         for I := 0 to pAdapter^.AddressLength - 1 do
  48.           strMAC := strMAC + '-' + IntToHex(pAdapter^.Address[I], 2);
  49.         Delete(strMAC, 1, 1);
  50.         Memo1.Lines.Add('MAC address: ' + strMAC);
  51.  
  52.         pIPAddress := @pAdapter^.IpAddressList;
  53.         repeat
  54.           Memo1.Lines.Add('IP address: ' + string(pIPAddress^.IpAddress.S));
  55.           Memo1.Lines.Add('IP subnet mask: ' + string(pIPAddress^.IpMask.S));
  56.           pIPAddress := pIPAddress^.Next;
  57.         until (pIPAddress = nil);
  58.  
  59.         //pIPAddress := @pAdapter^.GatewayList;
  60.         //repeat
  61.         //  Memo1.Lines.Add('Gateway: ' + string(pIPAddress^.IpAddress.S));
  62.         //  pIPAddress := pIPAddress^.Next;
  63.         //until (pIPAddress = nil);
  64.  
  65.         //Memo1.Lines.Add('DHCP enabled: ' + IntToStr(pAdapter^.DhcpEnabled));
  66.         //Memo1.Lines.Add('DHCP: ' + pAdapter^.DhcpServer.IpAddress.S);
  67.         //Memo1.Lines.Add('Have WINS: ' + BoolToStr(pAdapter^.HaveWins,True));
  68.         //Memo1.Lines.Add('Primary WINS: ' + pAdapter^.PrimaryWinsServer.IpAddress.S);
  69.  
  70.         //pIPAddress := @pAdapter^.SecondaryWinsServer;
  71.         //repeat
  72.         //  Memo1.Lines.Add('Secondary WINS: ' + string(pIPAddress^.IpAddress.S));
  73.         //  pIPAddress := pIPAddress^.Next;
  74.         //until (pIPAddress = nil);
  75.  
  76.         pAdapter := pAdapter^.Next;
  77.       until (pAdapter = nil);
  78.     finally
  79.       Dispose(pAdapters);
  80.     end;
  81.   finally
  82.     strings.Free;
  83.   end;
  84. end;
  85.  

Note that GetAdaptersInfo() has been deprecated since Windows Vista, you should be using GetAdaptersAddresses() (https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersaddresses) instead.
TinyPortal © 2005-2018