Pick up IP through Winsock - Return correct IP - Delphi

Asked

Viewed 1,477 times

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 first getting the IP of the VPN and not the machine. I wonder if it would be possible to control which IP it returns me or another way for me to 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 wait.

Follows code:

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;

1 answer

0


You must use Getadaptersinfo(), then select the result and choose the LAN you want.

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

  • Thanks worked out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.