简体   繁体   English

访问Microsoft.Win32.UnsafeNativeMethods?

[英]Accessing Microsoft.Win32.UnsafeNativeMethods?

Microsoft has a very nice Windows API wrapper included in .NET framework. Microsoft在.NET框架中包含一个非常好的Windows API包装器。 It is stored in Microsoft.Win32.UnsafeNativeMethods , Microsoft.Win32.SafeNativeMethods and Microsoft.Win32.NativeMethods .Unfortunately they aren't accessible because they are declared as private. 它存储在Microsoft.Win32.UnsafeNativeMethodsMicrosoft.Win32.SafeNativeMethodsMicrosoft.Win32.NativeMethods中。但遗憾的是,它们无法访问,因为它们被声明为私有。 Is there a way of accessing them easily? 有没有办法轻松访问它们?

Most method definitions in those classes (if not all) are extern declarations with DllImport attributes that refer to functions in the Windows API through P/Invoke. 这些类中的大多数方法定义(如果不是全部)都是具有DllImport属性的extern声明,这些属性通过P / Invoke引用Windows API中的函数。 In doesn't matter where these declarations reside. 无论这些声明在哪里都无关紧要。 You can create your own class named UnsafeNativeMethods or SafeNativeMethods and put declarations referring to the same Windows API functions in there. 您可以创建自己的名为UnsafeNativeMethods或SafeNativeMethods的类,并在其中放置引用相同Windows API函数的声明。 You'll find the signatures of many Windows API functions for C# on pinvoke.net . 您可以在pinvoke.net找到 C#的许多Windows API函数的签名。

Most of the functionality contained in these classes is exposed by the .Net framework itself; 这些类中包含的大多数功能都是由.Net框架本身公开的; you should search (or ask here) before making API calls. 你应该在进行API调用之前搜索(或在这里询问)。

To answer your question, no. 要回答你的问题,不。
The best you can do is to copy them from Reflector or the reference source. 您可以做的最好的事情是从Reflector或参考源复制它们。

For what it's worth I always thought there should have been a kernel32.interop.dll etc with the static methods already DllImport'ed. 为了它的价值我一直认为应该有一个kernel32.interop.dll等静态方法已经DllImport'ed。 But I've resorted to creating my own on an as-needed basis. 但我已经根据需要创建了自己的产品。 Over the years I've found I rarely use more than a handful of them but it's such a pain in the ass when I need an API that I haven't imported yet. 多年来我发现我很少使用它们中的一小部分,但是当我需要一个尚未导入的API时,它会如此痛苦。

The reason could be the security impact of SuppressUnmanagedCodeSecurityAttribute. 原因可能是SuppressUnmanagedCodeSecurityAttribute的安全影响。 Check Move P/Invokes to NativeMethods class 检查Move P / Invokes到NativeMethods类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM