private static string GetProxyString(PerConnectionOption optionToCheck) { InternetPerConnectionOption returnOption = GetInternetConnectionOption(optionToCheck); string proxyInfo = Marshal.PtrToStringAuto(returnOption.value.valuePtr); return(proxyInfo); }
public static bool SetInternetProxy(bool enableProxy, string proxyServerString, string proxyBypass, bool autoDetectSettings, bool useAutoConfigureScript, string autoConfigureScript) { InternetPerConnectionOption[] options = new InternetPerConnectionOption[4]; options[0].option = PerConnectionOption.INTERNET_PER_CONNECTION_FLAGS; if (autoDetectSettings) { options[0].value.valueInt |= (int)PerConnectionFlags.PROXY_TYPE_AUTO_DETECT; } if (useAutoConfigureScript) { options[0].value.valueInt |= (int)PerConnectionFlags.PROXY_TYPE_AUTO_PROXY_URL; } if (enableProxy) { options[0].value.valueInt |= (int)PerConnectionFlags.PROXY_TYPE_PROXY; } else { options[0].value.valueInt |= (int)PerConnectionFlags.PROXY_TYPE_DIRECT; } options[1].option = PerConnectionOption.INTERNET_PER_CONNECTION_AUTOCONFIG_URL; options[1].value.valueInt = Marshal.StringToHGlobalAuto(autoConfigureScript).ToInt32(); options[2].option = PerConnectionOption.INTERNET_PER_CONNECTION_PROXY_SERVER; options[2].value.valueInt = Marshal.StringToHGlobalAuto(proxyServerString).ToInt32(); options[3].option = PerConnectionOption.INTERNET_PER_CONNECTION_PROXY_BYPASS; options[3].value.valueInt = Marshal.StringToHGlobalAuto(proxyBypass).ToInt32(); // Marshal.StringToCoTaskMemAuto(proxyBypass); return(CallInternetSetOption(options)); }
public static bool SetAutoConfigURL(string autoConfigScriptURL) { InternetPerConnectionOption[] options = new InternetPerConnectionOption[1]; options[0] = new InternetPerConnectionOption(); options[0].option = PerConnectionOption.INTERNET_PER_CONNECTION_AUTOCONFIG_URL; options[0].value.valueInt = Marshal.StringToHGlobalAuto(autoConfigScriptURL).ToInt32(); return(CallInternetSetOption(options)); }
private static bool CheckProxyFlag(PerConnectionFlags proxyFlagToCheck) { InternetPerConnectionOption returnOption = GetInternetConnectionOption(PerConnectionOption.INTERNET_PER_CONNECTION_FLAGS); // Since this returns an int that contains the value of many flags // we need to AND the value against what we're looking for and // then compare it against our value if it returns true it was set. int andValue = returnOption.value.valueInt & (int)proxyFlagToCheck; return(andValue == (int)proxyFlagToCheck); }
private static InternetPerConnectionOption GetInternetConnectionOption(PerConnectionOption pco) { //Allocate the list and option. InternetPerConnectionOptionList perConnOptList = new InternetPerConnectionOptionList(); InternetPerConnectionOption ico = new InternetPerConnectionOption(); //pin the option structure GCHandle gch = GCHandle.Alloc(ico, GCHandleType.Pinned); //initialize the option for the data we want ico.option = pco; //Initialize the option list for the default connection or LAN. int listSize = Marshal.SizeOf(perConnOptList); perConnOptList.size = listSize; perConnOptList.connection = IntPtr.Zero; perConnOptList.optionCount = 1; perConnOptList.optionError = 0; // figure out sizes & offsets int icoSize = Marshal.SizeOf(ico); int optionTotalSize = icoSize; // alloc enough memory for the option perConnOptList.options = Marshal.AllocCoTaskMem(icoSize); long icoOffset = (long)perConnOptList.options + (long)icoSize; // Make pointer from the structure IntPtr optionListPtr = perConnOptList.options; Marshal.StructureToPtr(ico, optionListPtr, false); //Make the query if (InternetQueryOption( IntPtr.Zero, (int)InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION, //75 ref perConnOptList, ref listSize) == true) { //retrieve the value ico = (InternetPerConnectionOption)Marshal.PtrToStructure(perConnOptList.options, typeof(InternetPerConnectionOption)); } // free the COM memory Marshal.FreeCoTaskMem(perConnOptList.options); //unpin the structs gch.Free(); return(ico); }
private static bool SwitchInternetProxy(PerConnectionFlags proxyFlagToSet, bool enableProxy) { InternetPerConnectionOption currentOption = GetInternetConnectionOption(PerConnectionOption.INTERNET_PER_CONNECTION_FLAGS); if (enableProxy) { //To add this setting do a bitwise OR against the current value //(compare the value bit by bit if either of the values has that bit set leave it set) //this will essentially turn on this portion of the proxy flag. currentOption.value.valueInt |= (int)proxyFlagToSet; } else { //To remove this setting do a bitwise XOR against the current value //(compare the value bit by bit if one and only one of the two has that bit set leave it set) //this will essentially turn off this portion of the proxy flags. currentOption.value.valueInt ^= (int)proxyFlagToSet; } InternetPerConnectionOption[] options = new InternetPerConnectionOption[1]; options[0] = currentOption; return(CallInternetSetOption(options)); }
private static bool SwitchInternetProxy(PerConnectionFlags proxyFlagToSet, bool enableProxy) { InternetPerConnectionOption currentOption = GetInternetConnectionOption(PerConnectionOption.INTERNET_PER_CONNECTION_FLAGS); if (enableProxy) { //To add this setting do a bitwise OR against the current value //(compare the value bit by bit if either of the values has that bit set leave it set) //this will essentially turn on this portion of the proxy flag. currentOption.value.valueInt |= (int)proxyFlagToSet; } else { //To remove this setting do a bitwise XOR against the current value //(compare the value bit by bit if one and only one of the two has that bit set leave it set) //this will essentially turn off this portion of the proxy flags. currentOption.value.valueInt ^= (int)proxyFlagToSet; } InternetPerConnectionOption[] options = new InternetPerConnectionOption[1]; options[0] = currentOption; return CallInternetSetOption(options); }
private static InternetPerConnectionOption GetInternetConnectionOption(PerConnectionOption pco) { //Allocate the list and option. InternetPerConnectionOptionList perConnOptList = new InternetPerConnectionOptionList(); InternetPerConnectionOption ico = new InternetPerConnectionOption(); //pin the option structure GCHandle gch = GCHandle.Alloc(ico, GCHandleType.Pinned); //initialize the option for the data we want ico.option = pco; //Initialize the option list for the default connection or LAN. int listSize = Marshal.SizeOf(perConnOptList); perConnOptList.size = listSize; perConnOptList.connection = IntPtr.Zero; perConnOptList.optionCount = 1; perConnOptList.optionError = 0; // figure out sizes & offsets int icoSize = Marshal.SizeOf(ico); int optionTotalSize = icoSize; // alloc enough memory for the option perConnOptList.options = Marshal.AllocCoTaskMem(icoSize); long icoOffset = (long)perConnOptList.options + (long)icoSize; // Make pointer from the structure IntPtr optionListPtr = perConnOptList.options; Marshal.StructureToPtr(ico, optionListPtr, false); //Make the query if (InternetQueryOption( IntPtr.Zero, (int)InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION, //75 ref perConnOptList, ref listSize) == true) { //retrieve the value ico = (InternetPerConnectionOption)Marshal.PtrToStructure(perConnOptList.options, typeof(InternetPerConnectionOption)); } // free the COM memory Marshal.FreeCoTaskMem(perConnOptList.options); //unpin the structs gch.Free(); return ico; }
private static bool CallInternetSetOption(InternetPerConnectionOption[] options) { InternetPerConnectionOptionList optionList = new InternetPerConnectionOptionList(); optionList.size = Marshal.SizeOf(optionList); optionList.connection = IntPtr.Zero; //Default Connection optionList.optionCount = options.Length; optionList.optionError = 0; optionList.options = VarPtr(options); int optionListSize = Marshal.SizeOf(optionList); IntPtr optionListPtr = Marshal.AllocCoTaskMem(optionListSize); Marshal.StructureToPtr(optionList, optionListPtr, true); bool iReturn; iReturn = InternetSetOption(IntPtr.Zero, (int)InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION, optionListPtr, Marshal.SizeOf(optionList)); if (iReturn == false) { Win32Exception exception = new Win32Exception(Marshal.GetLastWin32Error()); throw exception; } else { iReturn = InternetOptionSettingsChanged(); //if (iReturn == true) //{ // iReturn = InternetOptionRefresh(); //} } // free the COM memory Marshal.FreeCoTaskMem(optionListPtr); return iReturn; }
public static bool SetInternetProxy(bool enableProxy, string proxyServerString, string proxyBypass, bool autoDetectSettings, bool useAutoConfigureScript, string autoConfigureScript) { InternetPerConnectionOption[] options = new InternetPerConnectionOption[4]; options[0].option = PerConnectionOption.INTERNET_PER_CONNECTION_FLAGS; if (autoDetectSettings) { options[0].value.valueInt |= (int)PerConnectionFlags.PROXY_TYPE_AUTO_DETECT; } if (useAutoConfigureScript) { options[0].value.valueInt |= (int)PerConnectionFlags.PROXY_TYPE_AUTO_PROXY_URL; } if (enableProxy) { options[0].value.valueInt |= (int)PerConnectionFlags.PROXY_TYPE_PROXY; } else { options[0].value.valueInt |= (int)PerConnectionFlags.PROXY_TYPE_DIRECT; } options[1].option = PerConnectionOption.INTERNET_PER_CONNECTION_AUTOCONFIG_URL; options[1].value.valueInt = Marshal.StringToHGlobalAuto(autoConfigureScript).ToInt32(); options[2].option = PerConnectionOption.INTERNET_PER_CONNECTION_PROXY_SERVER; options[2].value.valueInt = Marshal.StringToHGlobalAuto(proxyServerString).ToInt32(); options[3].option = PerConnectionOption.INTERNET_PER_CONNECTION_PROXY_BYPASS; options[3].value.valueInt = Marshal.StringToHGlobalAuto(proxyBypass).ToInt32(); // Marshal.StringToCoTaskMemAuto(proxyBypass); return CallInternetSetOption(options); }
public static bool SetAutoConfigURL(string autoConfigScriptURL) { InternetPerConnectionOption[] options = new InternetPerConnectionOption[1]; options[0] = new InternetPerConnectionOption(); options[0].option = PerConnectionOption.INTERNET_PER_CONNECTION_AUTOCONFIG_URL; options[0].value.valueInt = Marshal.StringToHGlobalAuto(autoConfigScriptURL).ToInt32(); return CallInternetSetOption(options); }