NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Defining Explicit Marshaling Behavior

In order to accurately marshal data between the managed and unmanaged space, the marshaler must know the expected managed and unmanaged data representations of the data being passed. For isomorphic types the representation is always the same and the managed signature always describes that representation. For example, a 4-byte integer is always marshaled to a 4-byte integer.

For non-isomorphic types, the marshaler knows the managed representation but not the unmanaged representation. For example, in the PrintMessage method shown in section 1, the marshaler can tell that the managed representation of the msg argument is a string but does not know what representation the PrintMessage function is actually expecting. Somehow the marshaler needs to know that the msg argument needs to be converted from a String to an ANSI character buffer.

The marshaler always knows the managed data representation from method signature or the field type. The unmanaged data representation can either be supplied explicitly or it can be inferred from the manage representation. For example, a string is converted to a BSTR when marshaled from managed to unmanaged code unless the marshaler is explicitly told to marshal the string to some other type like a LPSTR. You can explicitly supply the unmanaged data representation using the MarshalAsAttribute.