public InternetConnectionOption GetIEProxy(PerConnOption perConnOption) { InternetPerConnOptionList InternetPerConnList = new InternetPerConnOptionList(); int listSize = Marshal.SizeOf(InternetPerConnList); InternetPerConnList.dwSize = Marshal.SizeOf(InternetPerConnList); InternetPerConnList.dwOptionCount = 1; InternetPerConnList.dwOptionError = 0; InternetPerConnList.szConnection = IntPtr.Zero; InternetConnectionOption ico = new InternetConnectionOption(); GCHandle gch = GCHandle.Alloc(ico, GCHandleType.Pinned); ico.m_Option = perConnOption; int icoSize = Marshal.SizeOf(ico); InternetPerConnList.options = Marshal.AllocCoTaskMem(icoSize); IntPtr optionListPtr = InternetPerConnList.options; Marshal.StructureToPtr(ico, optionListPtr, false); if (NativeMethods.InternetQueryOption(IntPtr.Zero, 75, ref InternetPerConnList, ref listSize) == true) { ico = (InternetConnectionOption)Marshal.PtrToStructure(InternetPerConnList.options, typeof(InternetConnectionOption)); } Marshal.FreeCoTaskMem(InternetPerConnList.options); gch.Free(); return(ico); }
public void SetIEProxy(string proxyStr, string exceptions) { int optionCount = 1; InternetConnectionOption[] options = new InternetConnectionOption[3]; options[0].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS; if (string.IsNullOrEmpty(proxyStr)) { options[0].m_Value.m_Int = (int)PerConnFlags.PROXY_TYPE_DIRECT; } else { options[0].m_Value.m_Int = (int)(PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY); } if (!string.IsNullOrEmpty(proxyStr)) { options[1].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVER; options[1].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(proxyStr); optionCount = 2; if (!string.IsNullOrEmpty(exceptions)) { options[2].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS; if (exceptions == "null") { options[2].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(null); } else { options[2].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(exceptions); } optionCount = 3; } } InternetPerConnOptionList InternetPerConnList = new InternetPerConnOptionList(); InternetPerConnList.dwSize = Marshal.SizeOf(InternetPerConnList); InternetPerConnList.dwOptionError = 0; InternetPerConnList.szConnection = IntPtr.Zero; int optSize = Marshal.SizeOf(typeof(InternetConnectionOption)); IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * optionCount); for (int i = 0; i < optionCount; ++i) { if (options[i].m_Option > 0) { IntPtr opt = new IntPtr(optionsPtr.ToInt32() + (i * optSize)); Marshal.StructureToPtr(options[i], opt, false); } } InternetPerConnList.dwOptionCount = optionCount; InternetPerConnList.options = optionsPtr; IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)InternetPerConnList.dwSize); Marshal.StructureToPtr(InternetPerConnList, ipcoListPtr, false); NativeMethods.InternetSetOption(IntPtr.Zero, InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION, ipcoListPtr, InternetPerConnList.dwSize); Marshal.FreeCoTaskMem(optionsPtr); Marshal.FreeCoTaskMem(ipcoListPtr); }
public static extern bool InternetQueryOption(IntPtr hInternet, int dwOption, ref InternetPerConnOptionList optionsList, ref int bufferLength);