Everybody who used P/Invoke of Windows API knows a long list of declarations of static functions with attributes like
[DllImport ("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
The declaration of structures copied from Windows headers like WinNT.h or from web sites like www.pinvoke.net take also a lot of place in our programs.
Why we all have to spend our time for this? Why Microsoft not give us a simple way to include a line like in old unmanaged programs
#include <windows.h>
and we would be have access to a static class Native
with all or the most Windows functions and structures inside?
UPDATED based on some answers:
It you will be honest with yourself, which part of your .NET programs are running not under Windows? 5%? 1%? If you use WMI, Process, Registry or ServiceProcess.ServiceBase classes etc. in .NET is your program so “platform independent” and more “compatible” if you not use native API? Has Thread.ApartmentState
everywhere sense out of Windows?
Usage of SafeHandle
or try {…} finally
is of cause required. I would be happy to have a standard definition of Native API in any of these forms.
I know that some structures in windows.h
have different versions 32/64 or XP/Vista etc. I think it would be enough to have also different version of Native structures like MIB_IPADDRROW_XP
and MIB_IPADDRROW_W2K
or IMAGE_NT_HEADERS32
and IMAGE_NT_HEADERS64
(see the same names in windows headers).
One should of cause use managed .NET classes and methods if there are exist. It’s a pity, but the implementation of some the most powerful features of Windows come in managed .NET too late and a lot of there are accessed till now only in unmanaged world. 10 year ago at one of the first Microsoft conference about .NET I asked about my favor Memory Mapped Files. Only .NET 4 has now MemoryMappedFile
class implemented (see http://blogs.msdn.com/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx). The same problems exist permanently if one writes utilities for administrative purpose. For example, opening file with CreateFile
and Flag FILE_FLAG_BACKUP_SEMANTICS
or RegCreateKeyEx
with REG_OPTION_BACKUP_RESTORE
or REG_OPTION_CREATE_LINK
. Hard Links and Junctions (see http://msdn.microsoft.com/en-us/library/aa365006(v=VS.85).aspx) are next examples. One more example: working with Job Objects instead of Process (see http://msdn.microsoft.com/en-us/library/ms684161(VS.85).aspx and http://www.microsoft.com/msj/0399/jobkernelobj/jobkernelobj.aspx). If you want to start a process and wait not till this process, but also untill all it’s child processes be ended, job is very useful. One can use CreateJobObject
and AssignProcessToJobObject
and then use TerminateJobObject
or WaitHandle
like .NET version of (WaitForSingleObject
).
Only in .NET 4.0 System.IntPtr
und System.UIntPtr
classes supports Add
and Subtract
methods.
I can continue the examples, but I hope you understand what I mean. So I want to write a program in .NET because of a lot of advantages but I can do this not always without usage of native API. Moreover, there are sometimes customer's requirement to the language of software implementation and I choose the required way.
UPDATED 2: Microsoft improved the interoperability with COM in .NET 4.0, but not with C-like Windows API. I see some ways to makes working with P/Invoke easier. What way to do this you see? I suggest three directions which I see:
- One makes an assembly with declaration of all (or the most important) not yet fully implemented in .NET Windows API. This assemble can has only (or almost only) metadata of static Native class with static functions and corresponding classes / structures. This assembly can be placed in GAC and everybody could use it. (This way I tested. It works very well.)
- One implements a collection of snippets with declaration and samples of using of different native Windows API.
- One start an open source project on www.codeplex.com where one create .NET classes and extension methods to some most important native Windows API which are not yet implemented in .NET.
I am sure that exist more ways to make easier for developer the usage of native Windows API. Other suggestions are welcome.