放在哪里的DllImport? [英] Where to put DllImport?

查看:1196
本文介绍了放在哪里的DllImport?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static class Class    
{
    public static void methodRequiringStuffFromKernel32()
    {
        // code here...
    }
} 

在哪里我把函数[DllImport(Kernel32中.DLL)] 在这里?

推荐答案

您把它放在你从导入方法KERNEL32.DLL。

You put it on the method you're importing from Kernel32.dll.

例如,

static class Class    
{
    [DllImport("Kernel32.dll")]
    static extern Boolean Beep(UInt32 frequency, UInt32 duration);

    public static void methodRequiringStuffFromKernel32()
    {
        // code here...
        Beep(...);
    }
}






从@ DTB :请注意,类应命名为 NativeMethods SafeNativeMethods UnsafeNativeMethods 。请参见命名约定对于非托管代码的方法,获取更多详情。


From @dtb: Note that the class should be named NativeMethods, SafeNativeMethods or UnsafeNativeMethods. See Naming Convention for Unmanaged Code Methods for more details.

CA1060:移动p /调用到NativeMethods类


  • NativeMethods - 这个类不禁止堆散步对于非托管代码的权限。 (System.Security.SuppressUnmanagedCodeSecurityAttribute不能应用于此类。)这个类是可以在任何地方使用,因为堆栈步将要执行的方法。

  • NativeMethods - This class does not suppress stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute must not be applied to this class.) This class is for methods that can be used anywhere because a stack walk will be performed.

SafeNativeMethods - 这个类抑制栈走对于非托管代码的权限。 (System.Security.SuppressUnmanagedCodeSecurityAttribute应用于此类。)这个类是安全的人来调用方法。这些方法的调用者不需要进行全面的安全审查,以确保使用是安全的,因为这些方法都是无害因任何调用

SafeNativeMethods - This class suppresses stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute is applied to this class.) This class is for methods that are safe for anyone to call. Callers of these methods are not required to perform a full security review to make sure that the usage is secure because the methods are harmless for any caller.

UnsafeNativeMethods - 这个类可以抑制栈走对于非托管代码的权限。 (System.Security.SuppressUnmanagedCodeSecurityAttribute应用于此类。)这个类是有潜在危险的方法。这些方法的调用方必须进行全面的安全审查,以确保使用是安全的,因为没有堆栈步行将被执行。

UnsafeNativeMethods - This class suppresses stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute is applied to this class.) This class is for methods that are potentially dangerous. Any caller of these methods must perform a full security review to make sure that the usage is secure because no stack walk will be performed.

这篇关于放在哪里的DllImport?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