Export (0) Print
Expand All
1 out of 3 rated this helpful - Rate this topic

UIPermission Class

Controls the permissions related to user interfaces and the clipboard. This class cannot be inherited.

Namespace: System.Security.Permissions
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class UIPermission : CodeAccessPermission, IUnrestrictedPermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class UIPermission extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class UIPermission extends CodeAccessPermission implements IUnrestrictedPermission

Drawing and user input events in windows are user interfaces.

The permission to use windows can be one of the following: unrestricted, limited to SafeTopLevelWindows, only SafeSubWindows, or no window drawing or user input event access allowed. SafeTopLevelWindows and SafeSubWindows are restricted in title and size to prevent possible spoofing by potentially harmful code.

The permission to use the clipboard can be one of the following: unrestricted, write-only, or no clipboard access allowed. The paste limitation prevents potentially harmful applications from taking data from the clipboard without the user's consent, while still allowing the cut, copy, and paste operations when initiated by the user through keyboard commands.

using System;
using System.Security;
using System.Security.Permissions;

[assembly: CLSCompliant(true)]

public class UIPermissionDemo
{
    // The IsSubsetOf method determines whether the current permission is a
    // subset of the specified permission. This demo uses the IsSubsetOf
    // method to determine the relationship of two UIPermissionWindow
    // permissions and two UIPermissionClipboard permissions.
    private bool IsSubsetOfWindowAndClipboardDemo()
    {
        bool returnValue = true;
        UIPermissionWindow ui1, ui2;
        UIPermissionClipboard cb1, cb2;
        UIPermission uiIdPerm1, uiIdPerm2;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();
        uiGen1.ResetIndex();
        while (uiGen1.CreateUI( out ui1, out cb1))
        {
            uiIdPerm1 = new UIPermission(ui1, cb1);
            uiGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");

            while (uiGen2.CreateUI(out ui2, out cb2))
            {
                uiIdPerm2 = new UIPermission(ui2, cb2);
                try
                {
                    if (uiIdPerm1.IsSubsetOf(uiIdPerm2))
                    {
                        Console.WriteLine(uiIdPerm1.Window + " and " + uiIdPerm1.Clipboard + " is a subset of " +
                            uiIdPerm2.Window + " and " + uiIdPerm2.Clipboard);
                    }
                    else
                    {
                        Console.WriteLine(uiIdPerm1.Window + " and " + uiIdPerm1.Clipboard + " is not a subset of " +
                            uiIdPerm2.Window + " and " + uiIdPerm2.Clipboard);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown for subset :" + uiIdPerm1 + "\n" +
                        uiIdPerm2 + "\n" + e);
                    returnValue = false;
                }
            }
        }
        return returnValue;
    }
    // Compare two UIPemissionWindow permissions.
    private bool IsSubsetOfWindowDemo()
    {
        bool returnValue = true;
        UIPermissionWindow ui1, ui2;
        UIPermission uiIdPerm1, uiIdPerm2;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();
        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(out ui1))
        {
            uiIdPerm1 = new UIPermission(ui1);
            uiGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");

            while (uiGen2.CreateUI(out ui2))
            {
                uiIdPerm2 = new UIPermission(ui2);
                try
                {
                    if (uiIdPerm1.IsSubsetOf(uiIdPerm2))
                    {
                        Console.WriteLine(uiIdPerm1.Window + " is a subset of " + uiIdPerm2.Window);
                    }
                    else
                    {
                        Console.WriteLine(uiIdPerm1.Window + " is not a subset of " + uiIdPerm2.Window);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown for subset :" + uiIdPerm1 + "\n" +
                        uiIdPerm2 + "\n" + e);
                    returnValue = false;
                }
            }
        }
        return returnValue;
    }
    // Compare two UIPermissionClipboard permissions.
    private bool IsSubsetOfClipboardDemo()
    {
        bool returnValue = true;
        UIPermissionClipboard cb1, cb2;
        UIPermission uiIdPerm1, uiIdPerm2;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();
        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(out cb1))
        {
            uiIdPerm1 = new UIPermission(cb1);
            uiGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");

            while (uiGen2.CreateUI(out cb2))
            {
                uiIdPerm2 = new UIPermission(cb2);
                try
                {
                    if (uiIdPerm1.IsSubsetOf(uiIdPerm2))
                    {
                        Console.WriteLine(uiIdPerm1.Clipboard + " is a subset of " + uiIdPerm2.Clipboard);
                    }
                    else
                    {
                        Console.WriteLine(uiIdPerm1.Clipboard + " is not a subset of " + uiIdPerm2.Clipboard);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown for subset :" + uiIdPerm1 + "\n" +
                        uiIdPerm2 + "\n" + e);
                    returnValue = false;
                }
            }
        }
        return returnValue;
    }

    // Union creates a new permission that is the union of the current permission and the specified permission.
    private bool UnionDemo()
    {
        bool returnValue = true;
        UIPermissionWindow ui1, ui2;
        UIPermissionClipboard cb1, cb2;
        UIPermission uiIdPerm1, uiIdPerm2;
        IPermission uiIdPerm3;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();
        uiGen1.ResetIndex();
        while (uiGen1.CreateUI( out ui1, out cb1))
        {
            uiIdPerm1 = new UIPermission(ui1, cb1);
            Console.WriteLine("**********************************************************\n");
            uiGen2.ResetIndex();

            while (uiGen2.CreateUI(out ui2, out cb2))
            {
                uiIdPerm2 = new UIPermission(ui2, cb2);
                uiIdPerm3 = (UIPermission)uiIdPerm1.Union(uiIdPerm2);
                uiIdPerm3 = uiIdPerm1.Union(uiIdPerm2);

                if (uiIdPerm3 == null)
                {
                    Console.WriteLine("The union of " + ui1 + " and " + ui2 + " is null.");
                }
                else
                {
                    Console.WriteLine("The union of " + uiIdPerm1.Window + " and " + uiIdPerm1.Clipboard + " with " +
                        uiIdPerm2.Window + " and " + uiIdPerm2.Clipboard + " = \n\t" +
                        ((UIPermission)uiIdPerm3).Window.ToString() + " and " + ((UIPermission)uiIdPerm3).Clipboard.ToString());
                }

            }
        }

        return returnValue;

    }

    // Intersect creates and returns a new permission that is the intersection of the current
    // permission and the permission specified.
    private bool IntersectDemo()
    {
        bool returnValue = true;
        UIPermissionWindow ui1, ui2;
        UIPermissionClipboard cb1, cb2;
        UIPermission uiIdPerm1, uiIdPerm2, uiIdPerm3;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();
        uiGen1.ResetIndex();
        while (uiGen1.CreateUI( out ui1, out cb1))
        {
            uiIdPerm1 = new UIPermission(ui1, cb1);
            Console.WriteLine("**********************************************************\n");
            uiGen2.ResetIndex();

            while (uiGen2.CreateUI(out ui2, out cb2))
            {
                uiIdPerm2 = new UIPermission(ui2, cb2);
                uiIdPerm3 = (UIPermission)uiIdPerm1.Intersect(uiIdPerm2);
                if (uiIdPerm3 != null)
                {
                    Console.WriteLine("The intersection of " + uiIdPerm1.Window + " and " + uiIdPerm1.Clipboard + " with " +
                        uiIdPerm2.Window + " and " + uiIdPerm2.Clipboard + " = \n\t" +
                        ((UIPermission)uiIdPerm3).Window.ToString() + " and " + ((UIPermission)uiIdPerm3).Clipboard.ToString());
                }
                else
                {
                    Console.WriteLine("The intersection of " + ui1 + " and " + cb1 + " with " + ui2 + " and "
                        + cb2 + " is null.");
                }
            }
        }

        return returnValue;
    }

    //Copy creates and returns an identical copy of the current permission.
    private bool CopyDemo()
    {
        bool returnValue = true;

        UIPermissionWindow ui1;
        UIPermissionClipboard cb1;
        UIPermission uiIdPerm1, uiIdPerm2;

        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();

        uiGen1.ResetIndex();
        while (uiGen1.CreateUI( out ui1, out cb1))
        {
            uiIdPerm1 = new UIPermission(ui1, cb1);
            uiGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");
            try
            {
                uiIdPerm2 = (UIPermission)uiIdPerm1.Copy();
                Console.WriteLine("Result of copy = " + uiIdPerm2.ToString());

            }
            catch (Exception e)
            {
                Console.WriteLine("The copy failed :" + uiIdPerm1.ToString() + e);
                continue;
            }
        }
        return returnValue;
    }

    // ToXml creates an XML encoding of the permission and its current state;
    // FromXml reconstructs a permission with the specified state from the XML encoding.
    private bool ToFromXmlDemo()
    {
        bool returnValue = true;

        UIPermissionWindow ui1;
        UIPermissionClipboard cb1;
        UIPermission uiIdPerm1, uiIdPerm2;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();
        uiGen1.ResetIndex();
        while (uiGen1.CreateUI( out ui1, out cb1))
        {
            uiIdPerm1 = new UIPermission(ui1, cb1);
            Console.WriteLine("********************************************************\n");
            uiGen2.ResetIndex();
            try
            {
                uiIdPerm2 = new UIPermission(PermissionState.None);
                uiIdPerm2.FromXml(uiIdPerm1.ToXml());
                Console.WriteLine("Result of ToFromXml = " +
                    uiIdPerm2.ToString());

            }
            catch (Exception e)
            {
                Console.WriteLine("ToFromXml failed :" + uiIdPerm1.ToString() + e);
                continue;
            }
        }

        return returnValue;

    }
    // Invoke all demos.
    public bool runDemo()
    {
        bool returnCode = true;
        bool tempReturnCode;

        // Call the IsSubsetOfClipboard demo.
        if (tempReturnCode = IsSubsetOfClipboardDemo()) Console.Out.WriteLine("IsSubsetOfClipboard demo completed successfully.");
        else Console.Out.WriteLine("IsSubsetOfClipboard demo failed");
        returnCode = tempReturnCode && returnCode;

        // Call the IsSubsetOfWindow demo.
        if (tempReturnCode = IsSubsetOfWindowDemo()) Console.Out.WriteLine("IsSubsetOfWindow demo completed successfully.");
        else Console.Out.WriteLine("IsSubsetOfWindow demo failed");
        returnCode = tempReturnCode && returnCode;

        // Call the IsSubsetOfWindowAndClipboard demo.
        if (tempReturnCode = IsSubsetOfWindowAndClipboardDemo()) Console.Out.WriteLine("IsSubsetOfWindowAndClipboard demo completed successfully.");
        else Console.Out.WriteLine("IsSubsetOfWindowAndClipboard demo failed");
        returnCode = tempReturnCode && returnCode;

        // Call the Union demo.
        if (tempReturnCode = UnionDemo()) Console.Out.WriteLine("Union demo completed successfully.");
        else Console.Out.WriteLine("Union demo failed.");
        returnCode = tempReturnCode && returnCode;

        // Call the Intersect demo.
        if (tempReturnCode = IntersectDemo()) Console.Out.WriteLine("Intersect demo completed successfully. ");
        else Console.Out.WriteLine("Intersect demo failed.");
        returnCode = tempReturnCode && returnCode;


        // Call the Copy demo.
        if (tempReturnCode = CopyDemo()) Console.Out.WriteLine("Copy demo completed successfully.");
        else Console.Out.WriteLine("Copy demo failed.");
        returnCode = tempReturnCode && returnCode;

        // Call the ToFromXml demo.
        if (tempReturnCode = ToFromXmlDemo()) Console.Out.WriteLine("ToFromXml demo completed successfully.");
        else Console.Out.WriteLine("ToFromXml demo failed.");
        returnCode = tempReturnCode && returnCode;

        return (returnCode);
    }
    // Test harness.
    public static void Main(String[] args)
    {
        try
        {
            UIPermissionDemo democase = new UIPermissionDemo();
            bool returnCode = democase.runDemo();
            if (returnCode)
            {
                Console.Out.WriteLine("UIPermission demo completed successfully.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 100;
            }
            else
            {
                Console.Out.WriteLine("UIPermission demo failed.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 101;
            }
        }
        catch (Exception e)
        {
            Console.Out.WriteLine("UIPermission demo failed");
            Console.WriteLine(e.ToString());
            Console.Out.WriteLine("Press the Enter key to exit.");
            string consoleInput = Console.ReadLine();
            System.Environment.ExitCode = 101;
        }
    }
}


// This class generates UIPermission objects.

internal class UIGenerator
{
    private UIPermissionWindow[] myUI =
{
        UIPermissionWindow.AllWindows,UIPermissionWindow.SafeSubWindows,
    UIPermissionWindow.SafeTopLevelWindows, UIPermissionWindow.NoWindows};
    private UIPermissionClipboard[] myCB =
{ UIPermissionClipboard.AllClipboard, UIPermissionClipboard.OwnClipboard, UIPermissionClipboard.NoClipboard };

    private int uiIndex = 0;
    private int cbIndex = 0;

    public UIGenerator()
    {
        ResetIndex();
    }

    public void ResetIndex()
    {
        uiIndex = 0;
        cbIndex = 0;
    }
    public void ResetUIIndex()
    {
        uiIndex = 0;
    }

    // Create a UIPermission object using both a UIPermission Window enumeration and a
    // UIPermissionClipboard enumeration to specify permissions.
    public bool CreateUI( out UIPermissionWindow ui, out UIPermissionClipboard cb)
    {

        if (uiIndex >= myUI.Length && cbIndex >= (myCB.Length - 1))
        {
            ui = UIPermissionWindow.NoWindows;
            cb = UIPermissionClipboard.NoClipboard;
            return false;
        }

        if (uiIndex >= myUI.Length && cbIndex < (myCB.Length - 1))
        {
            ResetUIIndex();
            ui = myUI[uiIndex++];
            cb = myCB[++cbIndex];
            return true;
        }

        ui = myUI[uiIndex++];
        cb = myCB[cbIndex];
        try
        {
            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine("Cannot create UIPermission: " + ui + " " + e);
            ui = UIPermissionWindow.NoWindows;
            return true;
        }
    }

    // Create a UIPermission using Create a UIPermission using the specified UIPermissionWindow
    // enumeration to set Window permissions.
    public bool CreateUI(out UIPermissionWindow ui)
    {

        if (uiIndex >= myUI.Length)
        {

            ui = UIPermissionWindow.NoWindows;
            return false;
        }
        ui = myUI[uiIndex++];
        return true;

    }

    // Create a UIPermission using the specified UIPermissionClipboard enumeration to
    // set Clipboard permissions.
    public bool CreateUI(out UIPermissionClipboard cb)
    {


        if (cbIndex >= myCB.Length)
        {
            cb = UIPermissionClipboard.NoClipboard;
            return false;
        }

        cb = myCB[cbIndex++];
        return true;
    }
}

// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, 
// ToXml and FromXml methods
// of the UIPermission class.

import System.* ;
import System.Security.* ;
import System.Security.Permissions.*;

/** @assembly CLSCompliant(true)
 */

public class UIPermissionDemo
{
    // The IsSubsetOf method determines whether the current permission is 
    // a subset of the specified permission.
    // This demo uses the IsSubsetOf method to determine the relationship 
    // of two UIPermissionWindow permissions and two UIPermissionClipboard 
    // permissions.
    private boolean IsSubsetOfWindowAndClipboardDemo()
    {
        boolean returnValue = true;
        UIPermissionWindow ui1[] = new UIPermissionWindow[1];
        UIPermissionWindow ui2[] = new UIPermissionWindow[1];
        UIPermissionClipboard cb1[] = new UIPermissionClipboard[1];
        UIPermissionClipboard cb2[] = new UIPermissionClipboard[1];
        UIPermission uiIdPerm1[] = new UIPermission[1];
        UIPermission uiIdPerm2[] = new UIPermission[1];
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();

        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(uiIdPerm1, ui1, cb1)) {
            if (uiIdPerm1[0] == null) {
                continue;
            }
            uiGen2.ResetIndex();
            Console.WriteLine("**************************************" 
                + "******************\n");
            while (uiGen2.CreateUI(uiIdPerm2, ui2, cb2)) {
                if (uiIdPerm2[0] == null) {
                    continue;
                }
                try    {
                    if (uiIdPerm1[0].IsSubsetOf(uiIdPerm2[0])) {
                        Console.WriteLine((uiIdPerm1[0].get_Window() 
                            + " and " + uiIdPerm1[0].get_Clipboard() 
                            + " is a subset of " + uiIdPerm2[0].get_Window() 
                            + " and " + uiIdPerm2[0].get_Clipboard()));
                    }
                    else {
                        Console.WriteLine((uiIdPerm1[0].get_Window() 
                        + " and " + uiIdPerm1[0].get_Clipboard() 
                        + " is not a subset of " + uiIdPerm2[0].get_Window() 
                        + " and " + uiIdPerm2[0].get_Clipboard()));
                    }
                }
                catch (System.Exception e) {
                    Console.WriteLine(("An exception was " 
                        + "thrown for subset :" 
                        + uiIdPerm1[0] + "\n" + uiIdPerm2[0] + "\n" + e));
                    returnValue = false;
                }
            }
        }
        return returnValue;
    } //IsSubsetOfWindowAndClipboardDemo

    // Compare two UIPemissionWindow permissions.
    private boolean IsSubsetOfWindowDemo()
    {
        boolean returnValue = true;
        UIPermissionWindow ui1[] = new UIPermissionWindow[1];
        UIPermissionWindow ui2[] = new UIPermissionWindow[1];
        UIPermission uiIdPerm1[] = new UIPermission[1];
        UIPermission uiIdPerm2[] = new UIPermission[1];
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();

        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(uiIdPerm1, ui1)) {
            if (uiIdPerm1[0] == null) {
                continue;
            }
            uiGen2.ResetIndex();
            Console.WriteLine("********************************" 
                + "************************\n");
            while (uiGen2.CreateUI(uiIdPerm2, ui2)) {
                if (uiIdPerm2[0] == null) {
                    continue;
                }
                try {
                    if (uiIdPerm1[0].IsSubsetOf(uiIdPerm2[0])) {
                        Console.WriteLine((uiIdPerm1[0].get_Window() 
                            + " is a subset of " + uiIdPerm2[0].get_Window()));
                    }
                    else {
                        Console.WriteLine((uiIdPerm1[0].get_Window() 
                            + " is not a subset of " 
                            + uiIdPerm2[0].get_Window()));
                    }
                }
                catch (System.Exception e) {
                    Console.WriteLine(("An exception was " 
                        + "thrown for subset :" + uiIdPerm1[0] 
                        + "\n" + uiIdPerm2[0] + "\n" + e));
                    returnValue = false;
                }
            }
        }
        return returnValue;
    } //IsSubsetOfWindowDemo

    // Compare two UIPermissionClipboard permissions.
    private boolean IsSubsetOfClipboardDemo()
    {
        boolean returnValue = true;
        UIPermissionClipboard cb1[] = new UIPermissionClipboard[1];
        UIPermissionClipboard cb2[] = new UIPermissionClipboard[1];
        UIPermission uiIdPerm1[] = new UIPermission[1];
        UIPermission uiIdPerm2[] = new UIPermission[1];
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();

        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(uiIdPerm1, cb1)) {
            if (uiIdPerm1[0] == null) {
                continue;
            }
            uiGen2.ResetIndex();
            Console.WriteLine("********************************" 
                + "************************\n");
            while (uiGen2.CreateUI(uiIdPerm2, cb2)) {
                if (uiIdPerm2[0] == null) {
                    continue;
                }
                try {
                    if (uiIdPerm1[0].IsSubsetOf(uiIdPerm2[0])) {
                        Console.WriteLine((uiIdPerm1[0].get_Clipboard() 
                            + " is a subset of " 
                            + uiIdPerm2[0].get_Clipboard()));
                    }
                    else {
                        Console.WriteLine((uiIdPerm1[0].get_Clipboard() 
                            + " is not a subset of " 
                            + uiIdPerm2[0].get_Clipboard()));
                    }
                }
                catch (System.Exception e) {
                    Console.WriteLine(("An exception was thrown " 
                        + "for subset :" + uiIdPerm1[0] + "\n" 
                        + uiIdPerm2[0] + "\n" + e));
                    returnValue = false;
                }
            }
        }
        return returnValue;
    } //IsSubsetOfClipboardDemo

    // Union creates a new permission that is the union of the current 
    // permission and the specified permission.
    private boolean UnionDemo()
    {
        boolean returnValue = true;
        UIPermissionWindow ui1[] = new UIPermissionWindow[1];
        UIPermissionWindow ui2[] = new UIPermissionWindow[1];
        UIPermissionClipboard cb1[] = new UIPermissionClipboard[1];
        UIPermissionClipboard cb2[] = new UIPermissionClipboard[1];
        UIPermission uiIdPerm1[] = new UIPermission[1];
        UIPermission uiIdPerm2[] = new UIPermission[1];
        IPermission uiIdPerm3;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();

        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(uiIdPerm1, ui1, cb1)) {
            if (uiIdPerm1[0] == null) {
                continue;
            }
            Console.WriteLine("*************************************" 
                + "*********************\n");
            uiGen2.ResetIndex();
            while (uiGen2.CreateUI(uiIdPerm2, ui2, cb2)) {
                if (uiIdPerm2[0] == null) {
                    continue;
                }
                uiIdPerm3 = ((UIPermission)(uiIdPerm1[0].Union(uiIdPerm2[0])));
                uiIdPerm3 = uiIdPerm1[0].Union(uiIdPerm2[0]);
                if (uiIdPerm3 == null) {
                    Console.WriteLine(("The union of " + ui1[0] + " and " 
                        + ui2[0] + " is null."));
                }
                else {
                    Console.WriteLine(("The union of " 
                        + uiIdPerm1[0].get_Window() + " and " 
                        + uiIdPerm1[0].get_Clipboard() + " with " 
                        + uiIdPerm2[0].get_Window() + " and " 
                        + uiIdPerm2[0].get_Clipboard() + " = \n\t" 
                        + ((UIPermission)(uiIdPerm3)).get_Window().ToString() 
                        + " and " + ((UIPermission)(uiIdPerm3)).get_Clipboard().
                        ToString()));
                }
            }
        }
        return returnValue;
    } //UnionDemo

    // Intersect creates and returns a new permission that is the 
    // intersection of the current permission and the permission specified.
    private boolean IntersectDemo()
    {
        boolean returnValue = true;
        UIPermissionWindow ui1[] = new UIPermissionWindow[1];
        UIPermissionWindow ui2[] = new UIPermissionWindow[1];
        UIPermissionClipboard cb1[] = new UIPermissionClipboard[1];
        UIPermissionClipboard cb2[] = new UIPermissionClipboard[1];
        UIPermission uiIdPerm1[] = new UIPermission[1];
        UIPermission uiIdPerm2[] = new UIPermission[1];
        UIPermission uiIdPerm3 = null;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();

        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(uiIdPerm1, ui1, cb1)) {
            if (uiIdPerm1[0] == null) {
                continue;
            }
            Console.WriteLine("************************************" 
                + "**********************\n");
            uiGen2.ResetIndex();
            while (uiGen2.CreateUI(uiIdPerm2, ui2, cb2)) {
                if (uiIdPerm2[0] == null) {
                    continue;
                }
                uiIdPerm3 = 
                    ((UIPermission)(uiIdPerm1[0].Intersect(uiIdPerm2[0])));
                if (uiIdPerm3 != null) {
                    Console.WriteLine(("The intersection of " 
                        + uiIdPerm1[0].get_Window() + " and " 
                        + uiIdPerm1[0].get_Clipboard() + " with " 
                        + uiIdPerm2[0].get_Window() + " and " 
                        + uiIdPerm2[0].get_Clipboard() + " = \n\t" 
                        + ((UIPermission)(uiIdPerm3)).get_Window().ToString() 
                        + " and " + ((UIPermission)(uiIdPerm3)).get_Clipboard().
                        ToString()));
                }
                else {
                    Console.WriteLine(("The intersection of " 
                        + ui1[0] + " and " + cb1[0] + " with " + ui2[0] 
                        + " and " + cb2[0] + " is null."));
                }
            }
        }
        return returnValue;
    } //IntersectDemo

    //Copy creates and returns an identical copy of the current permission.
    private boolean CopyDemo()
    {
        boolean returnValue = true;
        UIPermissionWindow ui1[] = new UIPermissionWindow[1];
        UIPermissionClipboard cb1[] = new UIPermissionClipboard[1];
        UIPermission uiIdPerm1[] = new UIPermission[1];
        UIPermission uiIdPerm2 = null;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();

        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(uiIdPerm1, ui1, cb1)) {
            if (uiIdPerm1[0] == null) {
                continue;
            }
            uiGen2.ResetIndex();
            Console.WriteLine("**********************************" 
                + "**********************\n");
            try {
                uiIdPerm2 = ((UIPermission)(uiIdPerm1[0].Copy()));
                Console.WriteLine(("Result of copy = " 
                    + uiIdPerm2.ToString()));
            catch (System.Exception e) {
                Console.WriteLine(("The copy failed :" 
                    + uiIdPerm1[0].ToString() + e));
                continue;
            }
        }
        return returnValue;
    } //CopyDemo

    // ToXml creates an XML encoding of the permission and its current state;
    // FromXml reconstructs a permission with the specified state from the 
    // XML encoding.
    private boolean ToFromXmlDemo()
    {
        boolean returnValue = true;
        UIPermissionWindow ui1[] = new UIPermissionWindow[1];
        UIPermissionClipboard cb1[] = new UIPermissionClipboard[1];
        UIPermission uiIdPerm1[] = new UIPermission[1];
        UIPermission uiIdPerm2 = null;
        UIGenerator uiGen1 = new UIGenerator();
        UIGenerator uiGen2 = new UIGenerator();

        uiGen1.ResetIndex();
        while (uiGen1.CreateUI(uiIdPerm1, ui1, cb1)) {
            if (uiIdPerm1 == null) {
                continue;
            }
            Console.WriteLine("*************************************" 
                + "*******************\n");
            uiGen2.ResetIndex();
            try {
                uiIdPerm2 = new UIPermission(PermissionState.None);
                uiIdPerm2.FromXml(uiIdPerm1[0].ToXml());
                Console.WriteLine(("Result of ToFromXml = " 
                    + uiIdPerm2.ToString()));
            }
            catch (System.Exception e) {
                Console.WriteLine(("ToFromXml failed :" 
                    + uiIdPerm1[0].ToString() + e));
                continue;
            }
        }
        return returnValue;
    } //ToFromXmlDemo

    // Invoke all demos.
    public boolean runDemo()
    {
        boolean returnCode = true;
        boolean tempReturnCode;

        // Call the IsSubsetOfClipboard demo.
        if (tempReturnCode = IsSubsetOfClipboardDemo()) {
            Console.get_Out().WriteLine("IsSubsetOfClipboard demo " 
                + "completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("IsSubsetOfClipboard demo failed");
        }
        returnCode = tempReturnCode && returnCode;
        // Call the IsSubsetOfWindow demo.
        if (tempReturnCode = IsSubsetOfWindowDemo()) {
            Console.get_Out().WriteLine("IsSubsetOfWindow demo " 
                + "completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("IsSubsetOfWindow demo failed");
        }
        returnCode = tempReturnCode && returnCode;

        // Call the IsSubsetOfWindowAndClipboard demo.
        if (tempReturnCode = IsSubsetOfWindowAndClipboardDemo()) {
            Console.get_Out().WriteLine("IsSubsetOfWindowAndClipboard " 
                + "demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("IsSubsetOfWindowAndClipboard " 
                + "demo failed");
        }
        returnCode = tempReturnCode && returnCode;

        // Call the Union demo.
        if (tempReturnCode = UnionDemo()) {
            Console.get_Out().WriteLine("Union demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Union demo failed.");
        }

        returnCode = tempReturnCode && returnCode;

        // Call the Intersect demo.
        if (tempReturnCode = IntersectDemo()) {
            Console.get_Out().WriteLine("Intersect demo completed " 
                + "successfully. ");
        }
        else {
            Console.get_Out().WriteLine("Intersect demo failed.");
        }

        returnCode = tempReturnCode && returnCode;

        // Call the Copy demo.
        if (tempReturnCode = CopyDemo()) {
            Console.get_Out().WriteLine("Copy demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Copy demo failed.");
        }

        returnCode = tempReturnCode && returnCode;

        // Call the ToFromXml demo.
        if (tempReturnCode = ToFromXmlDemo()) {
            Console.get_Out().WriteLine("ToFromXml demo completed " 
                + "successfully.");
        }
        else {
            Console.get_Out().WriteLine("ToFromXml demo failed.");
        }
        returnCode = tempReturnCode && returnCode;
        return returnCode;
    } //runDemo

    // Test harness.
    public static void main(String[] args)
    {
        try {
            UIPermissionDemo democase = new UIPermissionDemo();
            boolean returnCode = democase.runDemo();
            if (returnCode) {
                Console.get_Out().WriteLine(
                "UIPermission demo completed successfully.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                Environment.set_ExitCode(100);
            }
            else {
                Console.get_Out().WriteLine("UIPermission demo failed.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                Environment.set_ExitCode(101);
            }
        }
        catch (System.Exception e) {
            Console.get_Out().WriteLine("UIPermission demo failed");
            Console.WriteLine(e.ToString());
            Console.get_Out().WriteLine("Press the Enter key to exit.");
            String consoleInput = Console.ReadLine();
            Environment.set_ExitCode(101);
        }
    } //main
} //UIPermissionDemo

// This class generates UIPermission objects.
class UIGenerator
{
    private UIPermissionWindow myUI[] =  { 
        UIPermissionWindow.AllWindows, 
        UIPermissionWindow.SafeSubWindows, 
        UIPermissionWindow.SafeTopLevelWindows, 
        UIPermissionWindow.NoWindows};

    private UIPermissionClipboard myCB[] =  { 
        UIPermissionClipboard.AllClipboard, 
        UIPermissionClipboard.OwnClipboard, 
        UIPermissionClipboard.NoClipboard};

    private int uiIndex = 0;
    private int cbIndex = 0;

    public UIGenerator()
    {
        ResetIndex();
    } //UIGenerator

    public void ResetIndex()
    {
        uiIndex = 0;
        cbIndex = 0;
    } //ResetIndex

    public void ResetUIIndex()
    {
        uiIndex = 0;
    } //ResetUIIndex

    // Create a UIPermission object using both a UIPermission Window 
    // enumeration and a UIPermissionClipboard enumeration to specify 
    // permissions.
    public boolean CreateUI(UIPermission uiPerm[], UIPermissionWindow ui[], 
        UIPermissionClipboard cb[])
    {
        if (uiIndex >= myUI.length && cbIndex >= myCB.length - 1) {
            ui[0] = UIPermissionWindow.NoWindows;
            cb[0] = UIPermissionClipboard.NoClipboard;
            uiPerm[0] = new UIPermission(ui[0], cb[0]);
            return false;
        }
        if (uiIndex >= myUI.length && cbIndex < myCB.length - 1) {
            ResetUIIndex();
            ui[0] = myUI[uiIndex++];
            cb[0] = myCB[++cbIndex];
            uiPerm[0] = new UIPermission(ui[0], cb[0]);
            return true;
        }
        ui[0] = myUI[uiIndex++];
        cb[0] = myCB[cbIndex];
        try {
            uiPerm[0] = new UIPermission(ui[0], cb[0]);
            return true;
        }
        catch (System.Exception e) {
            Console.WriteLine(("Cannot create UIPermission: " 
                + ui + " " + e));
            uiPerm[0] = new UIPermission(PermissionState.None);
            ui[0] = UIPermissionWindow.NoWindows;
            return true;
        }
    }

    // Create a UIPermission using Create a UIPermission using the 
    // specified UIPermissionWindow enumeration to set Window permissions.
    public boolean CreateUI(UIPermission uiPerm[], UIPermissionWindow ui[])
    {
        if (uiIndex >= myUI.length) {
            uiPerm[0] = new UIPermission(PermissionState.None);

            ui[0] = UIPermissionWindow.NoWindows;
            return false;
        }
        ui[0] = myUI[uiIndex++];

        try {
            uiPerm[0] = new UIPermission(ui[0]);
            return true;
        }
        catch (System.Exception e) {
            Console.WriteLine(("Cannot create UIPermission: " 
            + ui[0] + " " + e));
            uiPerm[0] = new UIPermission(PermissionState.None);
            ui[0] = UIPermissionWindow.NoWindows;
            return true;
        }
    } //CreateUI

    // Create a UIPermission using the specified UIPermissionClipboard 
    // enumeration to set Clipboard permissions.
    public boolean CreateUI(UIPermission uiPerm[], 
        UIPermissionClipboard cb[])
    {
        if (cbIndex >= myCB.length) {
            uiPerm[0] = new UIPermission(PermissionState.None);
            cb[0] = UIPermissionClipboard.NoClipboard;
            return false;
        }
        cb[0] = myCB[cbIndex++];
        try {
            uiPerm[0] = new UIPermission(cb[0]);
            return true;
        }
        catch (System.Exception e) {
            Console.WriteLine(("Cannot create UIPermission: " 
                + cb[0] + " " + e));
            uiPerm[0] = new UIPermission(PermissionState.None);
            cb[0] = UIPermissionClipboard.NoClipboard;
            return true;
        }
    } //CreateUI
} //UIGenerator

System.Object
   System.Security.CodeAccessPermission
    System.Security.Permissions.UIPermission
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.