Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Substitute for TIdStackVCLPosix.GetLocalAddressList
interface
uses
IdStack;
procedure GetLocalAddressList(const AAddresses: TIdStackLocalAddressList);
implementation
uses
System.SysUtils,
Androidapi.JNI.Java.Net, Androidapi.JNI.JavaTypes, Androidapi.Helpers, Androidapi.JNIBridge,
IdGlobal;
procedure GetLocalAddressList(const AAddresses: TIdStackLocalAddressList);
var
LInterfaces, LAddresses: JEnumeration;
LInterface: JNetworkInterface;
LAddress: JInetAddress;
LInet4Address: JInet4Address;
LInet6Address: JInet6Address;
LName, LHostAddress: string;
begin
AAddresses.Clear;
LInterfaces := TJNetworkInterface.JavaClass.getNetworkInterfaces;
while LInterfaces.hasMoreElements do
begin
LInterface := TJNetworkInterface.Wrap(JObjectToID(LInterfaces.nextElement));
LAddresses := LInterface.getInetAddresses;
while LAddresses.hasMoreElements do
begin
LAddress := TJInetAddress.Wrap(JObjectToID(LAddresses.nextElement));
if LAddress.isLoopbackAddress then
Continue;
// Hack until I can find out how to check properly
LName := JStringToString(LAddress.getClass.getName);
LHostAddress := JStringToString(LAddress.getHostAddress);
// Trim excess stuff
if LHostAddress.IndexOf('%') > -1 then
LHostAddress := LHostAddress.Substring(0, LHostAddress.IndexOf('%'));
if LName.Contains('Inet4Address') then
TIdStackLocalAddressIPv4.Create(AAddresses, LHostAddress, '')
else if LName.Contains('Inet6Address') then
TIdStackLocalAddressIPv6.Create(AAddresses, LHostAddress);
end;
end;
end;
@DelphiWorlds

This comment has been minimized.

Copy link
Owner Author

@DelphiWorlds DelphiWorlds commented Jun 5, 2017

TIdStackVCLPosix.GetLocalAddressList fails on Delphi 10.2 Tokyo, so this is a substitute routine. I'm comparing Java classnames to tell whether it's an IPv4 or IPv6 address instead of comparing interfaces, which I'm yet to work out how to do (Supports and QueryInterface do not seem to work, or are not appropriate).

Use at your own risk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment