1,105,402 Community Members

How would I find a system's IP address in Delphi?

Member Avatar
EnderX
Posting Shark
999 posts since Aug 2006
Reputation Points: 352 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 22 [?]
 
0
 

I'm working on a Delphi program designed for use with an Access database. In addition to the onscreen output, however, my boss wants me to provide PHP reports for it as well. We have PHP installed on our servers, so that's good, but the database in question is installed on the client box in this scenario; it'll be on the same box as my Delphi system is running on. In order to get the PHP page to read it, at the very least I'll need to be able to have the Delphi program pass the IP address of the system it's running on to the PHP page it's calling. How would I go about using Delphi to find this information?

Member Avatar
ExplainThat
Junior Poster in Training
69 posts since Sep 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm working on a Delphi program designed for use with an Access database. In addition to the onscreen output, however, my boss wants me to provide PHP reports for it as well. We have PHP installed on our servers, so that's good, but the database in question is installed on the client box in this scenario; it'll be on the same box as my Delphi system is running on. In order to get the PHP page to read it, at the very least I'll need to be able to have the Delphi program pass the IP address of the system it's running on to the PHP page it's calling. How would I go about using Delphi to find this information?

EnderX, I don't think there is any way to do what you want using Delphi on its on. However, rope in WinSock and you have a very simple solution using the various WinSock API functions. The attachment to this message contains all the code you need. I am storing your IP address in a global variable called _LocalIP - better than having to call WinSock functions over and over again each time you need to use your IP. If you like you can change that and turn it into a function that returns your IP as the result.


I cobbled this together using some code I have for other needs and it has not been tested. However, there should be no major bugs. If necessary check out the MSDN documentation on this API if necessary. Shouldn't be hard to find using Google.

Member Avatar
Member 784101
Newbie Poster
2 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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^ <> nil do
begin
Result.Add(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetIps.text);
end;

Member Avatar
Wolfgan
Junior Poster
101 posts since Jun 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 1 [?]
 
0
 

for Delphi 2009

uses
 Winsock;

const
  WINSOCK_VERSION = $0101;

function GetIPAddress(name: AnsiString): string;
const WSVer = $101;
var
  wsaData: TWSAData;
  P: PHostEnt;
begin
  Result := '';
  if WSAStartup(WSVer, wsaData) = 0 then begin
   begin
      P := GetHostByName(PAnsiChar(name));
      if P <> nil then Result := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
    end;
    WSACleanup;
  end;
end;

for Delphi 7

uses
 Winsock;

const
  WINSOCK_VERSION = $0101;

function GetIPAddress(name: String): string;
const WSVer = $101;
var
  wsaData: TWSAData;
  P: PHostEnt;
begin
  Result := '';
  if WSAStartup(WSVer, wsaData) = 0 then begin
   begin
      P := GetHostByName(PChar(name));
      if P <> nil then Result := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
    end;
    WSACleanup;
  end;
end;
Member Avatar
Member 784101
Newbie Poster
2 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks a lot Wolfgan
i use the Delphi 2009 code and its working very will
i want to ask you if i want to get the IP Address beyond the router ??

Member Avatar
Wolfgan
Junior Poster
101 posts since Jun 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 1 [?]
 
0
 

Try Indy. TidHTTP

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article