Solved

Get local IP Address

Posted on 1998-07-18
5
596 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

12 Experts available now in Live!

Get 1:1 Help Now