http://blogs.msdn.com/b/ie/archive/2005/01/26/361228.aspx
// ?2005 Microsoft Corporation. All rights reserved.
//
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///
public class MainDialog : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblSite;
private System.Windows.Forms.TextBox txtPattern;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.GroupBox groupBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
// constants from urlmon.h
public const UInt32 URLZONE_LOCAL_MACHINE = 0;
public const UInt32 URLZONE_INTRANET = URLZONE_LOCAL_MACHINE + 1;
public const UInt32 URLZONE_TRUSTED = URLZONE_INTRANET + 1;
public const UInt32 URLZONE_INTERNET = URLZONE_TRUSTED + 1;
public const UInt32 URLZONE_UNTRUSTED = URLZONE_INTERNET + 1;
public const UInt32 URLZONE_ESC_FLAG = 0x100;
public const UInt32 SZM_CREATE = 0;
public const UInt32 SZM_DELETE = 0x1;
string[] _strZone = new string[]{"Local Machine", "Intranet",
"Trusted", "Internet", "Restricted"};
public static Guid CLSID_InternetSecurityManager = new Guid("7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4");
public static Guid IID_IInternetSecurityManager = new Guid("79eac9ee-baf9-11ce-8c82-00aa004ba90b");
private System.Windows.Forms.RadioButton radioButton0;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton4;
private IInternetSecurityManager _ism; // IInternetSecurityManager interface of ecurityManager COM object
private object _securityManager;
private System.Windows.Forms.Button btnClose; // SecurityManager COM object
Control _ctrl;
public MainDialog()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Type t = Type.GetTypeFromCLSID(CLSID_InternetSecurityManager);
_securityManager = Activator.CreateInstance(t);
_ism = (IInternetSecurityManager) _securityManager;
radioButton0.Tag = URLZONE_LOCAL_MACHINE;
radioButton1.Tag = URLZONE_INTRANET;
radioButton2.Tag = URLZONE_TRUSTED;
radioButton3.Tag = URLZONE_INTERNET;
radioButton4.Tag = URLZONE_UNTRUSTED;
radioButton4.Checked = true;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
Marshal.ReleaseComObject( _securityManager );
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtPattern = new System.Windows.Forms.TextBox();
this.lblSite = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton0 = new System.Windows.Forms.RadioButton();
this.btnClose = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// txtPattern
//
this.txtPattern.Location = new System.Drawing.Point(8, 32);
this.txtPattern.Name = "txtPattern";
this.txtPattern.Size = new System.Drawing.Size(216, 20);
this.txtPattern.TabIndex = 0;
this.txtPattern.Text = "";
//
// lblSite
//
this.lblSite.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblSite.Location = new System.Drawing.Point(8, 8);
this.lblSite.Name = "lblSite";
this.lblSite.Size = new System.Drawing.Size(100, 16);
this.lblSite.TabIndex = 1;
this.lblSite.Text = "Site pattern";
//
// btnAdd
//
this.btnAdd.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnAdd.Location = new System.Drawing.Point(144, 216);
this.btnAdd.Name = "btnAdd";
this.btnAdd.TabIndex = 3;
this.btnAdd.Text = "Add";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton4);
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton3);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Controls.Add(this.radioButton0);
this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(8, 64);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(160, 144);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Zone";
//
// radioButton4
//
this.radioButton4.Location = new System.Drawing.Point(16, 112);
this.radioButton4.Name = "radioButton4";
this.radioButton4.TabIndex = 4;
this.radioButton4.Tag = "";
this.radioButton4.Text = "Restricted";
this.radioButton4.CheckedChanged += new System.EventHandler(this.OnCheckedChange);
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(16, 64);
this.radioButton2.Name = "radioButton2";
this.radioButton2.TabIndex = 3;
this.radioButton2.Text = "Trusted";
this.radioButton2.CheckedChanged += new System.EventHandler(this.OnCheckedChange);
//
// radioButton3
//
this.radioButton3.Location = new System.Drawing.Point(16, 88);
this.radioButton3.Name = "radioButton3";
this.radioButton3.TabIndex = 2;
this.radioButton3.Tag = "";
this.radioButton3.Text = "Internet";
this.radioButton3.CheckedChanged += new System.EventHandler(this.OnCheckedChange);
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(16, 40);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 1;
this.radioButton1.Text = "Intranet";
this.radioButton1.CheckedChanged += new System.EventHandler(this.OnCheckedChange);
//
// radioButton0
//
this.radioButton0.Location = new System.Drawing.Point(16, 16);
this.radioButton0.Name = "radioButton0";
this.radioButton0.TabIndex = 0;
this.radioButton0.Text = "Local Machine";
this.radioButton0.CheckedChanged += new System.EventHandler(this.OnCheckedChange);
//
// btnClose
//
this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnClose.Location = new System.Drawing.Point(8, 216);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 2;
this.btnClose.Text = "Close";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// MainDialog
//
this.AcceptButton = this.btnAdd;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(232, 245);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.lblSite);
this.Controls.Add(this.txtPattern);
this.Name = "MainDialog";
this.Text = "SetZoneMapping Demo";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainDialog());
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
int result = _ism.SetZoneMapping((UInt32)_ctrl.Tag, txtPattern.Text, SZM_CREATE);
if (0==result)
{
MessageBox.Show("Pattern \""+txtPattern.Text+"\" successfully added to "+ _ctrl.Text+" zone.");
}
else
{
MessageBox.Show("Could not add pattern \""+txtPattern.Text+"\" to "+ _ctrl.Text+" zone.");
}
}
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
private void OnCheckedChange(object sender, System.EventArgs e)
{
_ctrl = sender as Control;
}
}
[ComImport, GuidAttribute("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInternetSecurityManager
{
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int SetSecuritySite([In] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetSecuritySite([Out] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int MapUrlToZone([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, out UInt32 pdwZone, UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId, ref UInt32 pcbSecurityId, uint dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int ProcessUrlAction([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, UInt32 dwAction, out byte pPolicy, UInt32 cbPolicy, byte pContext, UInt32 cbContext, UInt32 dwFlags, UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int QueryCustomPolicy([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, ref Guid guidKey, ref byte ppPolicy, ref UInt32 pcbPolicy, ref byte pContext, UInt32 cbContext, UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int SetZoneMapping(UInt32 dwZone, [In,MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetZoneMappings(UInt32 dwZone, out UCOMIEnumString ppenumString, UInt32 dwFlags);
}
}