1.11M Members

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

 
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?

 
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.

 
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;

 
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;
 
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 ??

 
0
 

Try Indy. TidHTTP

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

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