Compartilhar via


IMarshal::MarshalInterface (Compact 2013)

3/26/2014

This method writes into a stream the data required to initialize a proxy object in some client process.

Syntax

HRESULT MarshalInterface(
  IStream* pStm,
  REFIID riid,
  void* pv,
  DWORD dwDestContext,
  void* pvDestContext,
  DWORD mshlflags 
);

Parameters

  • pStm
    [in] Pointer to the stream to be used during marshaling.
  • riid
    [in] Reference to the identifier of the interface to be marshaled. This interface must be derived from the IUnknown interface.
  • pv
    [in] Pointer to the interface pointer to be marshaled; can be NULL if the caller does not have a pointer to the desired interface.
  • dwDestContext
    [in] Destination context where the specified interface is to be unmarshaled.

    Values for dwDestContext come from the enumeration MSHCTX.

    Unmarshaling can occur in another apartment of the current process (MSHCTX_INPROC) or in another process on the same computer as the current process (MSHCTX_LOCAL).

  • pvDestContext
    [in] Reserved; set to 0 (zero).
  • mshlflags
    [in] Whether the data to be marshaled is to be transmitted back to the client process - the typical case - or written to a global table, where it can be retrieved by multiple clients.

    Valid values come from the MSHLFLAGS enumeration.

Return Value

The method supports the standard return value E_FAIL, as well as the following:

  • S_OK
    The interface pointer was marshaled successfully.
  • E_NOINTERFACE
    The specified interface is not supported.
  • STG_E_MEDIUMFULL
    The stream is full.

Remarks

This method is called indirectly, in a call to CoMarshalInterface, by whatever code in the server process may be responsible for marshaling a pointer to an interface on an object.

This marshaling code is usually a stub generated by COM for one of several interfaces that can marshal a pointer to an interface implemented on an entirely different object. An example is the IClassFactory interface. For this discussion, the code responsible for marshaling a pointer is called the marshaling stub.

To determine whether the platform supports this interface, see Determining Supported COM APIs.

Notes to Callers

Typically, rather than calling IMarshal::MarshalInterface directly, your marshaling stub instead should call the CoMarshalInterface function, which contains a call to this method.

The stub makes this call to command an object to write its marshaling data into a stream. The stub then either passes the marshaling data back to the client process or writes it to a global table, where it can be unmarshaled by multiple clients.

The stub's call to CoMarshalInterface is typically preceded by a call to CoGetMarshalSizeMax, to get the maximum size of the stream buffer into which the marshaling data will be written.

You do not explicitly call this method if you are doing the following:

  • Implementing existing COM interfaces
  • Defining your own interfaces using the Microsoft Interface Definition Language (MIDL)

In both cases, the MIDL-generated stub makes the call.

If you are not using MIDL to define your own interface, your marshaling stub must call this method, either directly or indirectly. Your stub implementation should call MarshalInterface immediately after its previous call to IMarshal::GetMarshalSizeMax returns.

Because the value returned by GetMarshalSizeMax is guaranteed to be valid only so long as the internal state of the object being marshaled does not change, a delay in calling MarshalInterface runs the risk that the object will require a larger stream buffer than originally indicated.

If the caller has a pointer to the interface to be marshaled, it should, as a matter of efficiency, use the pv parameter to pass that pointer. In this way, an implementation that may use such a pointer to determine the appropriate CLSID for the proxy does not have to call QueryInterface on itself.

If a caller does not have a pointer to the interface to be marshaled, it can pass NULL.

Notes to Implementers

Your implementation of IMarshal::MarshalInterface must write to the stream whatever data is needed to initialize the proxy on the receiving side. Such data would include the following:

  • A reference to the interface to be marshaled
  • A MSHLFLAGS value specifying whether the data should be returned to the client process or written to a global table
  • Whatever is needed to connect to the object, such as a handle to a window, or pointer to an RPC channel

Your implementation should not assume that the stream is large enough to hold all the data. Rather, it should gracefully handle a STG_E_MEDIUMFULL error. Just before exiting, your implementation should position the seek pointer in the stream immediately after the last byte of data written.

If the pv parameter is NULL and your implementation needs an interface pointer, it can call IUnknown::QueryInterface on the current object to get it. The pv parameter exists merely to improve efficiency.

Delegate marshaling to COM's default implementation for all dwDestContext values that your implementation does not handle.

To delegate marshaling to COM's default implementation, call the CoGetStandardMarshal helper function.

Using the MSHLFLAGS enumeration, callers can specify whether an interface pointer is to be marshaled back to a single client or written to a global table, where it can be unmarshaled by multiple clients. Be sure your object can handle calls from the multiple proxies that might be created from the same initialization data.

Requirements

Header

objidl.h,
objidl.idl

Library

ole32.lib,
uuid.lib

See Also

Reference

IMarshal
CoGetStandardMarshal
IMarshal::GetMarshalSizeMax
IMarshal::GetUnmarshalClass
IMarshal::UnmarshalInterface