Advertisement
rnqqpp

C# Proxy Setting

Mar 24th, 2018
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.42 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. namespace SetProxy
  5. {
  6.     class WinInetInterop
  7.     {
  8.         [DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
  9.         private static extern IntPtr InternetOpen(
  10.             string lpszAgent, int dwAccessType, string lpszProxyName,
  11.             string lpszProxyBypass, int dwFlags);
  12.  
  13.         [DllImport("wininet.dll", SetLastError = true)]
  14.         [return: MarshalAs(UnmanagedType.Bool)]
  15.         private static extern bool InternetCloseHandle(IntPtr hInternet);
  16.  
  17.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  18.         private struct INTERNET_PER_CONN_OPTION_LIST
  19.         {
  20.             public int Size;
  21.  
  22.             public System.IntPtr Connection;
  23.  
  24.             public int OptionCount;
  25.             public int OptionError;
  26.            
  27.             public System.IntPtr pOptions;
  28.         }
  29.         private enum INTERNET_OPTION
  30.         {
  31.             INTERNET_OPTION_PER_CONNECTION_OPTION = 75,
  32.             INTERNET_OPTION_SETTINGS_CHANGED = 39,
  33.             INTERNET_OPTION_REFRESH = 37
  34.         }
  35.  
  36.         private enum INTERNET_PER_CONN_OptionEnum
  37.         {
  38.             INTERNET_PER_CONN_FLAGS = 1,
  39.             INTERNET_PER_CONN_PROXY_SERVER = 2,
  40.             INTERNET_PER_CONN_PROXY_BYPASS = 3,
  41.             INTERNET_PER_CONN_AUTOCONFIG_URL = 4,
  42.             INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5,
  43.             INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL = 6,
  44.             INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS = 7,
  45.             INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME = 8,
  46.             INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL = 9,
  47.             INTERNET_PER_CONN_FLAGS_UI = 10
  48.         }
  49.         private const int INTERNET_OPEN_TYPE_DIRECT = 1;  // direct to net
  50.         private const int INTERNET_OPEN_TYPE_PRECONFIG = 0; // read registry
  51.  
  52.         private enum INTERNET_OPTION_PER_CONN_FLAGS
  53.         {
  54.             PROXY_TYPE_DIRECT = 0x00000001,   // direct to net
  55.             PROXY_TYPE_PROXY = 0x00000002,   // via named proxy
  56.             PROXY_TYPE_AUTO_PROXY_URL = 0x00000004,   // autoproxy URL
  57.             PROXY_TYPE_AUTO_DETECT = 0x00000008   // use autoproxy detection
  58.         }
  59.  
  60.         [StructLayout(LayoutKind.Explicit)]
  61.         private struct INTERNET_PER_CONN_OPTION_OptionUnion
  62.         {
  63.             [FieldOffset(0)]
  64.             public int dwValue;
  65.             [FieldOffset(0)]
  66.             public System.IntPtr pszValue;
  67.             [FieldOffset(0)]
  68.             public System.Runtime.InteropServices.ComTypes.FILETIME ftValue;
  69.         }
  70.  
  71.         [StructLayout(LayoutKind.Sequential)]
  72.         private struct INTERNET_PER_CONN_OPTION
  73.         {
  74.             public int dwOption;
  75.             public INTERNET_PER_CONN_OPTION_OptionUnion Value;
  76.         }
  77.  
  78.         [DllImport("wininet.dll", CharSet = CharSet.Ansi, SetLastError = true)]
  79.         private static extern bool InternetSetOption(
  80.             IntPtr hInternet,
  81.             INTERNET_OPTION dwOption,
  82.             IntPtr lpBuffer,
  83.             int lpdwBufferLength);
  84.  
  85.         [DllImport("wininet.dll", CharSet = CharSet.Ansi, SetLastError = true,
  86.             EntryPoint = "InternetQueryOption")]
  87.         private extern static bool InternetQueryOptionList(
  88.             IntPtr Handle,
  89.             INTERNET_OPTION OptionFlag,
  90.             ref INTERNET_PER_CONN_OPTION_LIST OptionList,
  91.             ref int size);
  92.  
  93.         public static bool SetConnectionProxy(string proxyServer)
  94.         {
  95.             IntPtr hInternet = InternetOpen(null, INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
  96.  
  97.             //// Create 3 options.
  98.             //INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3];
  99.  
  100.             // Create 2 options.
  101.             INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[2];
  102.  
  103.             // Set PROXY flags.
  104.             Options[0] = new INTERNET_PER_CONN_OPTION();
  105.             Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
  106.             Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY;
  107.  
  108.             // Set proxy name.
  109.             Options[1] = new INTERNET_PER_CONN_OPTION();
  110.             Options[1].dwOption =
  111.                 (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER;
  112.             Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer);
  113.  
  114.             //// Set proxy bypass.
  115.             //Options[2] = new INTERNET_PER_CONN_OPTION();
  116.             //Options[2].dwOption =
  117.             //    (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS;
  118.             //Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi("local");
  119.  
  120.             //// Allocate a block of memory of the options.
  121.             //System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
  122.             //    + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]));
  123.  
  124.             // Allocate a block of memory of the options.
  125.             System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
  126.                 + Marshal.SizeOf(Options[1]));
  127.  
  128.             System.IntPtr current = buffer;
  129.            
  130.             for (int i = 0; i < Options.Length; i++)
  131.             {
  132.                 Marshal.StructureToPtr(Options[i], current, false);
  133.                 current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
  134.             }
  135.            
  136.             INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST();
  137.            
  138.             option_list.pOptions = buffer;
  139.            
  140.             option_list.Size = Marshal.SizeOf(option_list);
  141.            
  142.             option_list.Connection = IntPtr.Zero;
  143.  
  144.             option_list.OptionCount = Options.Length;
  145.             option_list.OptionError = 0;
  146.             int size = Marshal.SizeOf(option_list);
  147.            
  148.             IntPtr intptrStruct = Marshal.AllocCoTaskMem(size);
  149.            
  150.             Marshal.StructureToPtr(option_list, intptrStruct, true);
  151.            
  152.             bool bReturn = InternetSetOption(hInternet,
  153.                 INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size);
  154.            
  155.             Marshal.FreeCoTaskMem(buffer);
  156.             Marshal.FreeCoTaskMem(intptrStruct);
  157.             InternetCloseHandle(hInternet);
  158.            
  159.             if (!bReturn)
  160.             {
  161.                 throw new ApplicationException(" Set Internet Option Failed!");
  162.             }
  163.  
  164.             return bReturn;
  165.         }
  166.  
  167.         private static INTERNET_PER_CONN_OPTION_LIST GetSystemProxy()
  168.         {
  169.             INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3];
  170.  
  171.             Options[0] = new INTERNET_PER_CONN_OPTION();
  172.             Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
  173.             Options[1] = new INTERNET_PER_CONN_OPTION();
  174.             Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER;
  175.             Options[2] = new INTERNET_PER_CONN_OPTION();
  176.             Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS;
  177.            
  178.             System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
  179.                 + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]));
  180.  
  181.             System.IntPtr current = (System.IntPtr)buffer;
  182.             for (int i = 0; i < Options.Length; i++)
  183.             {
  184.                 Marshal.StructureToPtr(Options[i], current, false);
  185.                 current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
  186.             }
  187.  
  188.             INTERNET_PER_CONN_OPTION_LIST Request = new INTERNET_PER_CONN_OPTION_LIST();
  189.             Request.pOptions = buffer;
  190.             Request.Size = Marshal.SizeOf(Request);
  191.             Request.Connection = IntPtr.Zero;
  192.             Request.OptionCount = Options.Length;
  193.             Request.OptionError = 0;
  194.             int size = Marshal.SizeOf(Request);
  195.  
  196.             bool result = InternetQueryOptionList(IntPtr.Zero,
  197.                 INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
  198.                 ref Request, ref size);
  199.             if (!result)
  200.             {
  201.                 throw new ApplicationException(" Set Internet Option Failed! ");
  202.             }
  203.  
  204.             return Request;
  205.         }
  206.  
  207.         public static bool RestoreSystemProxy()
  208.         {
  209.             IntPtr hInternet = InternetOpen(null, INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
  210.  
  211.             INTERNET_PER_CONN_OPTION_LIST request = GetSystemProxy();
  212.             int size = Marshal.SizeOf(request);
  213.             IntPtr intptrStruct = Marshal.AllocCoTaskMem(size);
  214.             Marshal.StructureToPtr(request, intptrStruct, true);
  215.             bool bReturn = InternetSetOption(hInternet,
  216.                 INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
  217.                 intptrStruct, size);
  218.  
  219.             Marshal.FreeCoTaskMem(request.pOptions);
  220.             Marshal.FreeCoTaskMem(intptrStruct);
  221.  
  222.             if (!bReturn)
  223.             {
  224.                 throw new ApplicationException(" Set Internet Option Failed! ");
  225.             }
  226.            
  227.             InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED,
  228.                 IntPtr.Zero, 0);
  229.             InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_REFRESH,
  230.                 IntPtr.Zero, 0);
  231.  
  232.             InternetCloseHandle(hInternet);
  233.  
  234.             return bReturn;
  235.         }
  236.  
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement