Home All Groups Group Topic Archive Search About

Security Exception and Windows Vista

Author
21 Jul 2006 8:08 AM
Olivier B.
Hi,

I'm currently having a problem for which I haven't foud a solution.
I've got a winform application (C#, .NET 1.1) which should work correctly on
Vista (currently Beta 2).

I'm trying to do a send key but it does crash when the OS is Vista (It works
fine on Windows XP and lower).

Here is the line of code which crashes :
SendKeys.SendWait("^(c)");

Here is the exception :
Error details:-
======== Exception Information ========
System.Security.SecurityException: Requested registry access is not allowed.
   at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
   at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
   at SYSTRAN.GUI.IEPlugin.IEPluginContextMenu.CleanContextMenu()
   at SYSTRAN.GUI.IEPlugin.IEPluginContextMenu.RegisterContextMenu()
The Zone of the assembly that failed was:
MyComputer

I've looked for information on Internet but I have found no solution related
to this particular issue.

I have tried to change security level for my assembly :
[assembly:UIPermissionAttribute(SecurityAction.RequestMinimum,                 
Clipboard=UIPermissionClipboard.AllClipboard)]

And also that :
// Create a new application domain.
AppDomain domain = AppDomain.CurrentDomain;      
// Create a new AppDomain PolicyLevel.
PolicyLevel polLevel = PolicyLevel.CreateAppDomainLevel();
// Create a new unrestricted permission set.
PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);
UIPermission perm = new UIPermission(UIPermissionClipboard.AllClipboard);
permSet.AddPermission(perm);
// Give the policy level's root code group a new policy statement based
// on the new permission set.
polLevel.RootCodeGroup.PolicyStatement = new PolicyStatement(permSet);
// Give the new policy level to the application domain.
domain.SetAppDomainPolicy(polLevel);

And in both cases, nothing changed ... I do still have the same error.
It's most probably a problem with the security in Vista where my application
isn't allowed to interact with the desktop. The solution may eventually be to
adjust some security properties on Vista, but but even by changing the
Runtime Security Policy of my assembly nothing changes.

Any idea would be welcomed.

Thanks.

Olivier

Author
21 Jul 2006 3:52 PM
Olivier B.
I finally found a solution using SendInput API, so I post it, in case someone
may be interested.
*******************************************************************************
using System;
using System.Runtime.InteropServices;

namespace TestSendKeys
{

    public class MySendKeys
    {

        public static void CopyToClipboard()
        {
            uint intReturn = 0;
            NativeWIN32.INPUT structInput;
            structInput = new NativeWIN32.INPUT();
            structInput.type = NativeWIN32.INPUT_KEYBOARD;
            structInput.ki.wScan = 0;
            structInput.ki.time = 0;
            structInput.ki.dwFlags = 0;
            structInput.ki.dwExtraInfo = 0;
            //Press Ctrl Key
            structInput.ki.wVk = (ushort)NativeWIN32.VK.CONTROL;
            intReturn = NativeWIN32.SendInput(NativeWIN32.INPUT_KEYBOARD, ref
structInput, Marshal.SizeOf(structInput));
            // Key down the actual key-code
            structInput.ki.wVk =67;
            intReturn = NativeWIN32.SendInput(NativeWIN32.INPUT_KEYBOARD, ref
structInput, Marshal.SizeOf(structInput));
            // Key up the actual key-code
            structInput.ki.dwFlags = NativeWIN32.KEYEVENTF_KEYUP;
            structInput.ki.wVk =67;
            intReturn = NativeWIN32.SendInput(NativeWIN32.INPUT_KEYBOARD, ref
structInput, Marshal.SizeOf(structInput));
            //Keyup Ctrl
            structInput.ki.dwFlags = NativeWIN32.KEYEVENTF_KEYUP;
            structInput.ki.wVk = (ushort)NativeWIN32.VK.CONTROL;
            intReturn = NativeWIN32.SendInput(NativeWIN32.INPUT_KEYBOARD, ref
structInput, Marshal.SizeOf(structInput));
        }


