Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

IP address of both NIC cards

Posted on 2000-05-12
10
Medium Priority
?
312 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
ID: 2806399
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
ID: 2806402
whoops just noticed a bug...getmem statement should be before the try clause.

l8knight
0
 

Author Comment

by:regsoft
ID: 2806848
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:regsoft
ID: 2806856
BTW - Im using D3 on a 98 box.

Tnks for the help so far.
0
 
LVL 1

Accepted Solution

by:
l8knight earned 2000 total points
ID: 2807137
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
 

Author Comment

by:regsoft
ID: 2807311
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
ID: 2822671
Adjusted points from 100 to 500
0
 

Author Comment

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

0
 

Author Comment

by:regsoft
ID: 2822675
Thanks again for the source and help.
0
 
LVL 1

Expert Comment

by:l8knight
ID: 2823835
no problem

regards

l8knight
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

927 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