PDA

Click to See Complete Forum and Search --> : [DELPHI] - Get IP Address


Madboy
Jun 4th, 2005, 12:36 PM
uses
Winsock;

function getIPs: Tstrings;
type
TaPInAddr = array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of Char;
I: Integer;
GInitData: TWSAData;
begin
WSAStartup($101, GInitData);
Result := TstringList.Create;
Result.Clear;
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(buffer);
if phe = nil then Exit;
pPtr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pPtr^[I] <> nil do
begin
Result.Add(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines := GetIps;
end;

Madboy
Jun 7th, 2005, 11:17 AM
There is an alternative way to do the above, this method (found from delphi.about.com) shows how to retrieve IP Address and Host name.

You may visit the exact link here: http://delphi.about.com/od/networking/l/aa103100a.htm

or view the below:

uses Winsock;

function GetIPFromHost
(var HostName, IPaddr, WSAErr: string): Boolean;
type
Name = array[0..100] of Char;
PName = ^Name;
var
HEnt: pHostEnt;
HName: PName;
WSAData: TWSAData;
i: Integer;
begin
Result := False;
if WSAStartup($0101, WSAData) <> 0 then begin
WSAErr := 'Winsock is not responding."';
Exit;
end;
IPaddr := '';
New(HName);
if GetHostName(HName^, SizeOf(Name)) = 0 then
begin
HostName := StrPas(HName^);
HEnt := GetHostByName(HName^);
for i := 0 to HEnt^.h_length - 1 do
IPaddr :=
Concat(IPaddr,
IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.');
SetLength(IPaddr, Length(IPaddr) - 1);
Result := True;
end
else begin
case WSAGetLastError of
WSANOTINITIALISED:WSAErr:='WSANotInitialised';
WSAENETDOWN :WSAErr:='WSAENetDown';
WSAEINPROGRESS :WSAErr:='WSAEInProgress';
end;
end;
Dispose(HName);
WSACleanup;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Host, IP, Err: string;
begin
if GetIPFromHost(Host, IP, Err) then begin
Edit1.Text := Host;
Edit2.Text := IP;
end
else
MessageDlg(Err, mtError, [mbOk], 0);
end;

Philips10
Aug 10th, 2006, 03:03 PM
I get the IP of the computer I am working on ONLY
(2 computer with a Router and a broadband internet).

What am I doing wrong.

Pino
Aug 27th, 2006, 01:09 PM
nothing the code only returns the local ip of the machine. for your internet Ip, use

www.WhatIsMyIp.com

Philips10
Aug 27th, 2006, 01:36 PM
Thank you

set4s
Jul 14th, 2008, 04:52 AM
can anyone tell me, how to get the outside pc ip, not the local one? i think it should be something with getting output from a website that provides showing the ip and then exctracting an ip address from the http response... can anyone give a bit of advice ? :]