[RESOLVED] discover real card and real ip address of my machine without calling directly WMI
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] discover real card and real ip address of my machine without calling directly WMI

  1. #1
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    [RESOLVED] discover real card and real ip address of my machine without calling directly WMI

    A friend of mine was trying to get valid lan ip from machine.

    Depending on Os and on installed programs (ie: Vmware), he could find more than one apparently valid IP

    We searched for a way to discover the real one without pinging and
    without using WMI.

    We apparently found a "dirty" solution.

    Question is: anybody knows a better way (without wmi) ?
    Code:
    //digged, builded and (here and there) copyed as is from web, without paying attention to style. PLease, do not shot to me for this 
    public static void DisplayIPAddresses() 
        { 
     
            Console.WriteLine("\r\n****************************"); 
            Console.WriteLine("     IPAddresses"); 
            Console.WriteLine("****************************"); 
     
     
            StringBuilder sb = new StringBuilder(); 
            // Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)      
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); 
     
            foreach (NetworkInterface network in networkInterfaces) 
            { 
     
                if (network.OperationalStatus == OperationalStatus.Up ) 
                { 
                    if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue; 
                    if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue; 
                    //GatewayIPAddressInformationCollection GATE = network.GetIPProperties().GatewayAddresses; 
                    // Read the IP configuration for each network    
     
                    IPInterfaceProperties properties = network.GetIPProperties(); 
                    //discard those who do not have a real gateaway  
                    if (properties.GatewayAddresses.Count > 0) 
                    { 
                        bool good = false; 
                        foreach  (GatewayIPAddressInformation gInfo in properties.GatewayAddresses) 
                        { 
                            //not a true gateaway (VmWare Lan) 
                            if (!gInfo.Address.ToString().Equals("0.0.0.0")) 
                            { 
                                sb.AppendLine(" GATEAWAY "+ gInfo.Address.ToString()); 
                                good = true; 
                                break; 
                            } 
                        } 
                        if (!good) 
                        { 
                            continue; 
                        } 
                    } 
                    else { 
                        continue; 
                    } 
                    // Each network interface may have multiple IP addresses        
                    foreach (IPAddressInformation address in properties.UnicastAddresses) 
                    { 
                        // We're only interested in IPv4 addresses for now        
                        if (address.Address.AddressFamily != AddressFamily.InterNetwork) continue; 
     
                        // Ignore loopback addresses (e.g., 127.0.0.1)     
                        if (IPAddress.IsLoopback(address.Address)) continue; 
     
                        if (!address.IsDnsEligible) continue; 
                        if (address.IsTransient) continue;  
     
                        sb.AppendLine(address.Address.ToString() + " (" + network.Name + ") nType:" + network.NetworkInterfaceType.ToString()     ); 
                    } 
                } 
            } 
            Console.WriteLine(sb.ToString()); 
        }
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: discover real card and real ip address of my machine without calling directly WMI

    There's nothing wrong or 'dirty' with that method at all. That's the way you're supposed to obtain all the network interfaces and you do filter out ones which are not suitable for your purposes in a reasonable way. What's not to like?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: discover real card and real ip address of my machine without calling directly WMI

    I hoped there was a more direct way to find out the enabled lan ip filtering out the virtual addresses.
    What I do not like?
    The code I posted is written in a hurry, and in the aim of experiment. It could be written better.
    The idea to look at gateaway infos to discard not really enabled lan ip might seem to work, but it is not a "this is a virtual address" method. This is a "workaround", and so an indirect method to try to find what we where looking for.

    ps
    Thanks for looking at this.
    Last edited by Cimperiali; February 1st, 2012 at 11:10 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: discover real card and real ip address of my machine without calling directly WMI

    this is final solution my colleague opted for. Note it uses the "old" (but in this case well working) System.Runtime.Remoting.Channels.Tcp
    Code:
    using System;
    using System.Collections.Generic;
    
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    
    public class MyClass
    {
    	public static void Main()
    	{
    	   	// Create the server channel.
            TcpChannel tcpChannel = new TcpChannel(22);
    		
            // Register the server channel.
            ChannelServices.RegisterChannel(tcpChannel,false);
    
            // Show the URIs associated with the channel.
            ChannelDataStore data = (ChannelDataStore) tcpChannel.ChannelData;
            foreach (string uri in data.ChannelUris)
            {
    			int len = uri.Length;
                Console.WriteLine("IP: {0}", uri.Substring(6,len-9));
            }
     
     		// Unregister the server channel.
     		ChannelServices.UnregisterChannel(tcpChannel);
    	
    		Console.ReadLine();
    	}
    	
    	#region Helper methods
    
    
    	
    	
    	#endregion
    }
    Last edited by Cimperiali; February 11th, 2012 at 05:07 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center