   [.net] Security Problems Calling Unmanaged Code
  Hello.  I am having trouble calling a function in user32.dll.  I need to call GetKeyState(), however I keep running into the following exception:An unhandled exception of type 'System.Security.SecurityException' occurred in Program.exeAdditional information: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.Whenever I call it.  The same occurs when I call any other function (for example, MessageBeep).  I have used the Administrative Tools .NET tool to set "All Code" to Full Trust.  I really have no idea what to do next.Here is an example of how I import the function:[DllImport("user32.dll")]static extern Boolean MessageBeep(UInt32 beepType);[DllImport("user32.dll")]public static extern ushort GetKeyState(short nVirtKey);public const ushort keyDownBit = 0x80;Thanks!   
  If you run your program in administrator mode, does it work?  It seems like you might be hosed.  Your application requires the UnmanagedCode privilidge and nothing's giving it up (the User->Code Group->All_Code is set to Full Trust on my machine, BTW).  Unless your application is being launched with those privilidges I'm not sure if there's a workaround.  There would be no point to app security if the app could dictate what it should have from the inside, would there?You could always try SuppressUnmanagedCodeSecurityAttribute, but that will simply cause your program to throw the exception at load time rather than call time (but it definitely saves on call overhead).Also, your key down bit should be 0x8000. :)   ;"If you run your program in administrator mode, does it work?"Not quite sure what you mean by this.  I am logged in as an administrator, if that is what you mean."Your application requires the UnmanagedCode privilidge and nothing's giving it up (the User->Code Group->All_Code is set to Full Trust on my machine, BTW)."One would think that I could simply set the code to have the appropriate privileges on my machine, as it is in fact my machine! I want to give it up, dammit! I have that permission set as well."Also, your key down bit should be 0x8000."Thanks!I guess I will keep searching.   ;  Might want to give more information, is it in C#? or you are loading it in a web context with ASP and IIS?   ;  Are you running it off a network share?   ;Quote:Original post by mutexAre you running it off a network share?Very good suggestion - I run into problems on *all* of my .Net apps when I try to run them from a shared folder at work.  This could easily explain the whole thing.   ;  Hello all.  I am not in fact running it off of a network share, nor am I doing anything related to IIS/Web development: this is a simple C# game using just GDI+ for rendering.   