This question is a result of this thread.

My question is, why do we have to specify the MarshalAs attribute for some of the parameters but not others? I would have thought the differences between c++ and c# data types would be large enough to require direction for the compiler?

Excuse my ignorance, as ive never dealt with unmanaged code before :)

Cheers, Adam

有帮助吗?

解决方案

The parameters that aren't marshalled are just ints. Ints are ints are ints are ints, everywhere, native or not. So there don't need to be any special instructions to deal with them, the value is just passed by value, and you're all set.

The pointers and strings are more tricky. C# strings aren't necessarily represented like C strings, which are simply pointers to a null-terminated array of characters. Internal conversion might be necessary. Similarly, the array needs to be passed by reference - and explicitly told to do so.

Remember, in unmanaged code, addresses are just data. There's nothing special about them. So the C# compiler needs to know how to take all it knows about your variables, and turn it into numbers.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top