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?

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.

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;

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;

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

Try Indy. TidHTTP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.