更改 Internet Explorer 7 代理设置而不重启 Internet Explorer
一篇解释如何通过注册表和 WinINet API 更改 Internet Explorer 7 设置的文章。

引言
此软件演示了如何在不重启浏览器的情况下更改 Internet Explorer 7 中的代理设置。
背景
此代码使用 WININET API 更改 InternetOptions,并使用 user32.dll 向 Windows 发送系统消息,以告知 Windows 注册表设置已更改。
Using the Code
RegUtils.cs
此类修改 Windows 注册表设置。
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
此类用于通知 Internet Explorer 注册表中进行的更改。
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);
}
}
}
然后,代码会通知 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);
}
}
}
CEngine
类调用所有必需的程序。
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;
}
}
}
关注点
此软件对那些需要在浏览网页时使用不同代理的用户很有用。
借助此软件,您可以自动更改代理,而无需重启 Internet Explorer。例如,如果在浏览网页时,您需要保持 IP 匿名性(例如,使用 TOR),您只需按一下复选框即可更改 Internet Explorer 代理设置。
历史
- 2007 年 10 月 4 日 -- 发布原始版本