Skip to content

Instantly share code, notes, and snippets.

@DelphiWorlds
Last active June 5, 2017 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DelphiWorlds/0733b6611707c8d5725ee42fd1ae01fd to your computer and use it in GitHub Desktop.
Save DelphiWorlds/0733b6611707c8d5725ee42fd1ae01fd to your computer and use it in GitHub Desktop.
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
Copy link
Author

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