This topic has not yet been rated - Rate this topic

AtlMarshalPtrInProc

Creates a new stream object, writes the CLSID of the proxy to the stream, and marshals the specified interface pointer by writing the data needed to initialize the proxy into the stream.

HRESULT AtlMarshalPtrInProc(
   IUnknown* pUnk,
   const IID& iid,
   IStream** ppStream 
);

Parameters

pUnk
[in] A pointer to the interface to be marshaled.
iid
[in] The GUID of the interface being marshaled.
ppStream
[out] A pointer to the IStream interface on the new stream object used for marshaling.

Return Value

A standard HRESULT value.

Remarks

The MSHLFLAGS_TABLESTRONG flag is set so the pointer can be marshaled to multiple streams. The pointer can also be unmarshaled multiple times.

If marshaling fails, the stream pointer is released.

AtlMarshalPtrInProc can only be used on a pointer to an in-process object.

Example

//marshaling interface from one thread to another

//IStream ptr to hold serialized presentation of interface ptr
IStream* g_pStm;

//forward declaration
DWORD WINAPI ThreadProc(LPVOID lpParameter);

HRESULT WriteInterfacePtrToStream(IFoo *pFoo)
{
   //marshall the interface ptr to another thread
   //pFoo has to be pointer to actual object & not a proxy
   HRESULT hr = AtlMarshalPtrInProc(pFoo, IID_IFoo, &g_pStm);

   //m_dwThreadID is a DWORD holding thread ID of thread being created.
   CreateThread(NULL, 0, ThreadProc, NULL, 0, &m_dwThreadID);
   return hr;
}

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
   IFoo* rpFoo;

   //unmarshal IFoo ptr from the stream
   HRESULT hr = AtlUnmarshalPtr(g_pStm, IID_IFoo, &rpFoo);

   // use IFoo ptr to call its methods
   //...
   //...

   //release the stream if no other thread requires it 
   //to unmarshal the IFoo interface pointer
   hr = AtlFreeMarshalStream(g_pStm);

   return hr;
}

See Also

Marshaling Global Functions | AtlUnmarshalPtr | AtlFreeMarshalStream | MSHLFLAGS

Did you find this helpful?
(1500 characters remaining)