Exemple #1
0
        private static bool SetSystemProxySettings(NativeMethods.InternetPerConnectionOptionList proxySettings)
        {
            int size = Marshal.SizeOf(proxySettings);

            // Allocate memory.
            IntPtr proxySettingBufferPointer = Marshal.AllocCoTaskMem(size);

            // Convert structure to IntPtr
            Marshal.StructureToPtr(proxySettings, proxySettingBufferPointer, true);

            // Set internet options.
            bool bReturn = NativeMethods.InternetSetOption(IntPtr.Zero, NativeMethods.InternetOption.PerConnectionOption, proxySettingBufferPointer, size);

            // Free the allocated memory.
            Marshal.FreeCoTaskMem(proxySettings.OptionsBufferPointer);
            Marshal.FreeCoTaskMem(proxySettingBufferPointer);

            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.
            NativeMethods.InternetSetOption(IntPtr.Zero, NativeMethods.InternetOption.SettingsChanged, IntPtr.Zero, 0);
            NativeMethods.InternetSetOption(IntPtr.Zero, NativeMethods.InternetOption.Refresh, IntPtr.Zero, 0);

            return(bReturn);
        }
Exemple #2
0
        private static NativeMethods.InternetPerConnectionOptionList GetSystemProxySettings()
        {
            // Query following options.
            NativeMethods.InternetPerConnectionOption[] options = new NativeMethods.InternetPerConnectionOption[3];

            options[0]        = new NativeMethods.InternetPerConnectionOption();
            options[0].Option = (int)NativeMethods.InternetPerConnectionOptionValue.Flags;
            options[1]        = new NativeMethods.InternetPerConnectionOption();
            options[1].Option = (int)NativeMethods.InternetPerConnectionOptionValue.ProxyServer;
            options[2]        = new NativeMethods.InternetPerConnectionOption();
            options[2].Option = (int)NativeMethods.InternetPerConnectionOptionValue.ProxyBypass;

            // Allocate a block of memory of the options.
            IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(options[0]) + Marshal.SizeOf(options[1]) + Marshal.SizeOf(options[2]));

            IntPtr current = (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 = (IntPtr)((int)current + Marshal.SizeOf(options[i]));
            }

            // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
            NativeMethods.InternetPerConnectionOptionList proxySettings = new NativeMethods.InternetPerConnectionOptionList();

            // Point to the allocated memory.
            proxySettings.OptionsBufferPointer = buffer;

            proxySettings.Size = Marshal.SizeOf(proxySettings);

            // IntPtr.Zero means LAN connection.
            proxySettings.Connection = IntPtr.Zero;

            proxySettings.OptionCount = options.Length;
            proxySettings.OptionError = 0;
            int size = Marshal.SizeOf(proxySettings);

            // Query internet options.
            bool result = NativeMethods.InternetQueryOptionList(IntPtr.Zero, NativeMethods.InternetOption.PerConnectionOption, ref proxySettings, ref size);

            if (!result)
            {
                throw new ApplicationException(" Set Internet Option Failed! ");
            }

            return(proxySettings);
        }
Exemple #3
0
        private static NativeMethods.InternetPerConnectionOptionList CreateCustomProxySettings(string proxyString, string proxyBypassString)
        {
            // Create 3 options.
            NativeMethods.InternetPerConnectionOption[] options = new NativeMethods.InternetPerConnectionOption[3];

            // Set PROXY flags.
            options[0]                    = new NativeMethods.InternetPerConnectionOption();
            options[0].Option             = (int)NativeMethods.InternetPerConnectionOptionValue.Flags;
            options[0].Value.integerValue = (int)NativeMethods.InternetPerConnectionProxyFlags.Proxy;

            // Set proxy name.
            options[1]                   = new NativeMethods.InternetPerConnectionOption();
            options[1].Option            = (int)NativeMethods.InternetPerConnectionOptionValue.ProxyServer;
            options[1].Value.stringValue = Marshal.StringToHGlobalAnsi(proxyString);

            // Set proxy bypass.
            options[2]                   = new NativeMethods.InternetPerConnectionOption();
            options[2].Option            = (int)NativeMethods.InternetPerConnectionOptionValue.ProxyBypass;
            options[2].Value.stringValue = Marshal.StringToHGlobalAnsi(proxyBypassString);

            // Allocate a block of memory of the options.
            IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(options[0]) + Marshal.SizeOf(options[1]) + Marshal.SizeOf(options[2]));

            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]));
            }

            // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
            NativeMethods.InternetPerConnectionOptionList proxySettings = new NativeMethods.InternetPerConnectionOptionList();

            // Point to the allocated memory.
            proxySettings.OptionsBufferPointer = buffer;

            // Return the unmanaged size of an object in bytes.
            proxySettings.Size = Marshal.SizeOf(proxySettings);

            // IntPtr.Zero means LAN connection.
            proxySettings.Connection = IntPtr.Zero;

            proxySettings.OptionCount = options.Length;
            proxySettings.OptionError = 0;
            return(proxySettings);
        }