问题描述
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(...);
}
}
从@ :请注意,类应命名为 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.
:
-
NativeMethods - 这个类不禁止堆散步对于非托管代码的权限。 (System.Security.SuppressUnmanagedCodeSecurityAttribute不能应用于此类。)这个类是可以在任何地方使用,因为堆栈步将要执行的方法。
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!