Reputation: 7619
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)?
Upvotes: 1
Views: 227
Reputation: 4274
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.
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.
Upvotes: 2
Reputation: 416059
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.
Upvotes: 1