Solved

IP address of both NIC cards

Posted on 2000-05-12
10
295 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error E2158 compiling with Delphi XE10 Seattle 2 100
Graphics32 under Delphi 10.1 Berlin 2 67
Multi-layered image in FireMonkey 9 34
Wincontrol not (correctly) drawn 15 38
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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