Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Change Internet Explorer 7 Proxy Setting without Restarting Internet Explorer

4.60/5 (11 votes)
4 Oct 2007CPOL 5   3.6K  
An article to explain how to change Internet Explorer 7 settings with Registy & WinINet API
TOR Activator Screenshoot

Introduction

This software shows how to change Proxy Settings in Internet Explorer 7 without restarting a new browser.

Background

This code uses WININET API to change InternetOptions and user32.dll to send a system message to inform Windows about the registry settings changes.

Using the Code

RegUtils.cs

This class modifies the Windows Registry settings.

C#
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Win32;

namespace RedTomahawk.TORActivator
{
    internal class RegUtils
    {
        internal enum RegKeyType : int
        {
            CurrentUser = 1,
            LocalMachine = 2
        }

        internal RegUtils() { }

        // byte[]
        internal void GetKeyValue(RegKeyType KeyType, string RegKey,
            string Name, out byte[] Value)
        {
            RegistryKey oRegKey = null;

            switch ((int)KeyType)
            {
                case 1:
                    oRegKey = Registry.CurrentUser;
                    break;
                case 2:
                    oRegKey = Registry.LocalMachine;
                    break;
            }
            oRegKey = oRegKey.OpenSubKey(RegKey);

            Value = (byte[])oRegKey.GetValue(Name);

            oRegKey.Close();
        }

        // byte[]
        internal void SetKeyValue(RegKeyType KeyType, string RegKey,
            string Name, byte[] Value)
        {
            RegistryKey oRegKey = null;

            switch ((int)KeyType)
            {
                case 1:
                    oRegKey = Registry.CurrentUser;
                    break;
                case 2:
                    oRegKey = Registry.LocalMachine;
                    break;
            }

            oRegKey = oRegKey.OpenSubKey(RegKey, true);
            oRegKey.SetValue(Name, Value);
            oRegKey.Close();
            User32Utils.Notify_SettingChange();
            WinINetUtils.Notify_OptionSettingChanges();
        }
    }
}

WinINetUtils.cs

This class is useful to notify Internet Explorer about the changes made in the Windows registry.

C#
using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

namespace RedTomahawk.TORActivator
{
    internal class WinINetUtils
    {
        #region WININET Options
        private const uint INTERNET_PER_CONN_PROXY_SERVER = 2;
        private const uint INTERNET_PER_CONN_PROXY_BYPASS = 3;
        private const uint INTERNET_PER_CONN_FLAGS = 1;

        private const uint INTERNET_OPTION_REFRESH = 37;
        private const uint INTERNET_OPTION_PROXY = 38;
        private const uint INTERNET_OPTION_SETTINGS_CHANGED = 39;
        private const uint INTERNET_OPTION_END_BROWSER_SESSION = 42;
        private const uint INTERNET_OPTION_PER_CONNECTION_OPTION = 75;

        private const uint PROXY_TYPE_DIRECT = 0x1;
        private const uint PROXY_TYPE_PROXY = 0x2;

        private const uint INTERNET_OPEN_TYPE_PROXY = 3;
        #endregion

        #region STRUCT
        struct Value1
        {
            uint dwValue;
            string pszValue;
            FILETIME ftValue;
        };

        [StructLayout(LayoutKind.Sequential)]
        struct INTERNET_PER_CONN_OPTION
        {
            uint dwOption;
            Value1 Value;
        };

        [StructLayout(LayoutKind.Sequential)]
        struct INTERNET_PER_CONN_OPTION_LIST
        {
            uint dwSize;
            [MarshalAs(UnmanagedType.LPStr, SizeConst = 256)]
            string pszConnection;
            uint dwOptionCount;
            uint dwOptionError;
            IntPtr pOptions;

        };

        [StructLayout(LayoutKind.Sequential)]
        struct INTERNET_CONNECTED_INFO
        {
            int dwConnectedState;
            int dwFlags;
        };
        #endregion

        #region Interop
        [DllImport("wininet.dll", EntryPoint = "InternetSetOptionA",
                  CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true)]
        private static extern bool InternetSetOption(IntPtr hInternet, uint dwOption,
                                                     IntPtr pBuffer, int dwReserved);
        #endregion

        internal WinINetUtils(){ }


        internal static void Notify_OptionSettingChanges()
        {
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED,
                    IntPtr.Zero, 0);
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
        }
    }
}

And then the code notifies changes to Windows:

User32Utils.cs

C#
using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

namespace RedTomahawk.TORActivator
{
    internal class User32Utils
    {
        #region USER32 Options
        static IntPtr HWND_BROADCAST = new IntPtr(0xffff);
        static IntPtr WM_SETTINGCHANGE = new IntPtr(0x001A);
        #endregion

