PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSWebBrowserWithProxy/WinINet.cs

#
C# | 323 lines | 192 code | 53 blank | 78 comment | 13 complexity | dd0642db05b6c75eb48a1c9e1a6cc61e MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. Module Name: WinINet.cs
  3. Project: CSWebBrowserWithProxy
  4. Copyright (c) Microsoft Corporation.
  5. This class is used to set the proxy. or restore to the system proxy for the
  6. current application
  7. This source is subject to the Microsoft Public License.
  8. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  9. All other rights reserved.
  10. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  11. EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  12. WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  13. \***************************************************************************/
  14. using System;
  15. using System.Runtime.InteropServices;
  16. using System.Diagnostics;
  17. namespace CSWebBrowserWithProxy
  18. {
  19. public static class WinINet
  20. {
  21. static string agent = Process.GetCurrentProcess().ProcessName;
  22. /// <summary>
  23. /// Set the LAN connection proxy server for current process.
  24. /// </summary>
  25. /// <param name="proxyServer">
  26. /// The Proxy Server.
  27. /// </param>
  28. /// <returns></returns>
  29. public static bool SetConnectionProxy(bool isMachineSetting, string proxyServer)
  30. {
  31. if (isMachineSetting)
  32. {
  33. return SetConnectionProxy(null, proxyServer);
  34. }
  35. else
  36. {
  37. return SetConnectionProxy(agent, proxyServer);
  38. }
  39. }
  40. /// <summary>
  41. /// Set the LAN connection proxy server.
  42. /// </summary>
  43. /// <param name="agentName">
  44. /// If agentName is null or empty, this function will set the Lan proxy for
  45. /// the machine, else for the current process.
  46. /// </param>
  47. /// <param name="proxyServer">The Proxy Server.</param>
  48. /// <returns></returns>
  49. public static bool SetConnectionProxy(string agentName, string proxyServer)
  50. {
  51. IntPtr hInternet = IntPtr.Zero;
  52. try
  53. {
  54. if (!string.IsNullOrEmpty(agentName))
  55. {
  56. hInternet = NativeMethods.InternetOpen(
  57. agentName,
  58. (int)INTERNET_OPEN_TYPE.INTERNET_OPEN_TYPE_DIRECT,
  59. null,
  60. null,
  61. 0);
  62. }
  63. return SetConnectionProxyInternal(hInternet, proxyServer);
  64. }
  65. finally
  66. {
  67. if (hInternet != IntPtr.Zero)
  68. {
  69. NativeMethods.InternetCloseHandle(hInternet);
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// Set the proxy server for LAN connection.
  75. /// </summary>
  76. static bool SetConnectionProxyInternal(IntPtr hInternet, string proxyServer)
  77. {
  78. // Create 3 options.
  79. INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3];
  80. // Set PROXY flags.
  81. Options[0] = new INTERNET_PER_CONN_OPTION();
  82. Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
  83. Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY;
  84. // Set proxy name.
  85. Options[1] = new INTERNET_PER_CONN_OPTION();
  86. Options[1].dwOption =
  87. (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER;
  88. Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer);
  89. // Set proxy bypass.
  90. Options[2] = new INTERNET_PER_CONN_OPTION();
  91. Options[2].dwOption =
  92. (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS;
  93. Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi("local");
  94. // Allocate a block of memory of the options.
  95. System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
  96. + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]));
  97. System.IntPtr current = buffer;
  98. // Marshal data from a managed object to an unmanaged block of memory.
  99. for (int i = 0; i < Options.Length; i++)
  100. {
  101. Marshal.StructureToPtr(Options[i], current, false);
  102. current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
  103. }
  104. // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
  105. INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST();
  106. // Point to the allocated memory.
  107. option_list.pOptions = buffer;
  108. // Return the unmanaged size of an object in bytes.
  109. option_list.Size = Marshal.SizeOf(option_list);
  110. // IntPtr.Zero means LAN connection.
  111. option_list.Connection = IntPtr.Zero;
  112. option_list.OptionCount = Options.Length;
  113. option_list.OptionError = 0;
  114. int size = Marshal.SizeOf(option_list);
  115. // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance.
  116. IntPtr intptrStruct = Marshal.AllocCoTaskMem(size);
  117. // Marshal data from a managed object to an unmanaged block of memory.
  118. Marshal.StructureToPtr(option_list, intptrStruct, true);
  119. // Set internet settings.
  120. bool bReturn = NativeMethods.InternetSetOption(
  121. hInternet,
  122. INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
  123. intptrStruct, size);
  124. // Free the allocated memory.
  125. Marshal.FreeCoTaskMem(buffer);
  126. Marshal.FreeCoTaskMem(intptrStruct);
  127. // Throw an exception if this operation failed.
  128. if (!bReturn)
  129. {
  130. throw new ApplicationException(" Set Internet Option Failed!");
  131. }
  132. // Notify the system that the registry settings have been changed and cause
  133. // the proxy data to be reread from the registry for a handle.
  134. NativeMethods.InternetSetOption(
  135. hInternet,
  136. INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED,
  137. IntPtr.Zero, 0);
  138. NativeMethods.InternetSetOption(
  139. hInternet,
  140. INTERNET_OPTION.INTERNET_OPTION_REFRESH,
  141. IntPtr.Zero, 0);
  142. return bReturn;
  143. }
  144. /// <summary>
  145. /// Get the current system options for LAN connection.
  146. /// Make sure free the memory after restoration.
  147. /// </summary>
  148. public static INTERNET_PER_CONN_OPTION_LIST GetSystemProxy()
  149. {
  150. // Query following options.
  151. INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3];
  152. Options[0] = new INTERNET_PER_CONN_OPTION();
  153. Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
  154. Options[1] = new INTERNET_PER_CONN_OPTION();
  155. Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER;
  156. Options[2] = new INTERNET_PER_CONN_OPTION();
  157. Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS;
  158. // Allocate a block of memory of the options.
  159. System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
  160. + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]));
  161. System.IntPtr current = (System.IntPtr)buffer;
  162. // Marshal data from a managed object to an unmanaged block of memory.
  163. for (int i = 0; i < Options.Length; i++)
  164. {
  165. Marshal.StructureToPtr(Options[i], current, false);
  166. current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
  167. }
  168. // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
  169. INTERNET_PER_CONN_OPTION_LIST Request = new INTERNET_PER_CONN_OPTION_LIST();
  170. // Point to the allocated memory.
  171. Request.pOptions = buffer;
  172. Request.Size = Marshal.SizeOf(Request);
  173. // IntPtr.Zero means LAN connection.
  174. Request.Connection = IntPtr.Zero;
  175. Request.OptionCount = Options.Length;
  176. Request.OptionError = 0;
  177. int size = Marshal.SizeOf(Request);
  178. // Query system internet options.
  179. bool result = NativeMethods.InternetQueryOption(
  180. IntPtr.Zero,
  181. INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
  182. ref Request,
  183. ref size);
  184. if (!result)
  185. {
  186. throw new ApplicationException("Get System Internet Option Failed! ");
  187. }
  188. return Request;
  189. }
  190. /// <summary>
  191. /// Restore to the system proxy settings.
  192. /// </summary>
  193. public static bool RestoreSystemProxy()
  194. {
  195. return RestoreSystemProxy(agent);
  196. }
  197. /// <summary>
  198. /// Restore to the system proxy settings.
  199. /// </summary>
  200. public static bool RestoreSystemProxy(string agentName)
  201. {
  202. if (string.IsNullOrEmpty(agentName))
  203. {
  204. throw new ArgumentNullException("Agent name cannot be null or empty!");
  205. }
  206. IntPtr hInternet = IntPtr.Zero;
  207. try
  208. {
  209. if (!string.IsNullOrEmpty(agentName))
  210. {
  211. hInternet = NativeMethods.InternetOpen(
  212. agentName,
  213. (int)INTERNET_OPEN_TYPE.INTERNET_OPEN_TYPE_DIRECT,
  214. null,
  215. null,
  216. 0);
  217. }
  218. return RestoreSystemProxyInternal(hInternet);
  219. }
  220. finally
  221. {
  222. if (hInternet != IntPtr.Zero)
  223. {
  224. NativeMethods.InternetCloseHandle(hInternet);
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// Restore to the system proxy settings.
  230. /// </summary>
  231. static bool RestoreSystemProxyInternal(IntPtr hInternet)
  232. {
  233. var request = GetSystemProxy();
  234. int size = Marshal.SizeOf(request);
  235. // Allocate memory.
  236. IntPtr intptrStruct = Marshal.AllocCoTaskMem(size);
  237. // Convert structure to IntPtr
  238. Marshal.StructureToPtr(request, intptrStruct, true);
  239. // Set internet options.
  240. bool bReturn = NativeMethods.InternetSetOption(
  241. hInternet,
  242. INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
  243. intptrStruct,
  244. size);
  245. // Free the allocated memory.
  246. Marshal.FreeCoTaskMem(request.pOptions);
  247. Marshal.FreeCoTaskMem(intptrStruct);
  248. if (!bReturn)
  249. {
  250. throw new ApplicationException(" Set Internet Option Failed! ");
  251. }
  252. // Notify the system that the registry settings have been changed and cause
  253. // the proxy data to be reread from the registry for a handle.
  254. NativeMethods.InternetSetOption(
  255. hInternet,
  256. INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED,
  257. IntPtr.Zero,
  258. 0);
  259. NativeMethods.InternetSetOption(
  260. hInternet,
  261. INTERNET_OPTION.INTERNET_OPTION_REFRESH,
  262. IntPtr.Zero,
  263. 0);
  264. return bReturn;
  265. }
  266. }
  267. }