Get the IP and local hostname  

Send By: Q3 Team
Web : www.q3.nu
Email: dlib@q3.nu
Date: 16/09/99

Tip accessed 693 times

 



If you don't want to get complicated the life, usa the component TCP that you have in the palette of
internet components and it will be enough with which you read their properties LocalHostName and
LocalIp

For example:



  	Memo1.Lines.Add(TCP1.LocalHostName);
         Memo1.Lines.Add(TCP1.LocalIp);





If you don't want to use the component TCP since it is a OCX that then gives imore complications when
you install it in another machine... you can ito use this other method to read it directly:

  • Make a form (Form1) an put a TButton (Button1) and a TMemo (Memo1)
  • Include 'Winsock' in the uses part
  • Into OnCreate event of the form put this code:


              procedure TForm1.FormCreate(Sender: TObject);
              var
                wVersionRequested : WORD;
                wsaData : TWSAData;
              begin
               {Start up WinSock}
                wVersionRequested := MAKEWORD(1, 1);
                WSAStartup(wVersionRequested, wsaData);
              end;
    
    



  • Into OnClick event of Button1 put this code:


              procedure TForm1.Button1Click(Sender: TObject);
              var
                p : PHostEnt;
                s : array[0..128] of char;
                p2 : pchar;
              begin
               {Get the computer name}
                GetHostName(@s, 128);
                p := GetHostByName(@s);
                Memo1.Lines.Add(p^.h_Name);
               {Get the IpAddress}
                p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
                Memo1.Lines.Add(p2);
              end;
    
    



  • Finally, into OnDestroy event of Form1 put this code:


              procedure TForm1.FormDestroy(Sender: TObject);
              begin
               {Shut down WinSock}
                WSACleanup;
              end;
    
    




    and it is already...