Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Microsoft.Win32.UnsafeNativeMethods?

Tags:

c#

native

winapi

Microsoft has a very nice Windows API wrapper included in .NET framework. 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. Is there a way of accessing them easily?

like image 880
Kristina Brooks Avatar asked Jan 31 '10 02:01

Kristina Brooks


2 Answers

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. 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. You'll find the signatures of many Windows API functions for C# on pinvoke.net.

like image 174
dtb Avatar answered Sep 21 '22 20:09

dtb


For what it's worth I always thought there should have been a kernel32.interop.dll etc with the static methods already 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.

like image 34
Josh Avatar answered Sep 23 '22 20:09

Josh