예제 #1
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);
        }
예제 #2
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);
        }