views:

229

answers:

6

Every few months I find myself needing to call Win32 from C#. Though I've done it a dozen times, I've usually forgotten the exact machinations, so I poke around the web or old code and figure out what DllImport statements I need, etc.

Am I alone?

Is there a sanctioned "Win32" class that has the requisite declarations for the entire Win32 API? Seems like there ought to be. Maybe I'm missing something.

+1  A: 

There's no sanctioned Win32 class, but http://www.pinvoke.net is a great central resource for these things.

itowlson
+8  A: 

You may find http://www.pinvoke.net helpful.

Also, for common Win32 functions, you could try the P/Invoke Interop Assistant.

Paul Baker
PInvoke Interop Assistant can also be used to translate C code straight to the appropriate signatures and functions.
JaredPar
+1  A: 

This sounds like a great idea for an open source project.

/em puts on thinking cap, and runs of to pinvoke.net

Mike Hofer
A: 

Check out: http://code.msdn.microsoft.com/WindowsAPICodePack

leon
A: 

You could use Reflector and search for Win32Native. It has a bunch of definitions that you could use for PInvoking.

SwDevMan81
+1  A: 

Getting to the correct DllImport statements are easy like everyone is saying, but for ease if use I usually wrap the Win32 functions I need in C# classes and compile into my core helper assembly. So next time I just reference the assembly. So if I need some Win32 function, chances are it is already in my helper lib if not I just add it.

Also I map the return codes to Exceptions rather, i.e. if the HResult is non zero I throw an Exception so my C# apps do not need to know about HResult or return codes ever.

Marthinus