views:

387

answers:

1

I'm using a third party library suite that was stupidly hard-coded to call GetHostEntry.AddressList[0] for a local IP address. It is also not written to support IPv6. I disabled IPv6 on all my network interfaces, but AddressList[0] in my test program (and in the 3rd party libraries) still returns {::1} rather than my first IPv4 address. Is there any Windows setting I can change to fix this so that it behaves like Windows XP (which returns the first IPv4 address)?

Here is the test program I'm using to verify the behavior:

   class Program
   {
    static void Main(string[] args)
    {
     List<string> addresses = ( from address in Dns.GetHostEntry(Dns.GetHostName()).AddressList select address.ToString() ).ToList();
   foreach (string a in addresses)
     {
      Console.WriteLine(a);
     }
     Console.Read();
    }
 }

On a Windows XP machine, the output of the program is 192.168.56.1

On my Windows 7 machine, the output of the program is ::1 192.168.56.2

Any suggestions? Changing the third party library code is not an option available to me.

+1  A: 

Each member of IPHostEntry.AddressList is a IPAddress which has a property AddressFamily which you can use to filter for a specific family.

E.g. IPv4 addresses only:

from address in Dns.GetHostEntry(Dns.GetHostName()).AddressList
where address.AddressFamily == AddressFamily.InterNetwork
select address.ToString()

(Change to AddressFamily.InterNetworkV6 to limit to IPv6 addresses.)

EDIT: Clearly this is a code change, so either (1) filter at the interface to the third party library, (2) get a "better" library, or (3) it is a feature and make your application work with IPv6 (which it is likely to need in the next few years anyway).

Richard
This is definitely the proper way to do what I want, but as you pointed out it does require a code change. Unfortunately, I don't use the third party library directly, there are two other libraries between me and this call.I'm afraid the answer I need (which is to get Windows Server 2008 to behave like Windows XP as regards this call) is unlikely to be possible.
natonic
@natonic: if you could (by updating the question) explain why this is a problem (getting an IPV6 address rather than an IPv4 one), an effective solution might be possible.
Richard