views:

61

answers:

2

I'm currently working on some C# code that calls into a custom, native dll. When marshaling data back to C#, I have need to explicitly align the fields of the classes/structs used for marshaling. I've had a number of issues with this stemming from an incomplete understanding of the marshaler and its rules. For example, I recently found out through a SO question that the marshaler requires arrays to be DWORD aligned. Had I known this earlier, it would have saved me some headache.

My question is two-fold:

1) Is there any good documentation for the marshaler that provides this type of information?

2) What other similar restrictions should I be aware of? (For example, do all DWORD-sized fields have to be DWORD aligned)?

+1  A: 

http://pinvoke.net has a lot of good info, including exact marshalling instructions for many (most?) of the stock win32 functions, as well as many others. It's wiki-based, so as you figure out new libraries or glitches with existing ones you can add them.

Joel Coehoorn
That is a useful site, but I'm marshaling data back from a custom .dll. So, I have to define all of the marshaling struct/classes myself. I've had some annoying issues stemming from not understanding the marshaler and its rules, so I was hoping to find some good documentation on the subject.
Odrade
+2  A: 

There is a useful tool for generating the pinvoke signatures for a given library. It has shortcuts for win32 apis but you can pass in your own files.

pinvoke interop assistant

I'm not sure how much it will help with understanding of the mappings but at least it might give you working examples. I have used it to generate a C# interface to some of our native code and it worked well.

The codeplex site seems to have some useful diagnostic tools as well that I'm going to give go.

Colin Gravill