Click here to Skip to main content
Click here to Skip to main content

Chameleon - Connection Settings Manager

By , 1 Aug 2007
 

Screenshot - Chameleon.jpg

Introduction

Chameleon is an application that allows users to easily change network configuration on each network adapter from the taskbar. Chameleon offers the benefit of changing network settings according to the user's location such as home or office.

Background

The main idea of this application is changing network settings via WMI, I access WMI by using System.Management namespace. I have two objects named Connection and Profile. Connection is an object which is responsible for storing network connection information. Profile is an object which is responsible for storing profile(home, office etc.) settings such as IP, DNS, gateway etc.

The solution contains four projects. These projects are GUI, BusinessObjects, ControlLib and Helper. And there is also a setup project.

The sample application is running on the taskbar and users can change connection settings after right clicking by selecting connection and profile.

Using the code

There is a method which returns the list of available connections. This list is filled from the VMI provider.

public List<Connection> GetConnections()
{
        List<Connection> insListConnection = new List<Connection>();
        ManagementObjectSearcher m = new ManagementObjectSearcher();
        m.Query = new ObjectQuery
            ("Select * from Win32_NetworkAdapterConfiguration 
                    Where IPEnabled = True");
        foreach (ManagementObject mo in m.Get())
        {
            Connection c = new Connection();
            c.ConnectionId = mo["Index"].ToString();
            c.ConnectionName = mo["Caption"].ToString().Substring
                        (mo["Caption"].ToString().IndexOf("]") + 1).Trim();
            insListConnection.Add(c);
        }
        return insListConnection;
}

After creating an instance and filling the connection and profile object by passing these objects as parameter to the method named SetConnectionProfile, you can change the settings on the selected network adapter.

public void SetConnectionProfile(Connection c, Profile p)
{
    ManagementObjectSearcher m = new ManagementObjectSearcher();
    m.Query = new ObjectQuery("Select *
        from Win32_NetworkAdapterConfiguration Where IPEnabled = True");
    foreach (ManagementObject mo in m.Get())
    {
        if (mo["Index"].ToString()==c.ConnectionId)
        {
            mo.InvokeMethod("EnableStatic", new object[] {new string[]
                        { p.IpAddress }, new string[] { p.SubnetMask } });
                mo.InvokeMethod("SetGateways", new object[] {new string[]
                        { p.DefaultGateway }, new string[] { "1" } });
            mo.InvokeMethod("SetDNSServerSearchOrder", new object[]
                        {new string[] { p.PreferredDns} });
                    if (p.ProxyEnabled)
            {
                RegistryManager.EnableProxy = true;
                RegistryManager.BypassProxyForLocalAddresses = 
                        p.ByPassProxyForLocal;
                RegistryManager.ProxyAddress= p.ProxyAddress + ":" + 
                        p.ProxyPort;
            }
            else
            {
                RegistryManager.EnableProxy = false;
                RegistryManager.ProxyAddress = "";
                RegistryManager.BypassProxyForLocalAddresses =true;
            }
        }
    }
}

And there is another method named SetConnectionAutomatic that resets the connection settings.

public void SetConnectionAutomatic(Connection c)
{
    ManagementObjectSearcher m =new ManagementObjectSearcher();
    m.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration
                                                    Where IPEnabled = True");
    foreach (ManagementObject mo in m.Get())
    {
        if (mo["Index"].ToString() == c.ConnectionId)
        {
            mo.InvokeMethod("EnableDHCP",null);
            mo.InvokeMethod("SetDNSServerSearchOrder",null);
            RegistryManager.EnableProxy = false;
            RegistryManager.ProxyAddress = "";
            RegistryManager.BypassProxyForLocalAddresses  = true;
        }
    }
}

Contact

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tamer Oz
Team Leader
Turkey Turkey
Member
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralWireless adapter not recognized....memberBit Builder27 Jul '12 - 11:14 
A very useful application and good programming example, thanks, I learned a lot of it.
Still one thing is bordering me and I can't find the solution....
My wireless card is not shown in the adapter list. Confused | :confused:
while it is an physical network adapter it seems not IPEnabled when it has no connection???
 
In WMImanager I tried to manipulating the Select query to:
 
"Select * from Win32_NetworkAdapter Where (AdapterType = 'Ethernet 802.3' or AdapterType = 'Wireless') and not Caption Like '%virtual%'"
 
In this case the wireless card is recognized and no Virtual adapters are shown but it is not possible to SET a new profile to the wireless adapter.
 
Any suggestions?
Some help would be appreciated. Thumbs Up | :thumbsup:
 

Bitbuilder.
GeneralPLEASE add an option for setting a custom MAC adressmemberMilepp29 Jul '08 - 10:44 
Hi I like your "Chameleon" project but PLEASE add an option in the profiles for setting a custom MAC adress(and restore) for the NIC, and also option for automatic restart of the NIC when the MAC is changed.
 
I don't have much knowledge in programming so PLEASE do this for me.
I'm tired changing manualy MAC, IP, Subnet Mask, Gateway, DNS.
 
I hope that you will help me.
 
Regards...
GeneralHelal olsun.memberXess26 Jun '08 - 5:18 
Burada biz türklerin ismini görmek gurur verici. Çalismlarinda basarilar dilerim. Bilge Adam da visual basic kursu almistim.
GeneralMore than 1 gateway, dns,ipmemberdsand2 Oct '07 - 22:28 
it ried to set more than 1 address like
new string[] {"192.168.168.1","192.168.200.1")
for ip and dns it works but for the dns it fails....
 
for dns :
new string[] {"192.168.168.1","192.168.200.1")
 

mo.InvokeMethod("SetGateways", new object[] { new string[] { "192.168.168.1","192.168.200.1" }, new string[] { "1,0" } });
 
but the Gateways arent set...
 


GeneralRe: More than 1 gateway, dns,ipmembervarungupta18 Feb '08 - 1:51 
how to set DSN programatically in C or MFC? There shuold be some function.
 

22

GeneralExcellent appmemberGutless18 Aug '07 - 8:41 
Hey, great app.
 
Just one suggestion, it would be great if we could specify "use DHCP" and "use DHCP for DNS addresses" in profiles we make, rather than having to rely on "use automatic".
 
The reason I suggest it is it would be useful to be able to switch to using a proxy with DHCP enabled, or vice-versa.
 
Otherwise, excellent utility.
 
Thanks.
 
Blake
GeneralFINALLY!!!!memberMarko Vnucec IncitoIT8 Aug '07 - 22:38 
I've been dreaming about this all my life.
 
Now I can finally delete that directory with screenshots of network settings Smile | :)
 
Keep up the good work
GeneralGreat appmembergiammin1 Aug '07 - 4:06 
Really nice app!
 
i created some script to avoid rewriteing everytime connection settings but your app is more usefull
 
I ask myself why microsoft didn't included a similar utility or a profiling tool for connections settings...
 
I think that would be nice to save profile in a xml file instead of windows registry so anyone can share/backup/syncronize across pcs.
 
------------------
nowhere...now here!

GeneralRe: Great appmemberTamerOz1 Aug '07 - 4:12 
Thanks for your comment and feedback
 
In next version I'm planning to provide backup and restore actions.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 1 Aug 2007
Article Copyright 2007 by Tamer Oz
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid