public static bool VarsayılanProxy() { IntPtr hInternet = InternetOpen(applicationName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); INTERNET_PER_CONN_OPTION_LIST request = SistemProxy(); int size = Marshal.SizeOf(request); IntPtr intptrStruct = Marshal.AllocCoTaskMem(size); Marshal.StructureToPtr(request, intptrStruct, true); bool bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); Marshal.FreeCoTaskMem(request.pOptions); Marshal.FreeCoTaskMem(intptrStruct); if (!bReturn) { try { throw new ApplicationException("Proxy Ayarlarken Hata Oluştu!"); } catch { } } InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); InternetCloseHandle(hInternet); return(bReturn); }
public static bool ResetProxy(ref int Win32Error) { var list = new INTERNET_PER_CONN_OPTION_LIST(); var option = new INTERNET_PER_CONN_OPTION[1]; IntPtr pOptions = Marshal.AllocHGlobal(Marshal.SizeOf(option[0])); IntPtr pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(list)); bool iRes = false; try { option[0].dwOption = PerConnOption.INTERNET_PER_CONN_FLAGS; option[0].Value.dwValue = (int)(PerConnFlag.PROXY_TYPE_DIRECT); Marshal.StructureToPtr(option[0], pOptions, true); list.dwSize = Marshal.SizeOf(list); list.pszConnection = null; list.dwOptionCount = 1; list.pOptions = pOptions; Marshal.StructureToPtr(list, pBuffer, false); iRes = InternetSetOption(IntPtr.Zero, OptionFlag.INTERNET_OPTION_PER_CONNECTION_OPTION, pBuffer, Marshal.SizeOf(list)); Win32Error = Marshal.GetLastWin32Error(); } finally { Marshal.FreeHGlobal(pOptions); Marshal.FreeHGlobal(pBuffer); } return(iRes); }
/// <summary> /// Restore the options for LAN connection. /// </summary> /// <param name="request"></param> /// <returns></returns> public static bool RestoreSystemProxy() { IntPtr hInternet = InternetOpen(applicationName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); INTERNET_PER_CONN_OPTION_LIST request = GetSystemProxy(); int size = Marshal.SizeOf(request); // Allocate memory. IntPtr intptrStruct = Marshal.AllocCoTaskMem(size); // Convert structure to IntPtr Marshal.StructureToPtr(request, intptrStruct, true); // Set internet options. bool bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); // Free the allocated memory. Marshal.FreeCoTaskMem(request.pOptions); Marshal.FreeCoTaskMem(intptrStruct); if (!bReturn) { throw new ApplicationException(" Set Internet Option Failed! "); } // Notify the system that the registry settings have been changed and cause // the proxy data to be reread from the registry for a handle. InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); InternetCloseHandle(hInternet); return(bReturn); }
private static bool ProxyEnable(WebProxy wProxy) { try { bool bReturn = true; string sPrx = wProxy.Address.DnsSafeHost + ":" + wProxy.Address.Port; INTERNET_PER_CONN_OPTION_LIST list = new INTERNET_PER_CONN_OPTION_LIST(); int dwBufSize = Marshal.SizeOf(list); INTERNET_PER_CONN_OPTION[] opts = new INTERNET_PER_CONN_OPTION[3]; int opt_size = Marshal.SizeOf(opts[0]); list.dwSize = dwBufSize; list.pszConnection = String.Empty; list.dwOptionCount = 3; //set flags opts[0].dwOption = (int)MyOptions.INTERNET_PER_CONN_FLAGS; //opts[0].dwValue = (int)(Options.PROXY_TYPE_DIRECT | Options.PROXY_TYPE_PROXY); //opts[0].dwValue = (int)(MyOptions.PROXY_TYPE_DIRECT | MyOptions.PROXY_TYPE_PROXY); opts[0].dwValue = (int)(Flags.PROXY_TYPE_PROXY); //set proxyname opts[1].dwOption = (int)MyOptions.INTERNET_PER_CONN_PROXY_SERVER; //opts[1].pszValue = Marshal.StringToHGlobalAnsi("http://" + sPrx); opts[1].pszValue = Marshal.StringToHGlobalAnsi(sPrx); //opts[1].pszValue = Marshal.StringToCoTaskMemAnsi("http=http://" + sPrx + "; ftp=ftp://" + sPrx + "; https=https://" + sPrx + "; gopher=gopher://" + sPrx + "; socks=socks://" + sPrx); //set override opts[2].dwOption = (int)MyOptions.INTERNET_PER_CONN_PROXY_BYPASS; opts[2].pszValue = Marshal.StringToHGlobalAnsi("localhost"); //opts[2].pszValue = Marshal.StringToCoTaskMemAnsi("<local>localhost; rado.ra-host.com"); byte[] b = new byte[3 * opt_size]; opts[0].GetBytes().CopyTo(b, 0); opts[1].GetBytes().CopyTo(b, opt_size); opts[2].GetBytes().CopyTo(b, 2 * opt_size); IntPtr ptr = Marshal.AllocCoTaskMem(3 * opt_size); Marshal.Copy(b, 0, ptr, 3 * opt_size); list.pOptions = ptr; //set the options on the connection bReturn = InternetSetOption(IntPtr.Zero, (int)MyOptions.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize); if (!bReturn) { Console.WriteLine(GetLastError()); } //Flush the current IE proxy setting bReturn = InternetSetOption(IntPtr.Zero, (int)MyOptions.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); if (!bReturn) { Console.WriteLine(GetLastError()); } Marshal.FreeHGlobal(opts[1].pszValue); Marshal.FreeHGlobal(opts[2].pszValue); Marshal.FreeCoTaskMem(ptr); return(bReturn); } catch (Exception) { throw; } }
/// <summary> /// Restore the options for LAN connection. /// </summary> /// <param name="request"></param> /// <returns></returns> public static void Disable() { IntPtr internetHandle = IntPtr.Zero; internetHandle = InternetOpen(AGENT, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); if (internetHandle == IntPtr.Zero) { throw new FlashpointProxyException("Could not open the Internet Handle."); } // initialize a INTERNET_PER_CONN_OPTION_LIST instance INTERNET_PER_CONN_OPTION_LIST internetPerConnOptionList = new INTERNET_PER_CONN_OPTION_LIST(); int internetPerConnOptionListSize = Marshal.SizeOf(internetPerConnOptionList); // create two options INTERNET_PER_CONN_OPTION[] internetPerConnOptionListOptions = new INTERNET_PER_CONN_OPTION[2]; GetSystemProxy(ref internetPerConnOptionList, ref internetPerConnOptionListOptions); // allocate memory IntPtr internetPerConnOptionListPointer = Marshal.AllocCoTaskMem(internetPerConnOptionListSize); // convert structure to IntPtr Marshal.StructureToPtr(internetPerConnOptionList, internetPerConnOptionListPointer, true); // set internet options bool result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, internetPerConnOptionListPointer, internetPerConnOptionListSize); // free the allocated memory Marshal.FreeCoTaskMem(internetPerConnOptionList.OptionsPointer); Marshal.FreeCoTaskMem(internetPerConnOptionListPointer); // notify the system that the registry settings have been changed and cause // the proxy data to be reread from the registry for a handle if (result) { result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); } if (result) { result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); } if (!InternetCloseHandle(internetHandle)) { throw new FlashpointProxyException("Could not close the Internet Handle."); } if (!result) { throw new FlashpointProxyException("Could not set the Internet Options."); } }
/// <summary> /// Backup the current options for LAN connection. /// Make sure free the memory after restoration. /// </summary> private static INTERNET_PER_CONN_OPTION_LIST GetSystemProxy() { // Query following options. var Options = new INTERNET_PER_CONN_OPTION[3]; Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[2] = new INTERNET_PER_CONN_OPTION(); Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS; // Allocate a block of memory of the options. var buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2])); var current = buffer; // Marshal data from a managed object to an unmanaged block of memory. for (var i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); current = (IntPtr)((int)current + Marshal.SizeOf(Options[i])); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. var Request = new INTERNET_PER_CONN_OPTION_LIST(); // Point to the allocated memory. Request.pOptions = buffer; Request.Size = Marshal.SizeOf(Request); // IntPtr.Zero means LAN connection. Request.Connection = IntPtr.Zero; Request.OptionCount = Options.Length; Request.OptionError = 0; var size = Marshal.SizeOf(Request); // // Query internet options. var result = InternetQueryOptionList(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, ref Request, ref size); if (!result) { throw new ApplicationException("Set Internet Option Failed!"); } return(Request); }
public static bool SetNamedProxy(string ProxyServer, string ProxyBypass, ref int Win32Error) { var list = new INTERNET_PER_CONN_OPTION_LIST(); var option = new INTERNET_PER_CONN_OPTION[3]; IntPtr pszProxyServer = Marshal.StringToHGlobalAuto(ProxyServer); IntPtr pszProxyBypass = Marshal.StringToHGlobalAuto(ProxyBypass); IntPtr pOptions = Marshal.AllocHGlobal(Marshal.SizeOf(option[0]) * 3); IntPtr pOption = new IntPtr(pOptions.ToInt64()); IntPtr pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(list)); bool iRes = false; try { option[0].dwOption = PerConnOption.INTERNET_PER_CONN_FLAGS; option[0].Value.dwValue = (int)(PerConnFlag.PROXY_TYPE_PROXY | PerConnFlag.PROXY_TYPE_DIRECT); option[1].dwOption = PerConnOption.INTERNET_PER_CONN_PROXY_SERVER; option[1].Value.pszValue = pszProxyServer; option[2].dwOption = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS; option[2].Value.pszValue = pszProxyBypass; for (var i = 0; i < 3; i++) { Marshal.StructureToPtr(option[i], pOption, true); pOption = new IntPtr(pOption.ToInt64() + Marshal.SizeOf(option[0])); } list.dwSize = Marshal.SizeOf(list); list.pszConnection = null; list.dwOptionCount = 3; list.pOptions = pOptions; Marshal.StructureToPtr(list, pBuffer, false); iRes = InternetSetOption(IntPtr.Zero, OptionFlag.INTERNET_OPTION_PER_CONNECTION_OPTION, pBuffer, Marshal.SizeOf(list)); Win32Error = Marshal.GetLastWin32Error(); } finally { Marshal.FreeHGlobal(pszProxyServer); Marshal.FreeHGlobal(pszProxyBypass); Marshal.FreeHGlobal(pOptions); Marshal.FreeHGlobal(pBuffer); } return(iRes); }
/// <summary> /// Proxy Ayarlama İşlemi /// </summary> /// <param name="proxy">Parametre olarak bir proxy değeri verin Ör : 192.168.1.1:8080</param> /// <returns>Proxy Ayar Sonucu</returns> public static bool ProxyAyarla(string proxy) { IntPtr hInternet = InternetOpen(applicationName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[2]; Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxy); System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1])); IntPtr current = buffer; for (int i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); int bir = Marshal.SizeOf(Options[i]); current += (int)bir; } INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST(); option_list.pOptions = buffer; option_list.Size = Marshal.SizeOf(option_list); option_list.Connection = IntPtr.Zero; option_list.OptionCount = Options.Length; option_list.OptionError = 0; int size = Marshal.SizeOf(option_list); IntPtr intptrStruct = Marshal.AllocCoTaskMem(size); Marshal.StructureToPtr(option_list, intptrStruct, true); bool bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); Marshal.FreeCoTaskMem(buffer); Marshal.FreeCoTaskMem(intptrStruct); InternetCloseHandle(hInternet); if (!bReturn) { throw new ApplicationException("Proxy Ayarlarken Hata Oluştu!"); } return(bReturn); }
/// <summary> /// Backup the current options for LAN connection. /// Make sure free the memory after restoration. /// </summary> private static void GetSystemProxy(ref INTERNET_PER_CONN_OPTION_LIST internetPerConnOptionList, ref INTERNET_PER_CONN_OPTION[] internetPerConnOptionListOptions) { int internetPerConnOptionListSize = Marshal.SizeOf(internetPerConnOptionList); if (internetPerConnOptionListOptions.Length < 2) { throw new ArgumentException("The Internet Per Connection Option List Options cannot have a Length of less than two."); } // set flags internetPerConnOptionListOptions[0] = new INTERNET_PER_CONN_OPTION(); internetPerConnOptionListOptions[0].Option = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; // set proxy name internetPerConnOptionListOptions[1] = new INTERNET_PER_CONN_OPTION(); internetPerConnOptionListOptions[1].Option = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; // allocate a block of memory of the options internetPerConnOptionList.OptionsPointer = Marshal.AllocCoTaskMem(Marshal.SizeOf(internetPerConnOptionListOptions[0]) + Marshal.SizeOf(internetPerConnOptionListOptions[1])); System.IntPtr internetPerConnOptionPointer = internetPerConnOptionList.OptionsPointer; // marshal data from a managed object to unmanaged memory for (int i = 0; i < internetPerConnOptionListOptions.Length; i++) { Marshal.StructureToPtr(internetPerConnOptionListOptions[i], internetPerConnOptionPointer, false); internetPerConnOptionPointer = (System.IntPtr)((int)internetPerConnOptionPointer + Marshal.SizeOf(internetPerConnOptionListOptions[i])); } // fill the internetPerConnOptionList structure internetPerConnOptionList.Size = internetPerConnOptionListSize; // NULL == LAN, otherwise connectoid name internetPerConnOptionList.Connection = IntPtr.Zero; // set two options internetPerConnOptionList.OptionCount = internetPerConnOptionListOptions.Length; internetPerConnOptionList.OptionError = 0; // query internet options bool result = InternetQueryOption(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, ref internetPerConnOptionList, ref internetPerConnOptionListSize); if (!result) { throw new FlashpointProxyException("Could not query the Internet Options."); } }
public static bool SetProxy(string proxy_full_addr) { bool bReturn; var list = new INTERNET_PER_CONN_OPTION_LIST(); int dwBufSize = Marshal.SizeOf(list); var opts = new INTERNET_PER_CONN_OPTION[4]; int opt_size = Marshal.SizeOf(opts[0]); list.dwSize = dwBufSize; list.pszConnection = Convert.ToString(ControlChars.NullChar); list.dwOptionCount = 3; opts[0].dwOption = (int)Options.INTERNET_PER_CONN_FLAGS; opts[0].dwValue = (int)(Options.PROXY_TYPE_DIRECT | Options.PROXY_TYPE_PROXY); opts[1].dwOption = (int)Options.INTERNET_PER_CONN_PROXY_SERVER; opts[1].pszValue = Marshal.StringToHGlobalAnsi(proxy_full_addr); opts[2].dwOption = (int)Options.INTERNET_PER_CONN_PROXY_BYPASS; opts[2].pszValue = Marshal.StringToHGlobalAnsi("local"); var b = new byte[3 * opt_size + 1]; opts[0].GetBytes().CopyTo(b, 0); opts[1].GetBytes().CopyTo(b, opt_size); opts[2].GetBytes().CopyTo(b, 2 * opt_size); var ptr = Marshal.AllocCoTaskMem(3 * opt_size); Marshal.Copy(b, 0, ptr, 3 * opt_size); list.pOptions = ptr; bReturn = InternetSetOption(IntPtr.Zero, (int)Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize); if (!bReturn) { Debug.WriteLine(GetLastError()); } bReturn = InternetSetOption(IntPtr.Zero, (int)Options.INTERNET_OPTION_SETTINGS_CHANGED, null, 0); if (!bReturn) { Debug.WriteLine(GetLastError()); } bReturn = InternetSetOption(IntPtr.Zero, (int)Options.INTERNET_OPTION_REFRESH, null, 0); if (!bReturn) { Debug.WriteLine(GetLastError()); } Marshal.FreeHGlobal(opts[1].pszValue); Marshal.FreeHGlobal(opts[2].pszValue); Marshal.FreeCoTaskMem(ptr); return(bReturn); }
public bool SetProxy(string proxy_full_addr) { INTERNET_PER_CONN_OPTION_LIST structure = new INTERNET_PER_CONN_OPTION_LIST(); int dwBufferLength = Marshal.SizeOf(structure); INTERNET_PER_CONN_OPTION[] internet_per_conn_optionArray = new INTERNET_PER_CONN_OPTION[4]; int index = Marshal.SizeOf(internet_per_conn_optionArray[0]); structure.dwSize = dwBufferLength; structure.pszConnection = "\0"; structure.dwOptionCount = 3; internet_per_conn_optionArray[0].dwOption = 1; internet_per_conn_optionArray[0].dwValue = 3; internet_per_conn_optionArray[1].dwOption = 2; internet_per_conn_optionArray[1].pszValue = Marshal.StringToHGlobalAnsi(proxy_full_addr); internet_per_conn_optionArray[2].dwOption = 3; internet_per_conn_optionArray[2].pszValue = Marshal.StringToHGlobalAnsi("local"); byte[] array = new byte[(3 * index) + 1]; internet_per_conn_optionArray[0].GetBytes().CopyTo(array, 0); internet_per_conn_optionArray[1].GetBytes().CopyTo(array, index); internet_per_conn_optionArray[2].GetBytes().CopyTo(array, (int)(2 * index)); IntPtr destination = Marshal.AllocCoTaskMem(3 * index); Marshal.Copy(array, 0, destination, 3 * index); structure.pOptions = destination; if (!InternetSetOption(IntPtr.Zero, 0x4b, structure, dwBufferLength)) { Debug.WriteLine(GetLastError()); } if (!InternetSetOption(IntPtr.Zero, 0x27, null, 0)) { Debug.WriteLine(GetLastError()); } bool flag = InternetSetOption(IntPtr.Zero, 0x25, null, 0); if (!flag) { Debug.WriteLine(GetLastError()); } Marshal.FreeHGlobal(internet_per_conn_optionArray[1].pszValue); Marshal.FreeHGlobal(internet_per_conn_optionArray[2].pszValue); Marshal.FreeCoTaskMem(destination); return(flag); }
private static INTERNET_PER_CONN_OPTION_LIST SistemProxy() { INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3]; Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[2] = new INTERNET_PER_CONN_OPTION(); Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS; System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2])); System.IntPtr current = (System.IntPtr)buffer; for (int i = 0; i < Options.Length; i++) { try { Marshal.StructureToPtr(Options[i], current, false); current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i])); } catch { } } INTERNET_PER_CONN_OPTION_LIST Request = new INTERNET_PER_CONN_OPTION_LIST(); Request.pOptions = buffer; Request.Size = Marshal.SizeOf(Request); Request.Connection = IntPtr.Zero; Request.OptionCount = Options.Length; Request.OptionError = 0; int size = Marshal.SizeOf(Request); bool result = InternetQueryOptionList(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, ref Request, ref size); if (!result) { try { throw new ApplicationException("Proxy Ayarlarken Hata Oluştu!"); } catch { } } return(Request); }
public static bool SetPacUrl(string PacUrl, ref int Win32Error) { var list = new INTERNET_PER_CONN_OPTION_LIST(); var option = new INTERNET_PER_CONN_OPTION[2]; IntPtr pszPacUrl = Marshal.StringToHGlobalAuto(PacUrl); IntPtr pOptions = Marshal.AllocHGlobal(Marshal.SizeOf(option[0]) * 2); IntPtr pOption = new IntPtr(pOptions.ToInt64()); IntPtr pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(list)); bool iRes = false; try { option[0].dwOption = PerConnOption.INTERNET_PER_CONN_FLAGS; option[0].Value.dwValue = (int)(PerConnFlag.PROXY_TYPE_AUTO_PROXY_URL | PerConnFlag.PROXY_TYPE_DIRECT); option[1].dwOption = PerConnOption.INTERNET_PER_CONN_AUTOCONFIG_URL; option[1].Value.pszValue = pszPacUrl; for (var i = 0; i < 2; i++) { Marshal.StructureToPtr(option[i], pOption, true); pOption = new IntPtr(pOption.ToInt64() + Marshal.SizeOf(option[0])); } list.dwSize = Marshal.SizeOf(list); list.pszConnection = null; list.dwOptionCount = 2; list.pOptions = pOptions; Marshal.StructureToPtr(list, pBuffer, false); iRes = InternetSetOption(IntPtr.Zero, OptionFlag.INTERNET_OPTION_PER_CONNECTION_OPTION, pBuffer, Marshal.SizeOf(list)); Win32Error = Marshal.GetLastWin32Error(); } finally { Marshal.FreeHGlobal(pszPacUrl); Marshal.FreeHGlobal(pOptions); Marshal.FreeHGlobal(pBuffer); } return(iRes); }
public static bool SetConnectionProxy(string proxyServer) { IntPtr hInternet = InternetOpen(agentName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); INTERNET_PER_CONN_OPTION[] options = new INTERNET_PER_CONN_OPTION[2]; options[0] = new INTERNET_PER_CONN_OPTION(); options[0].dwOption = (int)INTERNET_PER_CONN_OPTIONENUM.INTERNET_PER_CONN_FLAGS; options[0].value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; options[1] = new INTERNET_PER_CONN_OPTION(); options[1].dwOption = (int)INTERNET_PER_CONN_OPTIONENUM.INTERNET_PER_CONN_PROXY_SERVER; options[1].value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer); IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(options[0]) + Marshal.SizeOf(options[1])); IntPtr current = buffer; for (int i = 0; i < options.Length; i++) { Marshal.StructureToPtr(options[i], current, false); current = (IntPtr)((int)current + Marshal.SizeOf(options[i])); } INTERNET_PER_CONN_OPTION_LIST optionList = new INTERNET_PER_CONN_OPTION_LIST(); optionList.pOptions = buffer; optionList.Size = Marshal.SizeOf(optionList); optionList.Connection = IntPtr.Zero; optionList.OptionCount = options.Length; optionList.OptionError = 0; int size = Marshal.SizeOf(optionList); IntPtr intPtrStruct = Marshal.AllocCoTaskMem(size); Marshal.StructureToPtr(optionList, intPtrStruct, true); bool bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intPtrStruct, size); Marshal.FreeCoTaskMem(buffer); Marshal.FreeCoTaskMem(intPtrStruct); InternetCloseHandle(hInternet); return(bReturn); }
public static bool DisableProxy() { bool bReturn; var list = new INTERNET_PER_CONN_OPTION_LIST(); int dwBufSize = Marshal.SizeOf(list); var opts = new INTERNET_PER_CONN_OPTION[1]; int opt_size = Marshal.SizeOf(opts[0]); list.dwSize = dwBufSize; list.pszConnection = Convert.ToString(ControlChars.NullChar); list.dwOptionCount = 1; opts[0].dwOption = (int)Options.INTERNET_PER_CONN_FLAGS; opts[0].dwValue = (int)Options.PROXY_TYPE_DIRECT; var b = new byte[opt_size + 1]; opts[0].GetBytes().CopyTo(b, 0); var ptr = Marshal.AllocCoTaskMem(opt_size); Marshal.Copy(b, 0, ptr, opt_size); list.pOptions = ptr; bReturn = InternetSetOption(IntPtr.Zero, (int)Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize); if (!bReturn) { Debug.WriteLine(GetLastError()); } bReturn = InternetSetOption(IntPtr.Zero, (int)Options.INTERNET_OPTION_SETTINGS_CHANGED, null, 0); if (!bReturn) { Debug.WriteLine(GetLastError()); } bReturn = InternetSetOption(IntPtr.Zero, (int)Options.INTERNET_OPTION_REFRESH, null, 0); if (!bReturn) { Debug.WriteLine(GetLastError()); } Marshal.FreeCoTaskMem(ptr); return(bReturn); }
public static bool SetConnectionProxy(INTERNET_PER_CONN_OPTION_LIST optionList) { IntPtr hInternet = IntPtr.Zero; int size = Marshal.SizeOf(optionList); // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance. IntPtr intptrStruct = Marshal.AllocCoTaskMem(size); // Marshal data from a managed object to an unmanaged block of memory. Marshal.StructureToPtr(optionList, intptrStruct, true); // Set internet settings. bool bReturn = InternetSetOption( hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); // Free the allocated memory. Marshal.FreeCoTaskMem(intptrStruct); return(bReturn); }
public bool DisableProxy() { INTERNET_PER_CONN_OPTION_LIST structure = new INTERNET_PER_CONN_OPTION_LIST(); int dwBufferLength = Marshal.SizeOf(structure); INTERNET_PER_CONN_OPTION[] internet_per_conn_optionArray = new INTERNET_PER_CONN_OPTION[1]; int cb = Marshal.SizeOf(internet_per_conn_optionArray[0]); structure.dwSize = dwBufferLength; structure.pszConnection = "\0"; structure.dwOptionCount = 1; internet_per_conn_optionArray[0].dwOption = 1; internet_per_conn_optionArray[0].dwValue = 1; byte[] array = new byte[cb + 1]; internet_per_conn_optionArray[0].GetBytes().CopyTo(array, 0); IntPtr destination = Marshal.AllocCoTaskMem(cb); Marshal.Copy(array, 0, destination, cb); structure.pOptions = destination; if (!InternetSetOption(IntPtr.Zero, 0x4b, structure, dwBufferLength)) { Debug.WriteLine(GetLastError()); } if (!InternetSetOption(IntPtr.Zero, 0x27, null, 0)) { Debug.WriteLine(GetLastError()); } bool flag = InternetSetOption(IntPtr.Zero, 0x25, null, 0); if (!flag) { Debug.WriteLine(GetLastError()); } Marshal.FreeCoTaskMem(destination); return(flag); }
/// <summary> /// Set the proxy server for LAN connection. /// </summary> public static bool SetConnectionProxy(string proxyServer, string proxyLogin = null, string proxyPass = null) { var hInternet = InternetOpen(applicationName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); //// Create 3 options. //INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3]; // Create 2 options. var Options = new INTERNET_PER_CONN_OPTION[2]; // Set PROXY flags. Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; // Set proxy name. Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer); /* * if (proxyLogin!=null && proxyPass!=null) * { * // Set proxy name. * Options[2] = new INTERNET_PER_CONN_OPTION(); * Options[2].dwOption = * (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; * Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer); * * // Set proxy name. * Options[3] = new INTERNET_PER_CONN_OPTION(); * Options[3].dwOption = * (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; * Options[3].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer); * }*/ //// Set proxy bypass. //Options[2] = new INTERNET_PER_CONN_OPTION(); //Options[2].dwOption = // (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS; //Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi(“local”); //// Allocate a block of memory of the options. //System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) // + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2])); // Allocate a block of memory of the options. var buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1])); var current = buffer; // Marshal data from a managed object to an unmanaged block of memory. for (var i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); current = (IntPtr)((int)current + Marshal.SizeOf(Options[i])); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. var option_list = new INTERNET_PER_CONN_OPTION_LIST(); // Point to the allocated memory. option_list.pOptions = buffer; // Return the unmanaged size of an object in bytes. option_list.Size = Marshal.SizeOf(option_list); // IntPtr.Zero means LAN connection. option_list.Connection = IntPtr.Zero; option_list.OptionCount = Options.Length; option_list.OptionError = 0; var size = Marshal.SizeOf(option_list); // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance. var intptrStruct = Marshal.AllocCoTaskMem(size); // Marshal data from a managed object to an unmanaged block of memory. Marshal.StructureToPtr(option_list, intptrStruct, true); // Set internet settings. var bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); //установка login / pass для proxy /* * string un = "login"; * string pw = "pass"; * IntPtr username = Marshal.StringToHGlobalAnsi(un); * IntPtr password = Marshal.StringToHGlobalAnsi(pw); * * IntPtr hConnectHandle = WinInetInterop.InternetConnect(hInternet, "185.125.219.169", 0, un, pw, 3, 0, IntPtr.Zero); * var errorG1 = Marshal.GetLastWin32Error(); * * WinInetInterop.InternetSetOption(hConnectHandle, WinInetInterop.INTERNET_OPTION.INTERNET_OPTION_PROXY_USERNAME, username, Marshal.SizeOf(username)); * WinInetInterop.InternetSetOption(hConnectHandle, WinInetInterop.INTERNET_OPTION.INTERNET_OPTION_PROXY_PASSWORD, password, Marshal.SizeOf(password)); * var errorG = Marshal.GetLastWin32Error(); * * IntPtr hResourceHandle = WinInetInterop.HttpOpenRequest(hConnectHandle, "GET", "/", null, null, null, 0x00400000, IntPtr.Zero); * var errorG2 = Marshal.GetLastWin32Error(); * var r = WinInetInterop.HttpSendRequest(hResourceHandle, "", 0, IntPtr.Zero, 0); * var errorG3 = Marshal.GetLastWin32Error(); */ // Free the allocated memory. Marshal.FreeCoTaskMem(buffer); Marshal.FreeCoTaskMem(intptrStruct); InternetCloseHandle(hInternet); // Throw an exception if this operation failed. if (!bReturn) { throw new ApplicationException("Set Internet Option Failed!"); } return(bReturn); }
public bool GetFromWinINET(string sConnectionName) { this.Clear(); try { INTERNET_PER_CONN_OPTION_LIST structure = new INTERNET_PER_CONN_OPTION_LIST(); INTERNET_PER_CONN_OPTION[] internet_per_conn_optionArray = new INTERNET_PER_CONN_OPTION[5]; if (sConnectionName == "DefaultLAN") { sConnectionName = null; } structure.Connection = sConnectionName; structure.OptionCount = internet_per_conn_optionArray.Length; structure.OptionError = 0; internet_per_conn_optionArray[0] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[0].dwOption = 1; internet_per_conn_optionArray[1] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[1].dwOption = 2; internet_per_conn_optionArray[2] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[2].dwOption = 3; internet_per_conn_optionArray[3] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[3].dwOption = 4; internet_per_conn_optionArray[4] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[4].dwOption = 10; int cb = 0; for (int i = 0; i < internet_per_conn_optionArray.Length; i++) { cb += Marshal.SizeOf(internet_per_conn_optionArray[i]); } IntPtr ptr = Marshal.AllocCoTaskMem(cb); IntPtr ptr2 = ptr; for (int j = 0; j < internet_per_conn_optionArray.Length; j++) { Marshal.StructureToPtr(internet_per_conn_optionArray[j], ptr2, false); ptr2 = (IntPtr) (((long) ptr2) + Marshal.SizeOf(internet_per_conn_optionArray[j])); } structure.pOptions = ptr; structure.Size = Marshal.SizeOf(structure); int size = structure.Size; bool flag = InternetQueryOptionList(IntPtr.Zero, 0x4b, ref structure, ref size); int num5 = Marshal.GetLastWin32Error(); if (!flag) { if (0x57 == num5) { internet_per_conn_optionArray[4].dwOption = 5; ptr2 = ptr; for (int k = 0; k < internet_per_conn_optionArray.Length; k++) { Marshal.StructureToPtr(internet_per_conn_optionArray[k], ptr2, false); ptr2 = (IntPtr) (((long) ptr2) + Marshal.SizeOf(internet_per_conn_optionArray[k])); } structure.pOptions = ptr; structure.Size = Marshal.SizeOf(structure); size = structure.Size; flag = InternetQueryOptionList(IntPtr.Zero, 0x4b, ref structure, ref size); num5 = Marshal.GetLastWin32Error(); } if (!flag) { object[] args = new object[] { sConnectionName ?? "DefaultLAN", num5.ToString("x") }; FiddlerApplication.Log.LogFormat("Fiddler was unable to get information about the proxy for '{0}' [0x{1}].\n", args); } } if (flag) { ptr2 = ptr; for (int m = 0; m < internet_per_conn_optionArray.Length; m++) { Marshal.PtrToStructure(ptr2, internet_per_conn_optionArray[m]); ptr2 = (IntPtr) (((long) ptr2) + Marshal.SizeOf(internet_per_conn_optionArray[m])); } this._bDirect = 1 == (internet_per_conn_optionArray[0].Value.dwValue & 1); this._bUseConfigScript = 4 == (internet_per_conn_optionArray[0].Value.dwValue & 4); this._bAutoDetect = 8 == (internet_per_conn_optionArray[0].Value.dwValue & 8); this._bProxiesSpecified = 2 == (internet_per_conn_optionArray[0].Value.dwValue & 2); if (internet_per_conn_optionArray[4].dwOption == 10) { this._bAutoDetectWasUserSet = 8 == (internet_per_conn_optionArray[4].Value.dwValue & 8); } else { this._bAutoDetectWasUserSet = this._bAutoDetect && (1 == (internet_per_conn_optionArray[4].Value.dwValue & 1)); } this._sScriptURL = Marshal.PtrToStringAnsi(internet_per_conn_optionArray[3].Value.pszValue); Utilities.GlobalFree(internet_per_conn_optionArray[3].Value.pszValue); if (internet_per_conn_optionArray[1].Value.pszValue != IntPtr.Zero) { string sProxyString = Marshal.PtrToStringAnsi(internet_per_conn_optionArray[1].Value.pszValue); Utilities.GlobalFree(internet_per_conn_optionArray[1].Value.pszValue); this.SetManualProxies(sProxyString); } if (internet_per_conn_optionArray[2].Value.pszValue != IntPtr.Zero) { this._sHostsThatBypass = Marshal.PtrToStringAnsi(internet_per_conn_optionArray[2].Value.pszValue); Utilities.GlobalFree(internet_per_conn_optionArray[2].Value.pszValue); } } Marshal.FreeCoTaskMem(ptr); return flag; } catch (Exception exception) { FiddlerApplication.ReportException(exception, "Unable to get proxy information for " + (sConnectionName ?? "DefaultLAN")); return false; } }
internal bool SetToWinINET(string sConnectionName) { try { INTERNET_PER_CONN_OPTION_LIST structure = new INTERNET_PER_CONN_OPTION_LIST(); INTERNET_PER_CONN_OPTION[] internet_per_conn_optionArray = new INTERNET_PER_CONN_OPTION[5]; if (sConnectionName == "DefaultLAN") { sConnectionName = null; } structure.Connection = sConnectionName; structure.OptionCount = internet_per_conn_optionArray.Length; structure.OptionError = 0; int num = 0; if (this._bDirect) { num |= 1; } if (this._bAutoDetect) { num |= 8; } if (this._bAutoDetectWasUserSet) { num |= 8; } if (this._bUseConfigScript) { num |= 4; } if (this._bProxiesSpecified) { num |= 2; } internet_per_conn_optionArray[0] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[0].dwOption = 1; internet_per_conn_optionArray[0].Value.dwValue = num; internet_per_conn_optionArray[1] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[1].dwOption = 2; internet_per_conn_optionArray[1].Value.pszValue = Marshal.StringToHGlobalAnsi(this.CalculateProxyString()); internet_per_conn_optionArray[2] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[2].dwOption = 3; internet_per_conn_optionArray[2].Value.pszValue = Marshal.StringToHGlobalAnsi(this._sHostsThatBypass); internet_per_conn_optionArray[3] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[3].dwOption = 4; internet_per_conn_optionArray[3].Value.pszValue = Marshal.StringToHGlobalAnsi(this._sScriptURL); internet_per_conn_optionArray[4] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[4].dwOption = 5; internet_per_conn_optionArray[4].Value.dwValue = 0; if (this._bAutoDetectWasUserSet) { internet_per_conn_optionArray[4].Value.dwValue = 1; } int cb = 0; for (int i = 0; i < internet_per_conn_optionArray.Length; i++) { cb += Marshal.SizeOf(internet_per_conn_optionArray[i]); } IntPtr ptr = Marshal.AllocCoTaskMem(cb); IntPtr ptr2 = ptr; for (int j = 0; j < internet_per_conn_optionArray.Length; j++) { Marshal.StructureToPtr(internet_per_conn_optionArray[j], ptr2, false); ptr2 = (IntPtr) (((long) ptr2) + Marshal.SizeOf(internet_per_conn_optionArray[j])); } structure.pOptions = ptr; structure.Size = Marshal.SizeOf(structure); int size = structure.Size; bool flag = InternetSetOptionList(IntPtr.Zero, 0x4b, ref structure, size); int num6 = Marshal.GetLastWin32Error(); if (flag) { InternetSetOption(IntPtr.Zero, 0x5f, IntPtr.Zero, 0); } else { Trace.WriteLine("[Fiddler] SetProxy failed. WinINET Error #" + num6.ToString("x")); } Marshal.FreeHGlobal(internet_per_conn_optionArray[0].Value.pszValue); Marshal.FreeHGlobal(internet_per_conn_optionArray[1].Value.pszValue); Marshal.FreeHGlobal(internet_per_conn_optionArray[2].Value.pszValue); Marshal.FreeCoTaskMem(ptr); return flag; } catch (Exception exception) { Trace.WriteLine("[Fiddler] SetProxy failed. " + exception.Message); return false; } }
public bool GetFromWinINET(string sConnectionName) { this.Clear(); try { INTERNET_PER_CONN_OPTION_LIST structure = new INTERNET_PER_CONN_OPTION_LIST(); INTERNET_PER_CONN_OPTION[] internet_per_conn_optionArray = new INTERNET_PER_CONN_OPTION[5]; if (sConnectionName == "DefaultLAN") { sConnectionName = null; } structure.Connection = sConnectionName; structure.OptionCount = internet_per_conn_optionArray.Length; structure.OptionError = 0; internet_per_conn_optionArray[0] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[0].dwOption = 1; internet_per_conn_optionArray[1] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[1].dwOption = 2; internet_per_conn_optionArray[2] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[2].dwOption = 3; internet_per_conn_optionArray[3] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[3].dwOption = 4; internet_per_conn_optionArray[4] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[4].dwOption = 10; int cb = 0; for (int i = 0; i < internet_per_conn_optionArray.Length; i++) { cb += Marshal.SizeOf(internet_per_conn_optionArray[i]); } IntPtr ptr = Marshal.AllocCoTaskMem(cb); IntPtr ptr2 = ptr; for (int j = 0; j < internet_per_conn_optionArray.Length; j++) { Marshal.StructureToPtr(internet_per_conn_optionArray[j], ptr2, false); ptr2 = (IntPtr)(((long)ptr2) + Marshal.SizeOf(internet_per_conn_optionArray[j])); } structure.pOptions = ptr; structure.Size = Marshal.SizeOf(structure); int size = structure.Size; bool flag = InternetQueryOptionList(IntPtr.Zero, 0x4b, ref structure, ref size); int num5 = Marshal.GetLastWin32Error(); if (!flag && (0x57 == num5)) { internet_per_conn_optionArray[4].dwOption = 5; ptr2 = ptr; for (int k = 0; k < internet_per_conn_optionArray.Length; k++) { Marshal.StructureToPtr(internet_per_conn_optionArray[k], ptr2, false); ptr2 = (IntPtr)(((long)ptr2) + Marshal.SizeOf(internet_per_conn_optionArray[k])); } structure.pOptions = ptr; structure.Size = Marshal.SizeOf(structure); size = structure.Size; flag = InternetQueryOptionList(IntPtr.Zero, 0x4b, ref structure, ref size); num5 = Marshal.GetLastWin32Error(); } if (flag) { ptr2 = ptr; for (int m = 0; m < internet_per_conn_optionArray.Length; m++) { Marshal.PtrToStructure(ptr2, internet_per_conn_optionArray[m]); ptr2 = (IntPtr)(((long)ptr2) + Marshal.SizeOf(internet_per_conn_optionArray[m])); } this._bDirect = 1 == (internet_per_conn_optionArray[0].Value.dwValue & 1); this._bUseBrowserScript = 4 == (internet_per_conn_optionArray[0].Value.dwValue & 4); this._bAutoDetect = 8 == (internet_per_conn_optionArray[0].Value.dwValue & 8); this._bProxiesSpecified = 2 == (internet_per_conn_optionArray[0].Value.dwValue & 2); if (internet_per_conn_optionArray[4].dwOption == 10) { this._bAutoDetectWasUserSet = 8 == (internet_per_conn_optionArray[4].Value.dwValue & 8); } else { this._bAutoDetectWasUserSet = this._bAutoDetect && (1 == (internet_per_conn_optionArray[4].Value.dwValue & 1)); } this._sScriptURL = Marshal.PtrToStringAnsi(internet_per_conn_optionArray[3].Value.pszValue); Utilities.GlobalFree(internet_per_conn_optionArray[3].Value.pszValue); if (internet_per_conn_optionArray[1].Value.pszValue != IntPtr.Zero) { string sProxyString = Marshal.PtrToStringAnsi(internet_per_conn_optionArray[1].Value.pszValue); Utilities.GlobalFree(internet_per_conn_optionArray[1].Value.pszValue); this.InitializeFromProxyString(sProxyString); } if (internet_per_conn_optionArray[2].Value.pszValue != IntPtr.Zero) { this._sHostsThatBypass = Marshal.PtrToStringAnsi(internet_per_conn_optionArray[2].Value.pszValue); Utilities.GlobalFree(internet_per_conn_optionArray[2].Value.pszValue); this._bBypassIntranetHosts = this._sHostsThatBypass.Contains("<local>"); } } Marshal.FreeCoTaskMem(ptr); return(flag); } catch (Exception exception) { SDVPApplication.ReportException(exception, "Unable to get proxy information for " + (sConnectionName ?? "DefaultLAN")); return(false); } }
internal bool SetToWinINET(string sConnectionName) { try { INTERNET_PER_CONN_OPTION_LIST structure = new INTERNET_PER_CONN_OPTION_LIST(); INTERNET_PER_CONN_OPTION[] internet_per_conn_optionArray = new INTERNET_PER_CONN_OPTION[5]; if (sConnectionName == "DefaultLAN") { sConnectionName = null; } structure.Connection = sConnectionName; structure.OptionCount = internet_per_conn_optionArray.Length; structure.OptionError = 0; int num = 0; if (this._bDirect) { num |= 1; } if (this._bAutoDetect) { num |= 8; } if (this._bAutoDetectWasUserSet) { num |= 8; } if (this._bUseBrowserScript) { num |= 4; } if (this._bProxiesSpecified) { num |= 2; } internet_per_conn_optionArray[0] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[0].dwOption = 1; internet_per_conn_optionArray[0].Value.dwValue = num; internet_per_conn_optionArray[1] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[1].dwOption = 2; internet_per_conn_optionArray[1].Value.pszValue = Marshal.StringToHGlobalAnsi(this.CalculateProxyString()); internet_per_conn_optionArray[2] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[2].dwOption = 3; internet_per_conn_optionArray[2].Value.pszValue = Marshal.StringToHGlobalAnsi(this._sHostsThatBypass); internet_per_conn_optionArray[3] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[3].dwOption = 4; internet_per_conn_optionArray[3].Value.pszValue = Marshal.StringToHGlobalAnsi(this._sScriptURL); internet_per_conn_optionArray[4] = new INTERNET_PER_CONN_OPTION(); internet_per_conn_optionArray[4].dwOption = 5; internet_per_conn_optionArray[4].Value.dwValue = 0; if (this._bAutoDetectWasUserSet) { internet_per_conn_optionArray[4].Value.dwValue = 1; } int cb = 0; for (int i = 0; i < internet_per_conn_optionArray.Length; i++) { cb += Marshal.SizeOf(internet_per_conn_optionArray[i]); } IntPtr ptr = Marshal.AllocCoTaskMem(cb); IntPtr ptr2 = ptr; for (int j = 0; j < internet_per_conn_optionArray.Length; j++) { Marshal.StructureToPtr(internet_per_conn_optionArray[j], ptr2, false); ptr2 = (IntPtr)(((long)ptr2) + Marshal.SizeOf(internet_per_conn_optionArray[j])); } structure.pOptions = ptr; structure.Size = Marshal.SizeOf(structure); int size = structure.Size; bool flag = InternetSetOptionList(IntPtr.Zero, 0x4b, ref structure, size); int num6 = Marshal.GetLastWin32Error(); if (flag) { InternetSetOption(IntPtr.Zero, 0x5f, IntPtr.Zero, 0); } else { Trace.WriteLine("[SDVP] SetProxy failed. WinINET Error #" + num6.ToString("x")); } Marshal.FreeHGlobal(internet_per_conn_optionArray[0].Value.pszValue); Marshal.FreeHGlobal(internet_per_conn_optionArray[1].Value.pszValue); Marshal.FreeHGlobal(internet_per_conn_optionArray[2].Value.pszValue); Marshal.FreeCoTaskMem(ptr); return(flag); } catch (Exception exception) { Trace.WriteLine("[SDVP] SetProxy failed. " + exception.Message); return(false); } }
/// <summary> /// Set the proxy server for LAN connection. /// </summary> public static bool SetConnectionProxy(string proxyServer) { IntPtr hInternet = InternetOpen(applicationName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); //// Create 3 options. //INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3]; // Create 2 options. INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[2]; // Set PROXY flags. Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; // Set proxy name. Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer); //// Set proxy bypass. //Options[2] = new INTERNET_PER_CONN_OPTION(); //Options[2].dwOption = // (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS; //Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi("local"); //// Allocate a block of memory of the options. //System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) // + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2])); // Allocate a block of memory of the options. IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1])); IntPtr current = buffer; // Marshal data from a managed object to an unmanaged block of memory. for (int i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); // current = (IntPtr)((int)current + Marshal.SizeOf(Options[i])); //fixed by user Jianping Liu to include 64bit process current = (IntPtr)((Environment.Is64BitProcess ? current.ToInt64() : current.ToInt32()) + Marshal.SizeOf(Options[i])); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST(); // Point to the allocated memory. option_list.pOptions = buffer; // Return the unmanaged size of an object in bytes. option_list.Size = Marshal.SizeOf(option_list); // IntPtr.Zero means LAN connection. option_list.Connection = IntPtr.Zero; option_list.OptionCount = Options.Length; option_list.OptionError = 0; int size = Marshal.SizeOf(option_list); // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance. IntPtr intptrStruct = Marshal.AllocCoTaskMem(size); // Marshal data from a managed object to an unmanaged block of memory. Marshal.StructureToPtr(option_list, intptrStruct, true); // Set internet settings. bool bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); // Free the allocated memory. Marshal.FreeCoTaskMem(buffer); Marshal.FreeCoTaskMem(intptrStruct); InternetCloseHandle(hInternet); // Throw an exception if this operation failed. if (!bReturn) { throw new ApplicationException(" Set Internet Option Failed!"); } return(bReturn); }
private static bool ProxyEnable(WebProxy wProxy) { try { bool bReturn = true; string sPrx = wProxy.Address.DnsSafeHost + ":" + wProxy.Address.Port; INTERNET_PER_CONN_OPTION_LIST list = new INTERNET_PER_CONN_OPTION_LIST(); int dwBufSize = Marshal.SizeOf(list); INTERNET_PER_CONN_OPTION[] opts = new INTERNET_PER_CONN_OPTION[3]; int opt_size = Marshal.SizeOf(opts[0]); list.dwSize = dwBufSize; list.pszConnection = String.Empty; list.dwOptionCount = 3; //set flags opts[0].dwOption = (int)MyOptions.INTERNET_PER_CONN_FLAGS; //opts[0].dwValue = (int)(Options.PROXY_TYPE_DIRECT | Options.PROXY_TYPE_PROXY); //opts[0].dwValue = (int)(MyOptions.PROXY_TYPE_DIRECT | MyOptions.PROXY_TYPE_PROXY); opts[0].dwValue = (int)(Flags.PROXY_TYPE_PROXY); //set proxyname opts[1].dwOption = (int)MyOptions.INTERNET_PER_CONN_PROXY_SERVER; //opts[1].pszValue = Marshal.StringToHGlobalAnsi("http://" + sPrx); opts[1].pszValue = Marshal.StringToHGlobalAnsi(sPrx); //opts[1].pszValue = Marshal.StringToCoTaskMemAnsi("http=http://" + sPrx + "; ftp=ftp://" + sPrx + "; https=https://" + sPrx + "; gopher=gopher://" + sPrx + "; socks=socks://" + sPrx); //set override opts[2].dwOption = (int)MyOptions.INTERNET_PER_CONN_PROXY_BYPASS; opts[2].pszValue = Marshal.StringToHGlobalAnsi("localhost"); //opts[2].pszValue = Marshal.StringToCoTaskMemAnsi("<local>localhost; rado.ra-host.com"); byte[] b = new byte[3 * opt_size]; opts[0].GetBytes().CopyTo(b, 0); opts[1].GetBytes().CopyTo(b, opt_size); opts[2].GetBytes().CopyTo(b, 2 * opt_size); IntPtr ptr = Marshal.AllocCoTaskMem(3 * opt_size); Marshal.Copy(b, 0, ptr, 3 * opt_size); list.pOptions = ptr; //set the options on the connection bReturn = InternetSetOption(IntPtr.Zero, (int)MyOptions.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize); if (!bReturn) { Console.WriteLine(GetLastError()); } //Flush the current IE proxy setting bReturn = InternetSetOption(IntPtr.Zero, (int)MyOptions.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); if (!bReturn) { Console.WriteLine(GetLastError()); } Marshal.FreeHGlobal(opts[1].pszValue); Marshal.FreeHGlobal(opts[2].pszValue); Marshal.FreeCoTaskMem(ptr); return bReturn; } catch (Exception) { throw; } }
/// <summary> /// Set the proxy server for LAN connection. /// </summary> public static bool SetConnectionProxy(string proxyServer) { IntPtr hInternet = InternetOpen(applicationName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); //// Create 3 options. //INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3]; // Create 2 options. INTERNET_PER_CONN_OPTION[] Options; // Set PROXY flags. System.IntPtr buffer; if (string.IsNullOrEmpty(proxyServer)) { Options = new INTERNET_PER_CONN_OPTION[1]; Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_DIRECT; buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])); } else { Options= new INTERNET_PER_CONN_OPTION[2]; Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; // Set proxy name. Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer); buffer=Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1])); } //// Set proxy bypass. //Options[2] = new INTERNET_PER_CONN_OPTION(); //Options[2].dwOption = // (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS; //Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi("local"); //// Allocate a block of memory of the options. //System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) // + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2])); // Allocate a block of memory of the options. System.IntPtr current = buffer; // Marshal data from a managed object to an unmanaged block of memory. for (int i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i])); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST(); // Point to the allocated memory. option_list.pOptions = buffer; // Return the unmanaged size of an object in bytes. option_list.Size = Marshal.SizeOf(option_list); // IntPtr.Zero means LAN connection. option_list.Connection = IntPtr.Zero; option_list.OptionCount = Options.Length; option_list.OptionError = 0; int size = Marshal.SizeOf(option_list); // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance. IntPtr intptrStruct = Marshal.AllocCoTaskMem(size); // Marshal data from a managed object to an unmanaged block of memory. Marshal.StructureToPtr(option_list, intptrStruct, true); // Set internet settings. bool bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); // Free the allocated memory. Marshal.FreeCoTaskMem(buffer); Marshal.FreeCoTaskMem(intptrStruct); InternetCloseHandle(hInternet); // Throw an exception if this operation failed. if (!bReturn) { throw new ApplicationException(" Set Internet Option Failed!"); } return bReturn; }
private extern static bool InternetQueryOptionList( IntPtr Handle, INTERNET_OPTION OptionFlag, ref INTERNET_PER_CONN_OPTION_LIST OptionList, ref int size);
private static extern bool InternetSetOptionList(IntPtr hInternet, int Option, ref INTERNET_PER_CONN_OPTION_LIST OptionList, int size);
/// <summary> /// Set the proxy server for LAN connection. /// </summary> static bool SetConnectionProxyInternal(IntPtr hInternet, string proxyServer) { // Create 3 options. INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3]; // Set PROXY flags. Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; // Set proxy name. Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer); // Set proxy bypass. Options[2] = new INTERNET_PER_CONN_OPTION(); Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS; Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi("local"); // Allocate a block of memory of the options. System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2])); System.IntPtr current = buffer; // Marshal data from a managed object to an unmanaged block of memory. for (int i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i])); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST(); // Point to the allocated memory. option_list.pOptions = buffer; // Return the unmanaged size of an object in bytes. option_list.Size = Marshal.SizeOf(option_list); // IntPtr.Zero means LAN connection. option_list.Connection = IntPtr.Zero; option_list.OptionCount = Options.Length; option_list.OptionError = 0; int size = Marshal.SizeOf(option_list); // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance. IntPtr intptrStruct = Marshal.AllocCoTaskMem(size); // Marshal data from a managed object to an unmanaged block of memory. Marshal.StructureToPtr(option_list, intptrStruct, true); // Set internet settings. bool bReturn = InternetSetOption( hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); // Free the allocated memory. Marshal.FreeCoTaskMem(buffer); Marshal.FreeCoTaskMem(intptrStruct); // Throw an exception if this operation failed. if (!bReturn) { throw new ApplicationException(" Set Internet Option Failed!"); } // Notify the system that the registry settings have been changed and cause // the proxy data to be reread from the registry for a handle. InternetSetOption( hInternet, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); InternetSetOption( hInternet, INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); return(bReturn); }
/// <summary> /// Set the proxy server for LAN connection. /// </summary> public override bool SetSystemProxy(SystemProxy sp) { //IntPtr hInternet = InternetOpen(applicationName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3]; int flag = 0; if (sp.IsAutoDetect) { flag |= (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_DETECT; } if (sp.IsDirect) { flag |= (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_DIRECT; } if (sp.IsPac) { flag |= (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_PROXY_URL; } if (sp.IsProxy) { flag |= (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; } // Set PROXY flags. Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[0].Value.dwValue = flag; // Set proxy name. Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(sp.ProxyAddress); // Set proxy pac. Options[2] = new INTERNET_PER_CONN_OPTION(); Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_AUTOCONFIG_URL; Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi(sp.PacPath); System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2])); System.IntPtr current = buffer; // Marshal data from a managed object to an unmanaged block of memory. for (int i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i])); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST(); // Point to the allocated memory. option_list.pOptions = buffer; // Return the unmanaged size of an object in bytes. option_list.Size = Marshal.SizeOf(option_list); // IntPtr.Zero means LAN connection. option_list.Connection = IntPtr.Zero; option_list.OptionCount = Options.Length; option_list.OptionError = 0; int size = Marshal.SizeOf(option_list); // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance. IntPtr intptrStruct = Marshal.AllocCoTaskMem(size); // Marshal data from a managed object to an unmanaged block of memory. Marshal.StructureToPtr(option_list, intptrStruct, true); // Set internet settings. bool bReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size); // Free the allocated memory. Marshal.FreeCoTaskMem(buffer); Marshal.FreeCoTaskMem(intptrStruct); //InternetCloseHandle(hInternet); // Throw an exception if this operation failed. if (!bReturn) { throw new ApplicationException(" Set Internet Option Failed!"); } return(bReturn); }
/// <summary> /// Set the proxy server for LAN connection. /// </summary> public static void Enable(string proxyServer) { IntPtr internetHandle = IntPtr.Zero; internetHandle = InternetOpen(AGENT, INTERNET_OPEN_TYPE_DIRECT, null, null, 0); if (internetHandle == IntPtr.Zero) { throw new FlashpointProxyException("Could not open the Internet Handle."); } // initialize a INTERNET_PER_CONN_OPTION_LIST instance INTERNET_PER_CONN_OPTION_LIST internetPerConnOptionList = new INTERNET_PER_CONN_OPTION_LIST(); int internetPerConnOptionListSize = Marshal.SizeOf(internetPerConnOptionList); // create two options INTERNET_PER_CONN_OPTION[] internetPerConnOptionListOptions = new INTERNET_PER_CONN_OPTION[2]; // set PROXY flags internetPerConnOptionListOptions[0] = new INTERNET_PER_CONN_OPTION(); internetPerConnOptionListOptions[0].Option = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; internetPerConnOptionListOptions[0].Value.Value = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; // set proxy name internetPerConnOptionListOptions[1] = new INTERNET_PER_CONN_OPTION(); internetPerConnOptionListOptions[1].Option = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; internetPerConnOptionListOptions[1].Value.ValuePointer = Marshal.StringToHGlobalAnsi(proxyServer); // allocate memory for the INTERNET_PER_CONN_OPTION_LIST Options internetPerConnOptionList.OptionsPointer = Marshal.AllocCoTaskMem(Marshal.SizeOf(internetPerConnOptionListOptions[0]) + Marshal.SizeOf(internetPerConnOptionListOptions[1])); System.IntPtr internetPerConnOptionListOptionPointer = internetPerConnOptionList.OptionsPointer; // marshal data from a managed object to unmanaged memory for (int i = 0; i < internetPerConnOptionListOptions.Length; i++) { Marshal.StructureToPtr(internetPerConnOptionListOptions[i], internetPerConnOptionListOptionPointer, false); internetPerConnOptionListOptionPointer = (System.IntPtr)((long)internetPerConnOptionListOptionPointer + Marshal.SizeOf(internetPerConnOptionListOptions[i])); } // fill the internetPerConnOptionList structure internetPerConnOptionList.Size = Marshal.SizeOf(internetPerConnOptionList); // NULL == LAN, otherwise connectoid name internetPerConnOptionList.Connection = IntPtr.Zero; // set two options internetPerConnOptionList.OptionCount = internetPerConnOptionListOptions.Length; internetPerConnOptionList.OptionError = 0; // allocate memory for the INTERNET_PER_CONN_OPTION_LIST IntPtr internetPerConnOptionListPointer = Marshal.AllocCoTaskMem(internetPerConnOptionListSize); // marshal data from a managed object to unmanaged memory Marshal.StructureToPtr(internetPerConnOptionList, internetPerConnOptionListPointer, true); // set the options on the connection bool result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, internetPerConnOptionListPointer, internetPerConnOptionListSize); // free the allocated memory Marshal.FreeCoTaskMem(internetPerConnOptionList.OptionsPointer); Marshal.FreeCoTaskMem(internetPerConnOptionListPointer); if (!InternetCloseHandle(internetHandle)) { throw new FlashpointProxyException("Could not close the Internet Handle."); } // throw an exception if this operation failed if (!result) { throw new FlashpointProxyException("Could not set the Internet Options."); } }
/// <summary> /// Backup the current options for LAN connection. /// Make sure free the memory after restoration. /// </summary> private static INTERNET_PER_CONN_OPTION_LIST GetSystemProxy() { // Query following options. INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3]; Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[2] = new INTERNET_PER_CONN_OPTION(); Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS; // Allocate a block of memory of the options. System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2])); System.IntPtr current = (System.IntPtr)buffer; // Marshal data from a managed object to an unmanaged block of memory. for (int i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i])); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. INTERNET_PER_CONN_OPTION_LIST Request = new INTERNET_PER_CONN_OPTION_LIST(); // Point to the allocated memory. Request.pOptions = buffer; Request.Size = Marshal.SizeOf(Request); // IntPtr.Zero means LAN connection. Request.Connection = IntPtr.Zero; Request.OptionCount = Options.Length; Request.OptionError = 0; int size = Marshal.SizeOf(Request); // Query internet options. bool result = InternetQueryOptionList(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, ref Request, ref size); if (!result) { throw new ApplicationException(" Set Internet Option Failed! "); } return Request; }
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, INTERNET_PER_CONN_OPTION_LIST lpBuffer, int dwBufferLength);
/// <summary> /// Backup the current options for LAN connection. /// Make sure free the memory after restoration. /// </summary> public override SystemProxy GetSystemProxy() { // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. INTERNET_PER_CONN_OPTION_LIST Request = new INTERNET_PER_CONN_OPTION_LIST(); // Query following options. INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[4]; Options[0] = new INTERNET_PER_CONN_OPTION(); Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS; Options[1] = new INTERNET_PER_CONN_OPTION(); Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER; Options[2] = new INTERNET_PER_CONN_OPTION(); Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS; Options[3] = new INTERNET_PER_CONN_OPTION(); Options[3].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_AUTOCONFIG_URL; // Allocate a block of memory of the options. System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]) + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]) + Marshal.SizeOf(Options[3])); System.IntPtr current = (System.IntPtr)buffer; // Marshal data from a managed object to an unmanaged block of memory. for (int i = 0; i < Options.Length; i++) { Marshal.StructureToPtr(Options[i], current, false); current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i])); } // Point to the allocated memory. Request.pOptions = buffer; Request.Size = Marshal.SizeOf(Request); // IntPtr.Zero means LAN connection. Request.Connection = IntPtr.Zero; Request.OptionCount = Options.Length; Request.OptionError = 0; int size = Marshal.SizeOf(Request); // Query internet options.(buggy0 bool result = InternetQueryOptionList(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, ref Request, ref size); if (!result) { throw new ApplicationException(" Set Internet Option Failed! "); } current = buffer; for (int i = 0; i < Options.Length; i++) { Options[i] = (INTERNET_PER_CONN_OPTION)Marshal.PtrToStructure(current, typeof(INTERNET_PER_CONN_OPTION)); current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i])); } SystemProxy sp = new SystemProxy(); sp.IsDirect = (Options[0].Value.dwValue & (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_DIRECT) == (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_DIRECT; sp.IsProxy = (Options[0].Value.dwValue & (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY) == (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY; sp.IsAutoDetect = (Options[0].Value.dwValue & (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_DETECT) == (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_DETECT; sp.IsPac = (Options[0].Value.dwValue & (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_PROXY_URL) == (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_PROXY_URL; sp.PacPath = Marshal.PtrToStringAnsi(Options[3].Value.pszValue); sp.ProxyAddress = Marshal.PtrToStringAnsi(Options[1].Value.pszValue); Marshal.FreeCoTaskMem(Request.pOptions); return(sp); }
static extern bool InternetQueryOptionList(IntPtr handle, INTERNET_OPTION optionFlag, ref INTERNET_PER_CONN_OPTION_LIST optionList, ref int size);
/// <summary> /// Set the system proxy, as per the 'Internet Options' panel /// </summary> /// <returns>True if the operation was successful</returns> public static bool SetProxy(bool ProxyEnable, string ProxyServer, string ProxyBypass) { var options = new INTERNET_PER_CONN_OPTION[3]; // Option #1: ProxyEnable options[0].dwOption = INTERNET_PER_CONN.FLAGS; options[0].Value.i = (int) (ProxyEnable ? (PER_CONN_FLAGS.PROXY_TYPE_DIRECT | PER_CONN_FLAGS.PROXY_TYPE_PROXY) : PER_CONN_FLAGS.PROXY_TYPE_DIRECT); // Option #2: ProxyServer options[1].dwOption = INTERNET_PER_CONN.PROXY_SERVER; options[1].Value.iptr = Marshal.StringToHGlobalAuto( ProxyServer); // Option #3: ProxyBypass options[2].dwOption = INTERNET_PER_CONN.PROXY_BYPASS; options[2].Value.iptr = Marshal.StringToHGlobalAuto( ProxyBypass); var list = new INTERNET_PER_CONN_OPTION_LIST(); list.dwSize = Marshal.SizeOf(list); list.pszConnection = IntPtr.Zero; // change globally list.dwOptionCount = options.Length; list.dwOptionError = 0; // Marshall each option in options int optSize = Marshal.SizeOf(typeof(INTERNET_PER_CONN_OPTION)); IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length); for (int i = 0; i < options.Length; ++i) { var opt = new IntPtr(optionsPtr.ToInt32() + (i * optSize)); Marshal.StructureToPtr(options[i], opt, false); } list.pOptions = optionsPtr; // and then make a pointer out of the whole list IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)list.dwSize); Marshal.StructureToPtr(list, ipcoListPtr, false); // Finally, call the InternetSetOption API int res = InternetSetOption(IntPtr.Zero, INTERNET_OPTION.PER_CONNECTION_OPTION, ipcoListPtr, list.dwSize) ? -1 : 0; if (res == 0) res = Marshal.GetLastWin32Error(); Marshal.FreeCoTaskMem(optionsPtr); Marshal.FreeCoTaskMem(ipcoListPtr); if (res > 0) throw new Win32Exception(Marshal.GetLastWin32Error()); return (res < 0); }
/// <summary> /// Set the system proxy, as per the 'Internet Options' panel /// </summary> /// <returns>True if the operation was successful</returns> public static bool SetProxy(bool ProxyEnable, string ProxyServer, string ProxyBypass) { var options = new INTERNET_PER_CONN_OPTION[3]; // Option #1: ProxyEnable options[0].dwOption = INTERNET_PER_CONN.FLAGS; options[0].Value.i = (int)(ProxyEnable ? (PER_CONN_FLAGS.PROXY_TYPE_DIRECT | PER_CONN_FLAGS.PROXY_TYPE_PROXY) : PER_CONN_FLAGS.PROXY_TYPE_DIRECT); // Option #2: ProxyServer options[1].dwOption = INTERNET_PER_CONN.PROXY_SERVER; options[1].Value.iptr = Marshal.StringToHGlobalAuto( ProxyServer); // Option #3: ProxyBypass options[2].dwOption = INTERNET_PER_CONN.PROXY_BYPASS; options[2].Value.iptr = Marshal.StringToHGlobalAuto( ProxyBypass); var list = new INTERNET_PER_CONN_OPTION_LIST(); list.dwSize = Marshal.SizeOf(list); list.pszConnection = IntPtr.Zero; // change globally list.dwOptionCount = options.Length; list.dwOptionError = 0; // Marshall each option in options int optSize = Marshal.SizeOf(typeof(INTERNET_PER_CONN_OPTION)); IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length); for (int i = 0; i < options.Length; ++i) { var opt = new IntPtr(optionsPtr.ToInt32() + (i * optSize)); Marshal.StructureToPtr(options[i], opt, false); } list.pOptions = optionsPtr; // and then make a pointer out of the whole list IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)list.dwSize); Marshal.StructureToPtr(list, ipcoListPtr, false); // Finally, call the InternetSetOption API int res = InternetSetOption(IntPtr.Zero, INTERNET_OPTION.PER_CONNECTION_OPTION, ipcoListPtr, list.dwSize) ? -1 : 0; if (res == 0) { res = Marshal.GetLastWin32Error(); } Marshal.FreeCoTaskMem(optionsPtr); Marshal.FreeCoTaskMem(ipcoListPtr); if (res > 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(res < 0); }