Get IP through WinSock - Return correct IP - Delphi

0

Good morning,

I am using the method below to return the IP of the computer, but if I have an active VPN on the computer, it is getting the IP of the VPN first and not the machine. I would like to know if it would be possible to control which IP it returns me or another way so I can get the real IP of the computer (from a network card) and not from the VPN and also if it would be possible to get only IPV4 and not IPV6.

Thank you and I await.

Follow the code below:

function GetLocalIP: String;
var
ipwsa:TWSAData;
p:PHostEnt;
s:array[0..128] of char;
c : PAnsiChar;
begin
c := '';
try
  wsastartup(257,ipwsa);
  GetHostName(@s, 128);
  p := GetHostByName(@s);
  c := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
except
  on E: Exception do
  begin
     TVSMLogUtils.GetInstance.LogE(E.Message);
  end;
end;
Result := String(c);
end;
    
asked by anonymous 25.07.2016 / 14:44

1 answer

0

You should use GetAdaptersInfo (), then filter the result and choose the LAN you want.

In this link you will find the example of the code working.

    
25.07.2016 / 15:39