Link to home
Start Free TrialLog in
Avatar of BlackDeath
BlackDeath

asked on

Get local IP Address

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.
ASKER CERTIFIED SOLUTION
Avatar of bome
bome

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlackDeath
BlackDeath

ASKER

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.
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.
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
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