IMarshal interface

IMarshal interface is the mechanism by which an object is custom-marshaled. IMarshal is defined as follows:


interface IMarshal : IUnknown {
   HRESULT   GetUnmarshalClass(iid, pvInterface, dwDestContext, pvDestContext, mshlflags, pclsid);
   HRESULT   GetMarshalSizeMax(iid, pvInterface, dwDestContext, pvDestContext, mshlflags, pcb);
   HRESULT   MarshalInterface(pstm, iid, pvInterface, dwDestContext, pvDestContext, mshlflags);
   HRESULT   UnmarshalInterface(pstm, iid, ppvInterface);
   HRESULT   DisconnectObject(dwReserved);
   HRESULT   ReleaseMarshalData(pstm);
   };

The process of custom marshaling an interface pointer involves two steps, with an optional third:

  1. The code doing the marshaling calls IMarshal::GetUnmarshalClass(). This returns the class ID that will be used to create an uninitialized proxy object in the unmarshaling context.
  2. (Optional) The marshaler calls IMarshal::GetMarshalSizeMax() to learn an upper bound on the amount of memory that will be required by the object to do the marshaling.
  3. The marshaler calls IMarshal::MarshalInterface() to carry out the marshaling.
The class IDand the bits that were marshaled into the stream are then conveyed by appropriate means to the destination, where they are unmarshaled. Unmarshaling involves the following essential steps:


   IClassFactory * pcf;
   CoGetClassObject(clsid, CLSCTX_INPROCSERVER, IID_IClassFactory, &pcf);


IMarshal * proxy;
   pcf->CreateInstance(NULL, IID_IMarshal, &proxy);


   IOriginal * pobj;
   proxy->UnmarshalInterface(pstm, IID_Original, &pboj);
   proxy->Release();
   pcf->Release();

The object proxy is now ready for use.