gpt4 book ai didi

c# - 将 P/Invokes 移动到 NativeMethods 类 因为它是 P/Invoke 方法消息

转载 作者:行者123 更新时间:2023-11-30 13:48:08 33 4
gpt4 key购买 nike

有人可以建议我如何处理这条消息吗?

CA1060 Move P/Invokes to NativeMethods class Because it is a P/Invoke method, 'UControl.InternetGetConnectedState(out int, int)' should be defined in a class named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods. Mega. UControl.xaml.cs 33

代码:

namespace Mega
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UControl
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int description, int reservedValue);

谢谢!

最佳答案

要消除警告,只需将您的 P/Invoke 方法添加到下面的类之一(通常是 NativeMethods)。如果您正在创建一个可重用的库,您应该将它们放在 UnsafeNativeMethodsSafeNativeMethods 中。


msdn page

To fix a violation of this rule, move the method to the appropriate NativeMethods class. For most applications, moving P/Invokes to a new class that is named NativeMethods is enough.

他们推荐使用 3 个 NativeMethods 类:

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 类是可以的(这至少会消除您看到的警告):

internal static class NativeMethods    
{
[DllImport("kernel32.dll")]
internal static extern bool RemoveDirectory(string name);
}

关于这个有一些讨论here ,上面链接的文章给出了一些关于何时使用这 3 个类的建议:

NativeMethods

Because the NativeMethods class should not be marked by using SuppressUnmanagedCodeSecurityAttribute, P/Invokes that are put in it will require UnmanagedCode permission. Because most applications run from the local computer and run together with full trust, this is usually not a problem. However, if you are developing reusable libraries, you should consider defining a SafeNativeMethods or UnsafeNativeMethods class.

SafeNativeMethods

P/Invoke methods that can be safely exposed to any application and that do not have any side effects should be put in a class that is named SafeNativeMethods. You do not have to demand permissions and you do not have to pay much attention to where they are called from.

UnsafeNativeMethods

P/Invoke methods that cannot be safely called and that could cause side effects should be put in a class that is named UnsafeNativeMethods. These methods should be rigorously checked to make sure that they are not exposed to the user unintentionally. The rule CA2118: Review SuppressUnmanagedCodeSecurityAttribute usage can help with this. Alternatively, the methods should have another permission that is demanded instead of UnmanagedCode when they use them.

关于c# - 将 P/Invokes 移动到 NativeMethods 类 因为它是 P/Invoke 方法消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590447/

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