How do I Get My Own IP Address?

If you ever needed your own IP address then here it is the code. Please note that in certain situations you may have more than 1 IP address.

uses
  Winsock;
...
function ReadIPs: TStrings;
type
  TaPInAddr = array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  Buffer: array[0..63] of Char;
  iI: Integer;
  PPtr: PaPInAddr;
  pHE: PHostEnt;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  Result := TStringList.Create;
  Result.Clear;
  GetHostName(Buffer, SizeOf(Buffer));
  pHEe := GetHostByName(buffer);
  if pHE = nil then
    Exit;
  PPtr := PaPInAddr(pHE^.H_Addr_List);
  iI := 0;
  while pPtr^[iI] <> nil do begin
    Result.Add(Inet_NToA(PPtr^[iI]^));
    Inc(iI);
  end;
  WSACleanup;
end;
...
Memo1.Lines := ReadIPs;

Tags: , , , , , , , , , , , , , , , , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login