Solved

IP address of both NIC cards

Posted on 2000-05-12
10
293 Views
Last Modified: 2010-04-04
Hello,

How do I get the IP address of the NIC that I use to connect to the internet w/?

I used that local ip function - and that returns the IP address of the machine as seen on my local LAN.

I need to get the ip that Im connected from.

Please help!!!
0
Comment
Question by:regsoft
  • 6
  • 4
10 Comments
 
LVL 1

Expert Comment

by:l8knight
Comment Utility
I just happen to working on an app that does just that...here's the code. It inserts all IP addresses into a combobox so you can select whichever one you want. I'm using the latest header conversion of winsock 2, but it should work with the conversion that comes with Delphi, if it doesn't let me know and I will send you the link.

function TForm1.GetInterfaceList: Boolean;
var SList: LPSOCKET_ADDRESS_LIST;
    s: TSocket;
    ret,i: Integer;
    BytesRet: DWORD;
begin
  s := Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
  if s = INVALID_SOCKET then
  begin
    MessageDlg('Socket Failed!', mtError, [mbOK],0);
    Result := False;
  end
  else
  begin
    SList := Nil;
    BytesRet := 0;
    try
      GetMem(SList, 2048);
      ret := WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, Nil, 0, SList, 2048, @BytesRet, Nil, Nil);
      if ret = SOCKET_ERROR then
      begin
        MessageDlg('WSAIoctl Failed (SIO_ADDRESS_LIST_QUERY)!', mtError, [mbOK],0);
        Result := False;
      end
      else
      begin
        for i := 0 to SList^.IAddressCount-1 do
          ComboBox1.Items.Add( inet_ntoa(TInAddr(SList^.Address[i].lpSockAddr.sin_addr)));
        ComboBox1.ItemIndex := 0;
        Result := True;
      end;
    finally
      FreeMem(SList, 2048);
    end;
    CloseSocket(s);
  end;
end;

regards

l8knight
0
 
LVL 1

Expert Comment

by:l8knight
Comment Utility
whoops just noticed a bug...getmem statement should be before the try clause.

l8knight
0
 

Author Comment

by:regsoft
Comment Utility
I added winsock to my uses clause - but keep getting an error on this line :
var SList: LPSOCKET_ADDRESS_LIST;

Undeclared identifier : 'LPSOCKET_ADDRESS_LIST'

Am I missing something in the uses clause?
0
 

Author Comment

by:regsoft
Comment Utility
BTW - Im using D3 on a 98 box.

Tnks for the help so far.
0
 
LVL 1

Accepted Solution

by:
l8knight earned 500 total points
Comment Utility
Hi regsoft,

      SOCKET_ADDRESS_LIST = packed record
            iAddressCount : Integer;
            Address       : Array [0..0] of SOCKET_ADDRESS;
      end;
      LPSOCKET_ADDRESS_LIST = ^SOCKET_ADDRESS_LIST;

It's declared in the later version of winsock2, so it would probably would be a good idea to download it http://community.borland.com/homepages/dsp/ftp/d20free/winsock2.zip

feel free to ask if you need any more help.

regards

l8knight
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:regsoft
Comment Utility
Thanks for the help - but I still cant get it to work.

I get it to complile now but when I run it I get a socket failed! error.

Make you a deal - if you can send me source (email it robv@regsoft.com)
that works - Ill up the points to 500.

Thanks again.

-Rob
0
 

Author Comment

by:regsoft
Comment Utility
Adjusted points from 100 to 500
0
 

Author Comment

by:regsoft
Comment Utility
That did it - thanks
Sorry for the delay in getting back to you.

0
 

Author Comment

by:regsoft
Comment Utility
Thanks again for the source and help.
0
 
LVL 1

Expert Comment

by:l8knight
Comment Utility
no problem

regards

l8knight
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

10 Experts available now in Live!

Get 1:1 Help Now