Пример #1
0
        /// <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);
        }
Пример #2
0
 public abstract bool SetSystemProxy(SystemProxy sp);
Пример #3
0
        /// <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);
        }