When a client invokes a method on a remote object’s interface, it doesn’t know, and doesn’t have to know, where the target object exists. Yet the method invocation will find its way to the target object. This is the idea of location transparency, and it is one of the beauties of COM. As you have seen, the marshaling provided automatically by RPC is usually sufficient to pass arguments from a client to a server component. COM needs a little help, however, in one situation: where you try to pass an interface pointer as an argument so that the same object can be used by both a client and a server.
When you pass data between two different execution contexts (e.g., two processes) in COM, it is said to go from one apartment to another. In this section, assume that a client and a server occupy separate apartments and you have to get the interface pointer between them.
COM marshals an interface pointer in one apartment (the exporting apartment) and unmarshals it in another (the importing apartment). If a client invokes an interface method implemented by a particular object to obtain an interface pointer, as indicated by the
[out] attribute in the following method signature, COM, on the object side, will marshal the interface pointer into a chunk (series of bytes) using the CoMarshalInterface API function.
On the client side, COM will unmarshal the chunk into a valid interface pointer, using the CoUnmarshalInterface API function.
HRESULT TakeOcr([out] IOcr **ppOcr);
The same can ...