views:

92

answers:

6

Is there a one-stop shop for determining which .Net types/attributes to use, given a native type?

Example would look something like this:

    Native Type  |     .Net Type
---------------------------------------
     int         |        Integer
     int*        |        IntPtr (or is it ref int?)
     LPCSTR      |        [MarshalAs(UnmanagedType.LPStr)]String
     ...

Guidelines for custom structures: 
    ...

Also, as a related aside, what's the most reliable place for looking up marshalling declarations for Win32 functions?

+6  A: 

pinvoke.net is a great resource for win32 function signatures. Otherwise you really have to look at the C/C++ include files to figure out the calling convention and arguments. Complex structs are a little tougher, but the easiest way is usually to declare the struct in C# using the StructLayout attribute on it to make sure all of the fields align.

I'm not aware of a single all-encompassing resource for doing this (that doesn't mean it doesn't exist, just that I don't know about it) but there is a lot of info on MSDN about doing this.

Steve Dennis
That answers the aside, but what about the question? The reason I ask is that I need to P/Invoke some functions from in-house dll's.
BlueRaja - Danny Pflughoeft
I only answer asides. Well, ok, I've edited my answer.
Steve Dennis
+1  A: 

Another pinvoker tool for Visual studio

http://www.pinvoker.com/

And another tool which converts a given unmanaged set of files and generates a managed dll:

http://www.paulyao.com/res/pinvoke/pinvoke.aspx

Finally, if you don't want to download any tools you can always refer to the pinvoke.net online reference for all the unmanaged libraries to get the pinvoke / marshal as definitions:

http://www.pinvoke.net/default.aspx/kernel32.waitforsingleobject

Brian Scott
+1  A: 

Although this MSDN page is titled "Default Marshaling Behavior", it covers explicit marshaling too. For instance the "Default Marshaling for Arrays" subsection is pretty detailed.

http://msdn.microsoft.com/en-us/library/zah6xy75.aspx

"Marshaling Data with Platform Invoke" also has lots of information:

http://msdn.microsoft.com/en-us/library/fzhhdwae.aspx

Qwertie
+6  A: 

Here is a chart: http://msdn.microsoft.com/en-us/library/ac7ay120.aspx

John Rasch
Close call between this and [Daniel's answer](http://stackoverflow.com/questions/3119752/p-invoke-how-to-know-which-type-to-marshall-from/3120158#3120158)
BlueRaja - Danny Pflughoeft
+1  A: 

I like the interop assistant. Give it your declaration (of function or type - you need the type if it's a parameter to the function) and it gives you VB or C# code.

Kate Gregory
+3  A: 

This CodeProject article has a nice list: http://www.codeproject.com/kb/dotnet/Win32APICPlusPlustoDotNET.aspx

Daniel Rose