<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Code Analysis Team Blog : SuppressUnmanagedCode</title><link>http://blogs.msdn.com/fxcop/archive/tags/SuppressUnmanagedCode/default.aspx</link><description>Tags: SuppressUnmanagedCode</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>FAQ: How do I fix a violation of MovePInvokesToNativeMethodsClass?</title><link>http://blogs.msdn.com/fxcop/archive/2007/01/14/faq-how-do-i-fix-a-violation-of-movepinvokestonativemethodsclass.aspx</link><pubDate>Sun, 14 Jan 2007 19:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1429197</guid><dc:creator>David M. Kean</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1429197.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1429197</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1429197</wfw:comment><description>&lt;P&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms182161(VS.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms182161(VS.80).aspx"&gt;MovePInvokesToNativeMethodsClass&lt;/A&gt; fires on&amp;nbsp;P/Invokes&amp;nbsp;(ie methods marked with DllImport) that are not members of&amp;nbsp;one of the following classes;&amp;nbsp;NativeMethods, SafeNativeMethods or UnsafeNativeMethods.&lt;/P&gt;
&lt;P&gt;For example, the following sample fires this warning.&lt;BR&gt;&lt;BR&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt;[C#]&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;&lt;BR&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Runtime.InteropServices;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;internal&amp;nbsp;static&lt;/FONT&gt;&amp;nbsp;&lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;Directory&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;// Violates MovePInvokesToNativeMethodsClass&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#2b91af&gt;DllImport&lt;/FONT&gt;(&lt;FONT color=#a31515&gt;"kernel32.dll"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;extern&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt; RemoveDirectory(&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; name);&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;[Visual Basic]&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Public NotInheritable&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Directory&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Violates MovePInvokesToNativeMethodsClass&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;DllImport(&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#a31515&gt;"kernal32.dll"&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;, CharSet:=CharSet.Auto)&amp;gt; _&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; RemoveDirectory(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; Name &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;String&lt;/FONT&gt;) &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Boolean&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Function&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;In the above example, the RemoveDirectory P/Invoke should be moved to an appropriate class that is designed to only hold P/Invokes. 
&lt;P&gt;For most applications, moving P/Invokes to a new class called &lt;EM&gt;NativeMethods&lt;/EM&gt; is typically enough. However, in situations where you are developing reusable libraries for use in other applications then you should also consider defining two other classes called &lt;EM&gt;SafeNativeMethods&lt;/EM&gt; and &lt;EM&gt;UnsafeNativeMethods&lt;/EM&gt;. Both of these classes are similar to the NativeMethods class, however, they are marked with a special attribute called &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.security.suppressunmanagedcodesecurityattribute.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.security.suppressunmanagedcodesecurityattribute.aspx"&gt;SuppressUnmanagedCodeSecurityAttribute&lt;/A&gt;. Applying this attribute causes the runtime to avoid performing a full stack walk to make sure that all callers have the &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; permission when calling the p/invoke methods containing within these classes. The runtime will check your library for this permission at startup, however, not the assemblies that reference it. This can greatly improve performance when calling unmanaged code and also allows code with limited permissions to call these methods.&lt;/P&gt;
&lt;P&gt;However, using this attribute should not be taken lightly, as implemented incorrectly it can actually have serious security implications.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;STRONG&gt;NativeMethods&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;As the NativeMethods class should not be marked with &lt;STRONG&gt;SuppressUnmanagedCodeAttribute&lt;/STRONG&gt;, P/Invokes&amp;nbsp;placed within in it, will require &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; permission. As most applications run from the local machine and run with FullTrust, this is usually not a problem. However, if you developing reusable libraries, you should instead consider defining&amp;nbsp;a SafeNativeMethods or UnsafeNativeMethods class. &lt;/P&gt;
&lt;P&gt;The following example shows a method Interaction.Beep that wraps the MessageBeep function from user32.dll, the MessageBeep P/Invoke is placed within the NativeMethods class.&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[C#]&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;&lt;FONT face="courier new,courier"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.ComponentModel;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;Interaction&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// Callers require Unmanaged permission&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;void&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Beep()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#008000&gt;// No need to demand a permission as callers of Interaction.Beep&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // will require UnmanagedCode permission&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/FONT&gt; (!&lt;FONT color=#2b91af&gt;NativeMethods&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;.MessageBeep(-1))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;throw&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;Win32Exception&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;internal&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;NativeMethods&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#2b91af&gt;DllImport&lt;/FONT&gt;(&lt;FONT color=#a31515&gt;"user32.dll"&lt;/FONT&gt;, CharSet = &lt;FONT color=#2b91af&gt;CharSet&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;.Auto)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#0000ff&gt;return&lt;/FONT&gt;: &lt;FONT color=#2b91af&gt;MarshalAs&lt;/FONT&gt;(&lt;FONT color=#2b91af&gt;UnmanagedType&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;.Bool)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;extern&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt; MessageBeep(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; uType);&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[Visual Basic]&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt; System.ComponentModel&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Interaction&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' Callers require Unmanaged permission&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Beep()&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' No need to demand a permission as callers of Interaction.Beep&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' will require UnmanagedCode permission&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Not&lt;/FONT&gt; NativeMethods.MessageBeep(-1) &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Throw&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Win32Exception()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;If&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Class&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Friend&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; NativeMethods&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&amp;lt;DllImport(&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#a31515&gt;"user32.dll"&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;, CharSet:=CharSet.Auto)&amp;gt; _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; MessageBeep(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; uType &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Integer&lt;/FONT&gt;) &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &amp;lt;MarshalAs(UnmanagedType.Bool)&amp;gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Boolean&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;In the example above, any assembly that calls NativeMethod.MessageBeep or Interaction.Beep will require &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; permission.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;STRONG&gt;SafeNativeMethods&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;P/Invoke methods that are safe to be exposed to any application and do not have any side effects should be placed in a class called &lt;EM&gt;SafeNativeMethods&lt;/EM&gt; . No permissions need to be demanded and you do not need to pay too much attention to where they are getting called.&lt;/P&gt;
&lt;P&gt;The following example shows a property Environment.TickCount that wraps the GetTickCount function from kernel32.dll.&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[C#]&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;&lt;FONT face="courier new,courier"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Security;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;Environment&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// Callers do not require UnmanagedCode permission&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; TickCount&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#0000ff&gt;get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#008000&gt;// No need to demand a permission in place of&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#008000&gt;// UnmanagedCode as GetTickCount is considered&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// a safe method&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;SafeNativeMethods&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;.GetTickCount();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;SuppressUnmanagedCodeSecurityAttribute&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;]&lt;BR&gt;&lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;SafeNativeMethods&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#2b91af&gt;DllImport&lt;/FONT&gt;(&lt;FONT color=#a31515&gt;"kernel32.dll"&lt;/FONT&gt;, CharSet = &lt;FONT color=#2b91af&gt;CharSet&lt;/FONT&gt;.Auto, ExactSpelling = &lt;FONT color=#0000ff&gt;true&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;extern&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; GetTickCount();&lt;BR&gt;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;[Visual Basic]&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt; System.Security&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Environment&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' Callers do not require Unmanaged permission&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;ReadOnly&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Property&lt;/FONT&gt; TickCount() &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#0000ff&gt;Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#0000ff&gt;Get&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' No need to demand a permission in place of&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;' UnmanagedCode as GetTickCount is considered&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;' a safe method&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Return&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; SafeNativeMethods.GetTickCount()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Property&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;SuppressUnmanagedCodeSecurityAttribute()&amp;gt; _&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; SafeNativeMethods&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&amp;lt;DllImport(&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#a31515&gt;"kernel32.dll"&lt;/FONT&gt;, CharSet:=CharSet.Auto, ExactSpelling:=&lt;FONT color=#0000ff&gt;True&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)&amp;gt; _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; GetTickCount() &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Function&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;In the above example, although the assembly that defines Environment.TickCount still requires &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; permission, any callers of it do not.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;STRONG&gt;UnsafeNativeMethods&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;P/Invoke methods that are not safe to be called by anyone and can cause side effects should be placed in a class called &lt;EM&gt;UnsafeNativeMethods&lt;/EM&gt; . These methods should be either stringently checked to make sure that they are not being exposed to the user inadvertently (the rule &lt;EM&gt;Review SuppressUnmanagedCodeSecurity usage&lt;/EM&gt; can help with this) or should have another permission demanded in place of &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; when using them.&lt;/P&gt;
&lt;P&gt;The following example shows a method Cursor.Hide that wraps the ShowCursor function from user32.dll.&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[C#]&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Security;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Security.Permissions;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;Cursor&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;// Callers do not require UnmanagedCode permission, however,&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// they do require UIPermissionWindow.AllWindows&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;void&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Hide()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;// Need to demand an appropriate permission&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;// in place of UnmanagedCode permission as&amp;nbsp;&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// ShowCursor is not considered a safe method&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; UIPermission(UIPermissionWindow.AllWindows).Demand();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#2b91af&gt;UnsafeNativeMethods&lt;/FONT&gt;.ShowCursor(&lt;FONT color=#0000ff&gt;false&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;SuppressUnmanagedCodeSecurityAttribute&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;]&lt;BR&gt;&lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;UnsafeNativeMethods&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#2b91af&gt;DllImport&lt;/FONT&gt;(&lt;FONT color=#a31515&gt;"user32.dll"&lt;/FONT&gt;, CharSet = &lt;FONT color=#2b91af&gt;CharSet&lt;/FONT&gt;.Auto, ExactSpelling = &lt;FONT color=#0000ff&gt;true&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;extern&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; ShowCursor([&lt;FONT color=#2b91af&gt;MarshalAs&lt;/FONT&gt;(&lt;FONT color=#2b91af&gt;UnmanagedType&lt;/FONT&gt;.Bool)]&lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; bShow);&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;[Visual Basic]&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Security&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt; System.Security.Permissions&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Cursor&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' Callers do not require Unmanaged permission, however,&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;' they do require UIPermission.AllWindows&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Hide()&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;' Need to demand an appropriate permission&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#008000&gt;' in place of UnmanagedCode permission as&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' ShowCursor is not considered a safe method&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim&lt;/FONT&gt; permission &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; UIPermission(UIPermissionWindow.AllWindows)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;permission.Demand()&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;UnsafeNativeMethods.ShowCursor(&lt;FONT color=#0000ff&gt;False&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Sub&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;SuppressUnmanagedCodeSecurityAttribute()&amp;gt; _&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; UnsafeNativeMethods&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&amp;lt;DllImport(&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#a31515&gt;"user32.dll"&lt;/FONT&gt;, CharSet:=CharSet.Auto, ExactSpelling:=&lt;FONT color=#0000ff&gt;True&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)&amp;gt; _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; ShowCursor(&amp;lt;MarshalAs(UnmanagedType.Bool)&amp;gt; &lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; bShow &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Boolean&lt;/FONT&gt;) &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Function&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;In the above example, although the assembly that&amp;nbsp;defines Cursor.Hide still requires &lt;STRONG&gt;UnmanagedCode &lt;/STRONG&gt;permission, any callers of it do not. In place of &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; however, callers will require &lt;STRONG&gt;UIPermission.AllWindows&lt;/STRONG&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1429197" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/SuppressUnmanagedCode/default.aspx">SuppressUnmanagedCode</category></item></channel></rss>