        #region STRUCT
        enum SendMessageTimeoutFlags : uint
        {
            SMTO_NORMAL = 0x0000,
            SMTO_BLOCK = 0x0001,
            SMTO_ABORTIFHUNG = 0x0002,
            SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
        }
        #endregion

        #region Interop
        //[DllImport("user32.dll", CharSet = CharSet.Auto)]
        //public static extern int SendMessage
        //(int hWnd, int msg, int wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern IntPtr SendMessageTimeout(IntPtr hWnd,
                                                uint Msg,
                                                UIntPtr wParam,
                                                UIntPtr lParam,
                                                SendMessageTimeoutFlags fuFlags,
                                                uint uTimeout,
                                                out UIntPtr lpdwResult);
        #endregion


        internal User32Utils() { }

        internal static void Notify_SettingChange()
        {
            UIntPtr result;
            SendMessageTimeout(HWND_BROADCAST, (uint)WM_SETTINGCHANGE,
                               UIntPtr.Zero, UIntPtr.Zero,
                                SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out result);
        }
    }
}

The CEngine class calls all the required procedures:

CEngine.cs

C#
using System;
using System.Collections.Generic;
using System.Text;

namespace RedTomahawk.TORActivator
{
    public class CEngine
    {
        RegUtils _regUtils = new RegUtils();

        public CEngine() { }

        public void SetProxyName(string ProxyAddress)
        {
            string szRegKey =
                @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\";
            string szName = "ProxyServer";

            _regUtils.SetKeyValue(RegUtils.RegKeyType.CurrentUser,
                                   szRegKey, szName, ProxyAddress);
        }

        public void EnableProxy(string ProxyAddress)
        {
            string szRegKey =
                 @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\";
            string szName = "ProxyEnable";
            string szValue = string.Empty;

            _regUtils.GetKeyValue(RegUtils.RegKeyType.CurrentUser,
                    szRegKey, "ProxyServer", out szValue);

            if (szValue != ProxyAddress)
            {
                SetProxyName(ProxyAddress);
            }

            _regUtils.SetKeyValue(RegUtils.RegKeyType.CurrentUser, szRegKey, szName, 1);

            byte[] abValue;
            char[] aaValue;
            szRegKey = szRegKey + "Connections";
            _regUtils.GetKeyValue(RegUtils.RegKeyType.CurrentUser, szRegKey,
                                   "DefaultConnectionSettings", out abValue);
            aaValue = new char[abValue.Length];

            // This procedure enable the Proxy for IE7
            for (int i = 0; i < abValue.Length; i++)
            {
                if (i == 8)
                    abValue[i] = 3;
                aaValue[i] = (char)abValue[i];
            }


            _regUtils.SetKeyValue(RegUtils.RegKeyType.CurrentUser, szRegKey,
                                  "DefaultConnectionSettings",
                                  Encoding.ASCII.GetBytes(
                                  (new string(aaValue)).Replace(szValue, ProxyAddress)));
        }

        public void DisableProxy(string ProxyAddress)
        {
            string szRegKey =
                @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\";
            string szName = "ProxyEnable";
            _regUtils.SetKeyValue(RegUtils.RegKeyType.CurrentUser,
                                   szRegKey, szName, 0);

            byte[] abValue;
            szRegKey = szRegKey + "Connections";
            _regUtils.GetKeyValue(RegUtils.RegKeyType.CurrentUser, szRegKey,
                                  "DefaultConnectionSettings", out abValue);

            for (int i = 0; i < abValue.Length; i++)
            {
                //This procedure disable proxy
                if (i == 8)
                {
                    abValue[i] = 0;
                    break;
                }
            }
            _regUtils.SetKeyValue(RegUtils.RegKeyType.CurrentUser,
                         szRegKey, "DefaultConnectionSettings", abValue);
        }

        public string GetProxyName()
        {
            string szRegKey =
                 @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\";
            string szName = "ProxyServer";
            string szProxyAddress = string.Empty;

            _regUtils.GetKeyValue(RegUtils.RegKeyType.CurrentUser, szRegKey,
                                    szName, out szProxyAddress);

            return szProxyAddress;
        }

        public int GetProxyStatus()
        {
            string szRegKey =
                 @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\";
            string szName = "ProxyEnable";
            int iProxyStatus = 0;

            _regUtils.GetKeyValue(RegUtils.RegKeyType.CurrentUser,
                                  szRegKey, szName, out iProxyStatus);

            return iProxyStatus;
        }
    }
}

Points of Interest

This software is useful to those Internet users who need to or want to use different proxies during surfing.

Thanks to this software, you can change a proxy automatically without restarting Internet Explorer. For example, if while surfing, you need to keep your IP anonymity (for example, using TOR), you just need to press a checkbox to change your Internet Explorer proxy settings.

History

  • 4th October, 2007 -- Original version posted

License

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