Program.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7. using System.ComponentModel;
  8. namespace WebUIWrapper
  9. {
  10. [ComImport, GuidAttribute("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B"),
  11. InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
  12. public interface IInternetSecurityManager
  13. {
  14. [return: MarshalAs(UnmanagedType.I4)]
  15. [PreserveSig]
  16. int SetSecuritySite([In] IntPtr pSite);
  17. [return: MarshalAs(UnmanagedType.I4)]
  18. [PreserveSig]
  19. int GetSecuritySite([Out] IntPtr pSite);
  20. [return: MarshalAs(UnmanagedType.I4)]
  21. [PreserveSig]
  22. int MapUrlToZone([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, out UInt32 pdwZone, UInt32 dwFlags);
  23. [return: MarshalAs(UnmanagedType.I4)]
  24. [PreserveSig]
  25. int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId, ref UInt32 pcbSecurityId, uint dwReserved);
  26. [return: MarshalAs(UnmanagedType.I4)]
  27. [PreserveSig]
  28. int ProcessUrlAction([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, UInt32 dwAction, out byte pPolicy, UInt32 cbPolicy, byte pContext, UInt32 cbContext, UInt32 dwFlags, UInt32 dwReserved);
  29. [return: MarshalAs(UnmanagedType.I4)]
  30. [PreserveSig]
  31. int QueryCustomPolicy([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, ref Guid guidKey, ref byte ppPolicy, ref UInt32 pcbPolicy, ref byte pContext, UInt32 cbContext, UInt32 dwReserved);
  32. [return: MarshalAs(UnmanagedType.I4)]
  33. [PreserveSig]
  34. int SetZoneMapping(UInt32 dwZone, [In, MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, UInt32 dwFlags);
  35. [return: MarshalAs(UnmanagedType.I4)]
  36. [PreserveSig]
  37. int GetZoneMappings(UInt32 dwZone, out UCOMIEnumString ppenumString, UInt32 dwFlags);
  38. }
  39. static class Program
  40. {
  41. // constants from urlmon.h
  42. public const UInt32 URLZONE_LOCAL_MACHINE = 0;
  43. public const UInt32 URLZONE_INTRANET = URLZONE_LOCAL_MACHINE + 1;
  44. public const UInt32 URLZONE_TRUSTED = URLZONE_INTRANET + 1;
  45. public const UInt32 URLZONE_INTERNET = URLZONE_TRUSTED + 1;
  46. public const UInt32 URLZONE_UNTRUSTED = URLZONE_INTERNET + 1;
  47. public const UInt32 URLZONE_ESC_FLAG = 0x100;
  48. public const UInt32 SZM_CREATE = 0;
  49. public const UInt32 SZM_DELETE = 0x1;
  50. public static Guid CLSID_InternetSecurityManager = new Guid("7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4");
  51. public static Guid IID_IInternetSecurityManager = new Guid("79eac9ee-baf9-11ce-8c82-00aa004ba90b");
  52. /// <summary>
  53. /// The main entry point for the application.
  54. /// </summary>
  55. [STAThread]
  56. static void Main()
  57. {
  58. try
  59. {
  60. Type t = Type.GetTypeFromCLSID(CLSID_InternetSecurityManager);
  61. object securityManager = Activator.CreateInstance(t);
  62. if (securityManager != null)
  63. {
  64. IInternetSecurityManager ism = (IInternetSecurityManager)securityManager;
  65. ism.SetZoneMapping(URLZONE_TRUSTED, "http://127.0.0.1", SZM_CREATE);
  66. ism.SetZoneMapping(URLZONE_INTRANET, "http://127.0.0.1", SZM_CREATE);
  67. ism.SetZoneMapping(URLZONE_ESC_FLAG | URLZONE_TRUSTED, "http://127.0.0.1", SZM_CREATE);
  68. ism.SetZoneMapping(URLZONE_ESC_FLAG | URLZONE_INTRANET, "http://127.0.0.1", SZM_CREATE);
  69. }
  70. }
  71. catch
  72. {
  73. // Okay to continue if adding URL to trusted zone doesn't work...
  74. }
  75. Application.EnableVisualStyles();
  76. Application.SetCompatibleTextRenderingDefault(false);
  77. Application.Run(new Form1());
  78. }
  79. }
  80. }