I have 2 NICs installed. How can I get the IP of the both or a specific NIC ?
wxIPV4address::IPAddress tells me the IP only of the first NIC.
How to get the IP
Have you looked into the wxIP4Address source and checked which of the two NIC's is chosen as the one that is returned? Maybe you can either tweak it or add the missing NIC.
Regards,
- Jorgen
Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
-
- Earned a small fee
- Posts: 16
- Joined: Tue Feb 08, 2005 3:52 am
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: How to get the IP
Good question. Isn't the one IPAddress returns determined by the operating system - i.e. the user - though?monteaup wrote:I have 2 NICs installed. How can I get the IP of the both or a specific NIC ?
wxIPV4address::IPAddress tells me the IP only of the first NIC.
[Mostly retired moderator, still check in to clean up some stuff]
-
- Filthy Rich wx Solver
- Posts: 236
- Joined: Sat Sep 04, 2004 1:49 pm
- Location: Ohio, USA
- Contact:
The following code will populate a wxArrayString and an array of wxUint32s with both the string and long forms of the computer's assigned IP addresses (up to 12). If you need more than 12, just adjust the array and the loop. This code should work on all major platforms.
One other note: I have a special class for conversions, so you won't have my CConversions:: method used here, but you can substitute your favorite way of converting an int to a string.
I have seen this question many times so I hope that this code will help many people.
One other note: I have a special class for conversions, so you won't have my CConversions:: method used here, but you can substitute your favorite way of converting an int to a string.
Code: Select all
char szHostName[128];
wxString str;
struct hostent * pHost;
int i = 0, j = 0;
wxArrayString StrAddresses;
wxUint32 longAddresses[12];
if(gethostname(szHostName, 128) == 0 )
{
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL && i < 12; i++ )
{
for( j = 0; j < pHost->h_length; j++ )
{
if( j > 0 )
{
str += _T(".");
}
str += CConversions::UnsignedLongTowxString((unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
}
// Do not store an address if it is localhost
if (str != _T("127.0.0.1"))
{
// Now that we have an ip address, store it
StrAddresses.Add(str);
}
str.Clear();
}
}