3

Need to detect local IP address under FireMonkey3. In VCL version, I have been using unit WinSock with methods for it

WSAStartup(...)
gethostname(...)

One limitation: don't need to use any third-party library. I am porting ASTA components to FireMonkey3 platform, and don't want to do dependencies among components.

MD XF
  • 7,860
  • 7
  • 40
  • 71
Oleksandr
  • 319
  • 1
  • 6
  • 15

1 Answers1

9

If you need a cross-platform solution try using Indy and the TIdStack.AddLocalAddressesToList method included in the IdStack unit

Try this sample

var
  AAddresses: TStrings;
begin
  AAddresses := TStringList.Create;
  try
    TIdStack.IncUsage;
    try
      GStack.AddLocalAddressesToList(AAddresses);
    finally
      TIdStack.DecUsage;
    end;
    if AAddresses.Count > 0 then
      //do something
  finally
    AAddresses.Free;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 3
    If you do not have any Indy components instantiated at the time, you will have to manually call `TIdStack.IncUsage()` to ensure the `GStack` pointer is valid before using it, then call `TIdStack.DecUsage()` when you are done with it. – Remy Lebeau Dec 13 '13 at 18:08
  • @RemyLebeau this return just 127.0.0.1 for me on sdk and mobile device but i have access to web via wifi... – peiman F. Feb 10 '16 at 09:37
  • 1
    @peimanF.: are you using Android? `AddLocalAddressesToList()` has not been implemented on Android yet, but should work on iOS. – Remy Lebeau Feb 10 '16 at 16:36
  • @RemyLebeau yes im using android.answer mentioned as `cross-platform`.how ever what is your suggestion for android!? – peiman F. Feb 10 '16 at 20:08
  • 1
    @peimanF. You will likely have to use Embarcadero's Android JNI Bridge framework to access Android's Java APIs directly, specifically either the `NetworkInterface.getNetworkInterfaces()` and `NetworkInterface.getInetAddresses()` methods, or the `WifiManager.getConnectionInfo().getIpAddress()` method. – Remy Lebeau Feb 11 '16 at 01:46