        public static void PasteFromClipboard()
        {
            uint intReturn = 0;
            NativeWIN32.INPUT structInput;
            structInput = new NativeWIN32.INPUT();
            structInput.type = NativeWIN32.INPUT_KEYBOARD;
            structInput.ki.wScan = 0;
            structInput.ki.time = 0;
            structInput.ki.dwFlags = 0;
            structInput.ki.dwExtraInfo = 0;
            //Press Ctrl Key
            structInput.ki.wVk = (ushort)NativeWIN32.VK.CONTROL;
            intReturn = NativeWIN32.SendInput(NativeWIN32.INPUT_KEYBOARD, ref
structInput, Marshal.SizeOf(structInput));
            // Key down the actual key-code
            structInput.ki.wVk =86;
            intReturn = NativeWIN32.SendInput(NativeWIN32.INPUT_KEYBOARD, ref
structInput, Marshal.SizeOf(structInput));
            // Key up the actual key-code
            structInput.ki.dwFlags = NativeWIN32.KEYEVENTF_KEYUP;
            structInput.ki.wVk =86;
            intReturn = NativeWIN32.SendInput(NativeWIN32.INPUT_KEYBOARD, ref
structInput, Marshal.SizeOf(structInput));
            //Keyup Ctrl
            structInput.ki.dwFlags = NativeWIN32.KEYEVENTF_KEYUP;
            structInput.ki.wVk = (ushort)NativeWIN32.VK.CONTROL;
            intReturn = NativeWIN32.SendInput(NativeWIN32.INPUT_KEYBOARD, ref
structInput, Marshal.SizeOf(structInput));
        }
    }

    public class NativeWIN32
    {

        public const ushort KEYEVENTF_KEYUP = 0x0002;
        public enum VK : ushort
        {
            SHIFT                = 0x10,
            CONTROL              = 0x11,
            MENU                 = 0x12,
            ESCAPE               = 0x1B,
            BACK                 = 0x08,
            TAB                  = 0x09,
            RETURN               = 0x0D,
            PRIOR                = 0x21,
            NEXT                 = 0x22,
            END                  = 0x23,
            HOME                 = 0x24,
            LEFT                 = 0x25,
            UP                   = 0x26,
            RIGHT                = 0x27, 
            DOWN                 = 0x28, 
            SELECT               = 0x29, 
            PRINT                = 0x2A, 
            EXECUTE              = 0x2B, 
            SNAPSHOT             = 0x2C, 
            INSERT               = 0x2D, 
            DELETE               = 0x2E, 
            HELP                 = 0x2F,
            NUMPAD0              = 0x60, 
            NUMPAD1              = 0x61, 
            NUMPAD2              = 0x62, 
            NUMPAD3              = 0x63, 
            NUMPAD4              = 0x64, 
            NUMPAD5              = 0x65, 
            NUMPAD6              = 0x66, 
            NUMPAD7              = 0x67, 
            NUMPAD8              = 0x68, 
            NUMPAD9              = 0x69, 
            MULTIPLY             = 0x6A, 
            ADD                  = 0x6B, 
            SEPARATOR            = 0x6C, 
            SUBTRACT             = 0x6D, 
            DECIMAL              = 0x6E, 
            DIVIDE               = 0x6F, 
            F1                   = 0x70, 
            F2                   = 0x71, 
            F3                   = 0x72, 
            F4                   = 0x73, 
            F5                   = 0x74, 
            F6                   = 0x75, 
            F7                   = 0x76, 
            F8                   = 0x77, 
            F9                   = 0x78, 
            F10                  = 0x79, 
            F11                  = 0x7A, 
            F12                  = 0x7B, 
            OEM_1                = 0xBA,  
            // ',:' for US  OEM_PLUS             = 0xBB,  
            // '+' any country  OEM_COMMA            = 0xBC,  
            // ',' any country  OEM_MINUS            = 0xBD,  
            // '-' any country  OEM_PERIOD           = 0xBE,  
            // '.' any country  OEM_2                = 0xBF,  
            // '/?' for US  OEM_3                = 0xC0,  
            // '`~' for US  MEDIA_NEXT_TRACK         = 0xB0, 
            MEDIA_PREV_TRACK         = 0xB1, 
            MEDIA_STOP               = 0xB2, 
            MEDIA_PLAY_PAUSE         = 0xB3, 
            LWIN           =0x5B, 
            RWIN           =0x5C
        }

        public const uint INPUT_MOUSE = 0;
        public const uint INPUT_KEYBOARD = 1;
        public const uint INPUT_HARDWARE = 2;

        public struct KEYBDINPUT
        { 
            public ushort wVk; 
            public ushort wScan; 
            public uint dwFlags; 
            public long time; 
            public uint dwExtraInfo;
        };

        [StructLayout(LayoutKind.Explicit,Size=28)]
            public struct INPUT
        {
            [FieldOffset(0)]
            public uint type; 
            [FieldOffset(4)]
            public KEYBDINPUT ki;
        };

        [DllImport("user32.dll")]
        public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
cbSize);
    }

} *********************************************************************************
Author
25 Jul 2006 9:59 AM
dodot63
Ton anglais est pas mal :)

Comment ça, ça a rien à voir ? Bon je sors...