You're right that it's not easy but there is a good reason for that IMHO.
With .NET, Microsoft is actively discouraging people from writing unmanaged
code and instead using managed APIs for obvious reasons. While there are
some valid reasons for going to the Win32 API, they are in fact few in
comparison to going out from VB 6 for instance (multimedia is an obvious
exception). I would guess this was not done so that people stay mostly with
writing "pure" managed code and program one way to the .NET Framework SDK.
This results in many benefits. Of course, I have no insight into their
decision but this would be my take.
--
--------------------------------------------------------------
Sam Gentile [C#/.NET MVP]
.NET Blog
http://samgentile.com/blog/
MSDN Column:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/bridge.asp
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
---------------------------------------------------------------
Julie J. said:
It would have been nice (and not asking too much) if MS had created an
additional namespace that contained all the Win32 APIs.
The current way makes porting a real pita.
Jeff Schwartz said:
Here is an example of how you can create PInvoke signatures for these
native methods. I found which DLL had the exported function by searching
MSDN.
Hope this example helps.
using System;
using System.Runtime.InteropServices;
internal class NativeMethods {
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern IntPtr OpenProcess(int dwDesirdedAccess, bool
bInheritHandle, int dwProcessId);
[DllImport("psapi.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern bool EnumProcesses(int[] lpidProcess, int cb, out
int cbNeeded);
}
public class PClass {
public static int Main() {
int[] pids;
int bytesNeeded;
pids = new int[1024];
if (NativeMethods.EnumProcesses(pids, 1024, out bytesNeeded)) {
// returned true;
for(int i=0; i<(bytesNeeded/4); i++) {
Console.WriteLine("PID{0}: {1}",i, pids);
}
} else {
// got error
Console.WriteLine("NativeMethods.EnumProcesses() failed");
}
return 100;
}
}