From efefe274a6d4adf532f860e09668fad7ee6143e9 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 02:27:44 +0800 Subject: [PATCH] handle no privilege error Popup an error window when don't have enough privilege to change Registry. --- shadowsocks-csharp/Controller/SystemProxy.cs | 49 ++++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/shadowsocks-csharp/Controller/SystemProxy.cs b/shadowsocks-csharp/Controller/SystemProxy.cs index 803a326a..884c7260 100755 --- a/shadowsocks-csharp/Controller/SystemProxy.cs +++ b/shadowsocks-csharp/Controller/SystemProxy.cs @@ -1,4 +1,5 @@ -using Microsoft.Win32; +using System.Windows.Forms; +using Microsoft.Win32; using System; using System.Collections.Generic; using System.Runtime.InteropServices; @@ -13,32 +14,52 @@ namespace Shadowsocks.Controller public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); public const int INTERNET_OPTION_SETTINGS_CHANGED = 39; public const int INTERNET_OPTION_REFRESH = 37; - static bool settingsReturn, refreshReturn; + static bool _settingsReturn, _refreshReturn; public static void NotifyIE() { // These lines implement the Interface in the beginning of program // They cause the OS to refresh the settings, causing IP to realy update - settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); - refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); + _settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); + _refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); } public static void Enable() { - RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); - registry.SetValue("ProxyEnable", 0); - registry.SetValue("ProxyServer", ""); - registry.SetValue("AutoConfigURL", "http://127.0.0.1:8090/pac?t=" + GetTimestamp(DateTime.Now)); - SystemProxy.NotifyIE(); + try + { + RegistryKey registry = + Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", + true); + registry.SetValue("ProxyEnable", 0); + registry.SetValue("ProxyServer", ""); + registry.SetValue("AutoConfigURL", "http://127.0.0.1:8090/pac?t=" + GetTimestamp(DateTime.Now)); + SystemProxy.NotifyIE(); + } + catch (Exception) + { + MessageBox.Show("can not change registry!"); + throw; + } } public static void Disable() { - RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); - registry.SetValue("ProxyEnable", 0); - registry.SetValue("ProxyServer", ""); - registry.SetValue("AutoConfigURL", ""); - SystemProxy.NotifyIE(); + try + { + RegistryKey registry = + Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", + true); + registry.SetValue("ProxyEnable", 0); + registry.SetValue("ProxyServer", ""); + registry.SetValue("AutoConfigURL", ""); + SystemProxy.NotifyIE(); + } + catch (Exception) + { + MessageBox.Show("can not change registry!"); + throw; + } } private static String GetTimestamp(DateTime value)