gpt4 book ai didi

c# - DllImport 放在哪里?

转载 作者:太空狗 更新时间:2023-10-29 18:08:46 24 4
gpt4 key购买 nike

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

我应该把 [DllImport("Kernel32.dll")] 放在哪里?

最佳答案

你把它放在你从 Kernel32.dll 导入的方法上。

例如,

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

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

来自@dtb : 请注意,该类应命名为 NativeMethodsSafeNativeMethodsUnsafeNativeMethods。参见 Naming Convention for Unmanaged Code Methods了解更多详情。

CA1060: Move P/Invokes to NativeMethods class :

  • 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 - 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 - 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.

关于c# - DllImport 放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6076717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com