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!

Using a Custom Marshaler

Once the marshaler is complete, it can be used as a custom wrapper for a particular type. Consider the managed interface definition IUserData written in C# below:

public interface IUserData {
   public void DoSomeStuff(INew pINew);
}

You would like the IUserData interface to use the NewOldMarshaler in order for unmanaged clients to be able to pass an IOld interface to the DoSomeStuff method method. This implies that the managed description of the DoSomeStuff method would take an INew interface (shown above) while the unmanaged version of DoSomeStuff takes an IOld interface pointer (shown below).

[uuid(9B2BAADA-0705-11D3-A0CD-00C04FA35826)]
Library UserLib {
    [uuid(9B2BABCD-0705-11D3-A0CD-00C04FA35826)]
    interface IUserData : IUnknown
       HRESULT DoSomeStuff(IOld pIOld);
}

When done correctly, the type library generated by exporting the managed definition of IUserData would yield the unmanaged definition shown above rather than the standard definition.

To accomplish this, the INew argument in the managed definition of the DoSomeStuff method is decorated with a custom attribute as shown below. The System.Runtime.InteropServices.MarshalAs attribute is used to indicate that the argument uses a custom marshaler.

using System.Runtime.InteropServices;

public interface IUserData {
void DoSomeStuff(
      [MarshalAs(UnmanagedType.CustomMarshaler, MarshalType=”NewOldMarshaler”]               
      INew pINew
   );
}

When used to specify a custom marshaler, the MarshalAs attribute has 2 named arguments:

See the class library documentation for System.Runtime.InteropServices.MarshalAs for more details.