Results 1 to 6 of 6

Thread: [DELPHI] - Get IP Address

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

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

  2. #2

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    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;

  3. #3
    New Member
    Join Date
    Aug 2006
    Posts
    2

    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.

  4. #4
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [DELPHI] - Get IP Address

    nothing the code only returns the local ip of the machine. for your internet Ip, use

    www.WhatIsMyIp.com

  5. #5
    New Member
    Join Date
    Aug 2006
    Posts
    2

    Re: [DELPHI] - Get IP Address

    Thank you

  6. #6
    New Member
    Join Date
    Jul 2008
    Posts
    1

    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
  •  



Click Here to Expand Forum to Full Width