-
Jun 4th, 2005, 11:36 AM
#1
Thread Starter
Supreme User
[DELPHI] - Get IP Address
Code:
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;
-
Jun 7th, 2005, 10:17 AM
#2
Thread Starter
Supreme User
Re: [DELPHI] - Get IP Address
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:
Code:
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;
-
Aug 10th, 2006, 02:03 PM
#3
New Member
Re: [DELPHI] - Get IP Address
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.
-
Aug 27th, 2006, 12:09 PM
#4
Re: [DELPHI] - Get IP Address
nothing the code only returns the local ip of the machine. for your internet Ip, use
www.WhatIsMyIp.com
-
Aug 27th, 2006, 12:36 PM
#5
New Member
Re: [DELPHI] - Get IP Address
-
Jul 14th, 2008, 03:52 AM
#6
New Member
Re: [DELPHI] - Get IP Address
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 ? :]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|