Get local IP Address

Posted on 1998-07-18
Last Modified: 2010-04-04
Hi, folks.

I don't wanna - I HAVE 2 know how 2 retrieve the local IP address bound 2 the network interface card via TCP/IP-protocol of the computer I'm running on under Windows 95 as well as under Windows NT. Should b Delphi 3.
This 1 is quite urgent, so I state this question as moderate even though most of u might think it's easy.
I just ain't got the time 2 browse through all the internet sites til I find the answer.
So - please help me or I'll perish...

Ach, not 2 forget:
U can assume there's only 1 card installed and the netware connection is active when calling the function(s). If that is of any help.
Thanxalot in advance.

Black Death.
Question by:BlackDeath
  • 3
  • 2

Accepted Solution

bome earned 100 total points
ID: 1358152
PLace a button and a TEdit on a form.
Add ScktComp and WinSock to the uses clause.

OnClick of Button1:

procedure TForm1.Button1Click(Sender: TObject);
 with TServerSocket.Create(self) do
  with Socket.LookupName('').S_un_b do

This will display the local address in Edit1.

Author Comment

ID: 1358153
Hey, bome!

This 1's excellent! I've just tried it out on either system, Windows95 & WindowsNT. & it worked just fine!
Amazingly simple, isn't it. But that's the old problem - where do u get the information u need right now.

Well - I think I owe my irrelevant life 2 u.
Don't care, u couldn't make use of it anyway.

I rated this A 4 being a quick&correct response. Thanxalot.

Black Death.

Author Comment

ID: 1358154
Aaaargh! F*@$! Problem!

If there's a dial-up network adapter with a tcp/ip-address installed, this function returns this address, not the one of the network card (after about 15 sec. or so) - mostly!

This annoys the s... outta me.

How can i handle this problem?

P.S.: occurs (at least up 2 now) only on win95-machines. Could it b that this problem possibly will occur on nt-machines, 2?
If so, handling this is as important as the 95er problem.

UND: where 2 get documentation on the Windows Network API? (not Winsock.hlp, that's a sucker).

If this always only partly functioning Windows API crap carries on any longer, i'm gonna blow up! My nerves' endings lie open! I'll go berserk! MS go2 hell! No - MS go2 MS!! S.M.F.'s!!!

Enuff of that stuff.

bome - can u help me again? post a mail if u want my 41 points left - i'll post this question with value 40. if u've got a final solution, i'll rate A.

I'd feel good about it if it could return the card's name or node id, 2. Just to b sure it's the card & nuthin else i'm retrieving info from.


Black Death.

Expert Comment

ID: 1358155
Hi Black,
no I don't know a way. The problem is using high level functions which return the logical IP address - when using a dial-up connection, the IP address must be changed so that apps can now connect to the "real" internet. So normally this is intended.

To retrieve the local IP address assigned to the computer, you must go a bit deeper. I don't have the time to explore this, but
in MSDN I found an example, it enumerates the IP addresses. Sorry, it's C, but shouldn't be too difficult to translate to delphi.

   char     Hostname[100];
   HOSTENT *pHostEnt;
   int      nAdapter = 0;

   gethostname( Hostname, sizeof( Hostname ));
   pHostEnt = gethostbyname( Hostname );

   while ( pHostEnt->h_addr_list[nAdapter] )
      // pHostEnt->h_addr_list[nAdapter] -the current address in host order


Author Comment

ID: 1358156
Hi folkz.

I chaecked out that last hint of bome. Browsed the WINSOCK.HLP once more (sic). This is the result 4 everybody who might b interested:

unit TCPIP;


function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;


  SysUtils, Winsock;

  TName = array[0..100] of Char;
  PName = ^TName;

function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;
  HEnt: pHostEnt;
  HName: PName;
  WSAData: TWSAData;
  iCnt: Integer;
  Result := False;    
  if WSAStartup($0101, WSAData) <> 0 then begin
    sWSAError := 'WSAStartup error';
  sHostName := '';
  sIPAddr := '';
  sWSAError := '';
  if GetHostName(HName^, SizeOf(TName)) = 0 then begin
    Result := True;
    sHostName := StrPas(HName^);
    HEnt := GetHostByName(HName^);
    for iCnt := 0 to HEnt^.h_length - 1 do
      sIPAddr := sIPAddr + IntToStr(Ord(HEnt^.h_addr_list^[iCnt])) + '.';
    SetLength(sIPAddr, Length(sIPAddr) - 1);
  else begin
    case WSAGetLastError of
      WSAEFAULT        : sWSAError := 'WSAEFault';
      WSANOTINITIALISED: sWSAError := 'WSANotInitialised';
      WSAENETDOWN      : sWSAError := 'WSAENetDown';
      WSAEINPROGRESS   : sWSAError := 'WSAEInProgress';

The code for the main program would read like this:

procedure TForm1.Button1Click(Sender: TObject);
  sIP, sHost, sErr: string;
  if GetHostIP(sHost, sIP, sErr) then begin
    Edit1.Text := sHost;
    Edit2.Text := sIP;
    MessageDlg(sErr, mtError, [mbOk], 0);

That's it. Bye,

Black Death

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now