如何知道本机方法安全/不安全? [英] How to know if native method is safe / unsafe?

查看:149
本文介绍了如何知道本机方法安全/不安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现这个功能: GetSystemPowerStatusEx &安培; GetSystemPowerStatusEx2



根据这篇文章在 MSDN ,我要建立一个根据我将使用功能命名的类,但我的问题是:我怎么能知道在哪个班,我应该把GetSystemPowerStatusEx&安培; GetSystemPowerStatusEx2?



我迷路了......



感谢您的帮助。



我的问题是:这三个类名是好的,我(NativeMethods / SafeNativeMethods / UnsafeNativeMethods)




这些方法应在下列类别之一:



NativeMethods - 这个类并不
禁止堆走非托管
代码权限。本B类是方法,可以在任何地方使用

(System.Security.SuppressUnmanagedCodeSecurityAttribute
不能应用于此类。)$ B $因为堆栈步
就。执行



SafeNativeMethods - 这个类
抑制栈走非托管
代码权限。
(System.Security.SuppressUnmanagedCodeSecurityAttribute
被施加到这个类。)此类
是那些为
任何调用安全的方法。这些
方法的调用者不需要执行
全面的安全审查,以确保
中的使用是安全的,因为
方法是无害的任何调用者。



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



解决方案

这是一个非常愚蠢的警告,并最终不毛之地。但保持它的快乐很简单,只需添加一个静态类到您的项目命名为NativeMethods并把函数[DllImport]声明里面。无需单独的类。声明它们的内部



当心,你不能调用这些函数在一个模拟器,测试它们将需要在设备上运行它。为了使你的程序可调试在模拟器中请务必将与#IFDEF DEBUG调用它们的代码。


I implement this function : GetSystemPowerStatusEx & GetSystemPowerStatusEx2

according to this article on MSDN, I should create a class named according to the functions I will use, but my question is : How can I know in which class I should put GetSystemPowerStatusEx & GetSystemPowerStatusEx2 ?

I'm lost...

Thanks for help.

[EDIT] My question is : which of these three class names are the good one for me (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.

解决方案

It's a pretty silly warning and ultimately unproductive. But keeping it happy is simple, just add a static class to your project named NativeMethods and put the [DllImport] declarations inside it. No need for separate classes. Declare them internal.

Beware that you cannot call these functions on an emulator, testing them is going to require running it on the device itself. To keep your program debuggable in the emulator be sure to wrap the code that calls them with #ifdef DEBUG.

这篇关于如何知道本机方法安全/不安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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