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?
4 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.

- 213,145
- 36
- 401
- 431
-
That takes ages. I want to have access to all the functions in one class. – Kristina Jan 31 '10 at 02:31
-
As @SLaks wrote, many API functions are already exposed as managed classes in the .NET Framework, e.g., registry access, windows, GDI. It usually doesn't take so long to copy a couple of method signatures from a website into a C# file. If you're heavily depending on the Windows API and make no use of the .NET Framework, why not use a more appropriate programming language? – dtb Jan 31 '10 at 02:34
-
1Creating a nice UI in C++ is a headache. (IMHO) – Kristina Jan 31 '10 at 02:38
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.

- 68,005
- 14
- 144
- 156
Most of the functionality contained in these classes is exposed by the .Net framework itself; you should search (or ask here) before making API calls.
To answer your question, no.
The best you can do is to copy them from Reflector or the reference source.

- 868,454
- 176
- 1,908
- 1,964
-
-
1@Nick Brooks: "Open Source" is a well defined term. And it does not apply to the license to look at source code for personal or academic purposes (under which the reference source is licensed). – dtb Jan 31 '10 at 02:36
-
1@Josh Einstein: It's not the only one correct way to do it. The correct way to do it is to look up the function definitions in the Windows SDK documentation and translate them to P/Invoke declarations, as done on pinvoke.net – dtb Jan 31 '10 at 02:37
-
4I guarantee MS doesn't give a rats ass about people using reflector to copy DllImport attributes and signatures. – Josh Jan 31 '10 at 02:39
The reason could be the security impact of SuppressUnmanagedCodeSecurityAttribute. Check Move P/Invokes to NativeMethods class

- 15,125
- 2
- 28
- 46