2

Is there any GetHostByName equivalent for firemonkey, which works for Desktop Win32/Win64/Mac?

2 Answers2

2

Look into System.Net.Socket and you find TIPAddress.LookupName which has the implementation you are looking for. On Posix it calls gethostbyname from Posix.NetDB.

Stefan Glienke
  • 20,860
  • 2
  • 48
  • 102
  • 2
    FYI, there has been a bug in `TIPAddress.LookupName`: https://quality.embarcadero.com/browse/RSP-12829 It affects *Delphi 10 Seattle* and has been fixed in *Delphi 10.1 Seattle*. – René Hoffmann Apr 03 '17 at 12:36
  • 1
    @RenéHoffmann this is marked as resolved but actually still not fixed as of Delphi 10.2 Tokyo – EugeneK Apr 04 '17 at 22:03
2

If you use Indy, its cross-platform TIdStack interface has public ResolveHost() and HostByName() methods (where ResolveHost() calls HostByName() for input that is not already an IP address). HostByName() internally uses either getaddrinfo() or gethostbyname(), depending on platform.

The caveat, however, is that such platform functions return a list of IP addresses that can have more than one IP in it, but ResolveHost()/HostByName() currently only return the first IP in the list. If you needed the full list, you would have to drop down to the platform layer and call the socket functions directly.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • thank you, I will definitely take a look at TIdStack, it seems to make things easier than my current solution. –  Apr 05 '17 at 01:33