Exploring MarshalAs Attributes

When should we use this attribute and why do we need it? For example, if a native function in c takes a pointer to an unsigned char as a parameter, and I know that it needs to execute an unsigned chars array, why can't I use a byte array in C # to use this function? Do I need to do marshalling?

+5
source share
2 answers

In most cases, the runtime can automatically determine how data is marshaled between native and managed code, so you usually don't need to specify an attribute. MarshalAsnecessary only in case of uncertainty in the definition (and you want to specify the runtime exactly, as for data marshaling), or if you require non-standard behavior.

In my experience, it is MarshalAsreally only required when working with strings, since there are so many different representations in the native code; unicode / ansi, c-lines or not, etc.

+6
source

MarshalAs - ( ) ByValArray SizeConst. , Windows API .

+1

All Articles