Hi,
I'm looking at simple solutions to get the local IP Address and certainly INDY seems simplest. That would be, of course, if it worked.
This is on a parallels MacOS VM running Mojave running on a 2015 MBP. I'm using Lazarus 2.0.0RC3 and the code is in a DYLIB. I need the IP address of the local machine to get it's MACAddress.
I'm using TIdWatch, which should simply return the Local IP address (which is also used in Current IP Address function(. I like both solutions, IdStack below, because the code is very simple, but, of course, no matter how simple not of benefit if the code does not work:
Uses
IdBaseComponent,
IdComponent,
IdIPWatch,
...
...
...
function getLocalIP: string;
var
IPW: TIdIPWatch;
begin
IpW := TIdIPWatch.Create(nil);
try
if IpW.LocalIP <> '' then
Result := IpW.LocalIP;
showmessage('IP: ' + Result);
finally
IpW.Free;
end;
end;
So basically the call to the function is returning '' (the local IP address is 192.168.1.25).
I tried setting active to True (IpW.Active := True;), but that just kills the application.
Any ideas what I'm doing wrong or what I might need to setup????
Likewise I have tried using IdStack and get the same issue, ie. it Returns '':
Uses
IdStack,
...
...
function GetLocalIP : String;
begin
TIdStack.IncUsage;
try
Result := GStack.LocalAddress;
showmessage('IP: ' + Result);
finally
TIdStack.DecUsage;
end;
end;
Again, this code was from the internet, but I have looked up both IdStack and TIdIPWatch and according to my readings, should return the local IP address as stated on the website.
Again, anyone got any ideas what I might be doing wrong?
EDIT:I also tried this modified code, but it returns an index out of bounds error, clearly nothing is being returned to the stringiest by the AddLocalAddressesToList function:
function getLocalIP: string;
var
IPStrings: TStringList;
begin
try
try
Result:='';
IPStrings:=TStringList.Create();
TIdStack.IncUsage;
GStack.AddLocalAddressesToList(IPStrings);
Result := IPStrings[0];
showmessage('IP: ' + Result);
except
On E:Exception do
begin
Result := '';
showmessage('IP Error: ' + E.message);
end;
end;
finally
TIdStack.DecUsage;
end;
end;
Kevin