Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GStack.LocalAddress returns empty only with Delphi 10
#5
(11-15-2019, 06:45 PM)kudzu Wrote: This is baffling to me. They both use the same Windows stack. I think you should test this deeper.

They may use the same socket stack (WinSock), but not the same Windows APIs to discover the local IPs.

Indy originally used gethostbyname(gethostname()) to enumerate local IPs, which is a bad way to go! It doesn't always work as expected. Later, Indy switched to getaddrinfo(gethostname()) when getaddrinfo() became available, which is still not the right way to go, but at least it has somewhat more predictable results for a local host name. So, it is possible that such APIs explicitly report 127.0.0.1 as an available IP for a local host name even if no other networks are available.

However, modern versions of Indy now use GetAdaptersInfo()/GetAdaptersAddresses() instead, which is Microsoft's official way to enumerate local network adapters and read their local IPs. If there is no active WiFi or Ethernet network available, then there is simply no local IP for the APIs to report. You could easily detect this condition and fallback to 127.0.0.1 manually.

Also, note that the TIdStack.LocalAddress(es) properties are deprecated. Use the TIdStack.GetLocalAddressList() method instead.

Code:
LList := TIdStackLocalAddressList.Create;
try
  GStack.GetLocalAddressList(LList);
  if List.Count > 0 then
  begin
    // use reported IP(s) as needed...
  end else
  begin
    // use 127.0.0.1/::1 as needed...
  end;
finally
  LList.Free;
end;

Reply


Messages In This Thread
RE: GStack.LocalAddress returns empty only with Delphi XE10 - by rlebeau - 11-16-2019, 07:26 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)