Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have simple dilemma about what is propper syntax and semanthics when using Unsafe Native Methods with UI Permissions. Here is example of program code with explenation at official MS Visual Studio Docs:

Article link

If I have understand correctly the syntax is this :

create NEW UI PERMISSION
call Unsafe Method


What I did not understand ,
Do I have to repeatedly create New UI Security Permission, each time I call Unsafe Method ?
If I have to call many different Unsafe Methods successively , do I have to create new UI Security Permission for each one of them.

What I have tried:

After reading a lot of unfamiliar stuf on the Internet about this subject I have created program that works properly, debbuger do not shows any errors or warnings, but I still do not know is it correctly written. Here is peace of code :

C#
void Register_Clipboard_Listener_And_HotKeys()
{
    // Reset Registration message
    Registration_Message = string.Empty;

    // Demand new security permission
    new UIPermission(UIPermissionWindow.AllWindows, UIPermissionClipboard.AllClipboard).Demand();

    // Set Clipboard listener
    Clipboard_Listener = UnsafeNativeMethods.AddClipboardFormatListener(this.Handle);

    if (Clipboard_Listener == false)
    {
        Registration_Message += "   Clipboard listener could not be registered.  \n\n";
        Registration_Ok = false;
        return;
    }

    // Demand new security permission
    new UIPermission(UIPermissionWindow.AllWindows).Demand();

    // Hot Key Combination to Control + V
    Ctrl_V = UnsafeNativeMethods.RegisterHotKey(this.Handle, V, Control | MOD_NOREPEAT, V);
    if (Ctrl_V == false)
    {
        Registration_Message += "   Hot Key - Ctrl + V - could not be registered.  \n\n";
        Registration_Ok = false;
        return;
    }

    // Demand new security permission
    new UIPermission(UIPermissionWindow.AllWindows).Demand();

    // Hot Key Combination to Control + V
    Ctrl_Alt_V = UnsafeNativeMethods.RegisterHotKey(this.Handle, W, Control | Alt | MOD_NOREPEAT, V);
    if (Ctrl_Alt_V == false)
    {
        Registration_Message += "   Hot Key - Ctrl + Alt + V - could not be registered.  \n\n";
        Registration_Ok = false;
        return;
    }


Is this part of code correctly written.
Is there anything that is necessary to do before main window is closed.

Thanks in advance.

All the best,
Željko Perić
Posted
Updated 30-Jan-20 6:45am

Maybe you can find more information about this subject in chapter 21.Security of the book "C# 6 in a Nutshell".

And here is a CodeProject article: CAS (Code access security) & .NET 4.0 Security model FAQ (With Full Video demonstration)[^]
 
Share this answer
 
v2
Comments
Perić Željko 30-Jan-20 12:53pm    
This solution is too expensive.
RickZeeland 30-Jan-20 13:50pm    
Luckily my boss pays the bill :)
There are articles on CodeProject about CAS too, just had to use the correct search term ...
Perić Željko 30-Jan-20 14:08pm    
When I was typing "too expensive",
I meant at the time I need to get the answer.
Anyhow tanks.
After a long search on the Internet, I have found this article :

Code Access Security in .NET | DotNet Information[^]

It clearly gives answer to the above question.

All the best,
Željko Perić
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900