gpt4 book ai didi

c# - 如何知道 native 方法是否安全/不安全?

转载 作者:可可西里 更新时间:2023-11-01 08:25:18 25 4
gpt4 key购买 nike

我实现了这个功能:GetSystemPowerStatusEx & GetSystemPowerStatusEx2

根据这篇关于 MSDN 的文章,我应该创建一个根据我将使用的功能命名的类,但我的问题是:我怎么知道我应该把 GetSystemPowerStatusEx 和 GetSystemPowerStatusEx2 放在哪个类中?

我迷路了...

感谢您的帮助。

[编辑] 我的问题是:这三个类名中哪一个对我来说比较好(NativeMethods/SafeNativeMethods/UnsafeNativeMethods)?

These methods should be in one of the following classes:

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.

最佳答案

这是一个非常愚蠢的警告,最终没有效果。但让它开心很简单,只需将静态类添加到名为 NativeMethods 的项目中,并将 [DllImport] 声明放入其中。不需要单独的类(class)。将它们声明为内部

请注意,您不能在模拟器上调用这些函数,测试它们需要在设备本身上运行它。为了让您的程序在模拟器中可调试,请务必使用#ifdef DEBUG 包装调用它们的代码。

关于c# - 如何知道 native 方法是否安全/不安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4511418/

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