Recent

Author Topic: INDY 10 - TIdIPWatch returns '' (same problem with IDStack)  (Read 2223 times)

kevin.black

  • Full Member
  • ***
  • Posts: 121
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:
Code: Pascal  [Select][+][-]
  1. Uses
  2.   IdBaseComponent,
  3.   IdComponent,
  4.   IdIPWatch,
  5. ...
  6. ...
  7. ...
  8. function getLocalIP: string;
  9. var
  10.    IPW: TIdIPWatch;
  11.  
  12. begin
  13.   IpW := TIdIPWatch.Create(nil);
  14.   try
  15.     if IpW.LocalIP <> '' then
  16.       Result := IpW.LocalIP;
  17.     showmessage('IP: ' + Result);
  18.   finally
  19.     IpW.Free;
  20.   end;
  21. end;
  22.  
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 '':
Code: Pascal  [Select][+][-]
  1. Uses
  2.   IdStack,
  3. ...
  4. ...
  5. function GetLocalIP : String;
  6. begin
  7.   TIdStack.IncUsage;
  8.   try
  9.     Result := GStack.LocalAddress;
  10.     showmessage('IP: ' + Result);
  11.   finally
  12.     TIdStack.DecUsage;
  13.   end;
  14. end;
  15.  

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:
Code: Pascal  [Select][+][-]
  1. function getLocalIP: string;
  2. var
  3.   IPStrings: TStringList;
  4.  
  5. begin
  6.   try
  7.     try
  8.       Result:='';
  9.       IPStrings:=TStringList.Create();
  10.  
  11.       TIdStack.IncUsage;
  12.       GStack.AddLocalAddressesToList(IPStrings);
  13.       Result := IPStrings[0];
  14.       showmessage('IP: ' + Result);
  15.     except
  16.       On E:Exception do
  17.       begin
  18.         Result := '';
  19.        showmessage('IP Error: ' + E.message);
  20.       end;
  21.     end;
  22.   finally
  23.     TIdStack.DecUsage;
  24.   end;
  25.  
  26. end;
  27.  

Kevin
« Last Edit: May 07, 2019, 04:33:14 am by kevin.black »

JimD

  • Jr. Member
  • **
  • Posts: 62
Re: INDY 10 - TIdIPWatch returns '' (same problem with IDStack)
« Reply #1 on: May 11, 2019, 09:55:25 pm »
Your first example using TIdIPWatch works for me.

Using: Windows 10, Lazarus 1.8.4, Indy10.6.2.5494

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: INDY 10 - TIdIPWatch returns '' (same problem with IDStack)
« Reply #2 on: May 11, 2019, 10:30:11 pm »
Your first example using TIdIPWatch works for me.

Using: Windows 10, Lazarus 1.8.4, Indy10.6.2.5494

That is because retrieving the local IPs is well-tested on Windows.  I made sure of that, as I am a Windows developer myself and have put a lot of effort into the retrieval code on Windows.  But the retrieval code for other platforms is less tested, as I don't develop for other platforms, I rely on support from community Delphi/FPC members for that.
« Last Edit: May 11, 2019, 10:38:33 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: INDY 10 - TIdIPWatch returns '' (same problem with IDStack)
« Reply #3 on: May 11, 2019, 11:10:46 pm »
FYI, there is a related discussion on StackOverflow for this same issue:

TIdIPWatch returns blank IP (as does TIdStack)
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

kevin.black

  • Full Member
  • ***
  • Posts: 121
Re: INDY 10 - TIdIPWatch returns '' (same problem with IDStack)
« Reply #4 on: May 17, 2019, 04:02:09 am »
@Remy,

Yes, many thanks, that was me......

I didn't get a response here so posted to stackOverflow. Apologies if it caused additional, unnecessary work, I should have cross-referenced, my bad.

Kevin

 

TinyPortal © 2005-2018