tags:

views:

53

answers:

2

Lately, I've been doing a lot of interaction with unmanaged libraries and I keep coming back to SO to ask questions about certain method signatures because I'm not a C/C++ programmer (although it's not completely alien to me). There are situations where the same type of argument in two different methods require two different P/Invoke signatures (Ex: sometimes I can use the out keyword, sometimes I have to use OutAttribute, etc). I can't really see any sort of reasoning behind it.

Are there any good resources out there for understanding P/Invoke and marshaling better for someone who isn't a C/C++ expert?

+2  A: 

PInvoke.net

Adam Maras
I love pinvoke.net, but I don't know if I'd send someone there to learn about pinvoke. There appears to be a decent 'Suggested Reading' section though.
Jay Riggs
The signatures for this particular unmanaged library isn't on PInvoke.net. I use that all time when working with Windows libraries, though. I'll take a look at the "Suggested Reading" section, as well. Thanks.
David Brown
+1  A: 

The problem is that the C/C++ languages do not give you a way to see whether the function produces or consumes data and whether an argument pointer points to a single value or an array of values. Learning the languages or studying P/Invoke don't really help with this, although it gives you a better shot at guessing it right.

You can only resolve this is by learning more about the specific native code for which you are writing a P/Invoke declaration. That requires its source code and some familiarity with the language. Or a good working relationship with the code's original author or owner.

Adam Nathan's book is the standard reference.

Hans Passant
For my current project, I have very detailed documentation for the unmanaged side. But there are cases, for example, where a method appears to take a pointer to a single struct, but the documentation states that it fills an array given by that argument. I have no idea how to write an equivalent C# signature for that sort of thing.
David Brown
Well, if it's an array then declare the argument as an array[]. Sure, Nathan can help you with that.
Hans Passant