Solved

Get local IP Address

Posted on 1998-07-18
5
597 Views
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.
0
Comment
Question by:BlackDeath
  • 3
  • 2
5 Comments
 

Accepted Solution

by:
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);
begin
 with TServerSocket.Create(self) do
 try
  with Socket.LookupName('').S_un_b do
   Edit1.Text:=IntToStr(Ord(s_b1))+'.'
              +IntToStr(Ord(s_b2))+'.'
              +IntToStr(Ord(s_b3))+'.'
              +IntToStr(Ord(s_b4))+'.';
 finally
  Free;
 end;
end;

This will display the local address in Edit1.
bome
0
 
LVL 1

Author Comment

by:BlackDeath
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.
0
 
LVL 1

Author Comment

by:BlackDeath
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 0.0.0.0!

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.

Bye,

Black Death.
0
 

Expert Comment

by:bome
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
      nAdapter++;
   }

bome
0
 
LVL 1

Author Comment

by:BlackDeath
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;

interface

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

implementation

uses
  SysUtils, Winsock;

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

function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;
var
  HEnt: pHostEnt;
  HName: PName;
  WSAData: TWSAData;
  iCnt: Integer;
begin
  Result := False;    
  if WSAStartup($0101, WSAData) <> 0 then begin
    sWSAError := 'WSAStartup error';
    Exit;
  end;
  sHostName := '';
  sIPAddr := '';
  sWSAError := '';
  New(HName);
  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);
  end
  else begin
    case WSAGetLastError of
      WSAEFAULT        : sWSAError := 'WSAEFault';
      WSANOTINITIALISED: sWSAError := 'WSANotInitialised';
      WSAENETDOWN      : sWSAError := 'WSAENetDown';
      WSAEINPROGRESS   : sWSAError := 'WSAEInProgress';
    end;
  end;
  Dispose(HName);
  WSACleanup;
end;
   
end.

The code for the main program would read like this:

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

That's it. Bye,

Black Death
0

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