Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get local IP Address

Posted on 1998-07-18
5
Medium Priority
?
609 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Accepted Solution

by:
bome earned 400 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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