Click here to Skip to main content
12,299,283 members (58,976 online)
Click here to Skip to main content
Add your own
alternative version

Stats

122.9K views
3.2K downloads
31 bookmarked
Posted

Change Internet Explorer 7 Proxy Setting without Restarting Internet Explorer

, 4 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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.

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

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

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)

Share

About the Author

Manuel C
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionWant to also set and get the Automatically Detect Settings Pin
rsnyder4-Oct-13 10:09
memberrsnyder4-Oct-13 10:09 
QuestionTOR Activator GUI Pin
SysEng697-May-13 11:39
professionalSysEng697-May-13 11:39 
AnswerRe: TOR Activator GUI Pin
rsnyder4-Oct-13 9:55
memberrsnyder4-Oct-13 9:55 
AnswerRe: TOR Activator GUI Pin
rsnyder4-Oct-13 9:56
memberrsnyder4-Oct-13 9:56 
QuestionCan you send me the whole code include where to start and how... Pin
Eyal9194-Aug-10 11:23
memberEyal9194-Aug-10 11:23 
GeneralNo Form is included in the code Pin
Member 420060423-Jun-09 7:37
memberMember 420060423-Jun-09 7:37 
GeneralLocalMachine vs. CurrentUser Pin
Ossan Dust25-Sep-08 23:47
memberOssan Dust25-Sep-08 23:47 
GeneralIE6 Pin
creatorulcom1-Sep-08 0:24
membercreatorulcom1-Sep-08 0:24 
GeneralPls UPload the Whole project . Pin
kothanzaw6-Dec-07 16:38
memberkothanzaw6-Dec-07 16:38 
GeneralProblem occured - registry damaged [modified] Pin
schleicher@vkn17-Nov-07 2:04
memberschleicher@vkn17-Nov-07 2:04 
GeneralRe: Problem occured - registry damaged Pin
Manuel C17-Nov-07 2:57
memberManuel C17-Nov-07 2:57 
GeneralRe: Problem occured - registry damaged [modified] Pin
schleicher@vkn17-Nov-07 9:08
memberschleicher@vkn17-Nov-07 9:08 
GeneralIE7 Pin
jc.net.com4-Oct-07 11:22
memberjc.net.com4-Oct-07 11:22 
GeneralRe: IE7 Pin
nous.mc4-Oct-07 11:57
membernous.mc4-Oct-07 11:57 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 4 Oct 2007
Article Copyright 2007 by Manuel C
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid