COM: CoMarshalInterface problem.
Hello!
I have server side application with the method AddClient( IUnknown*
pUnkClient ).
The client is simple comobject.
When inside function AddClient I call pair:
CoMarshalInterface/CoUnmarshalInterface - count of references increases
in 3. Later client comobject become destroyed only when CoUninitialize
is called in the server. If in function AddClient I call pair:
CoMarshalInterThreadInterfaceInStream/CoGetInterfaceAndReleaseStream -
everything work ok.
I can't understand why this behaviour occurs.
Please, review Method1 in function AddClient and let me know, where am
I
wrong.
My function AddClient. There are 2 methods inside:
HRESULT AddClient( IUnknown* pUnkClient )
{
/*********/
// Method 1 -- increase client counter in 3; client object is destroyed
in CoUninialize of the server
CComPtr<IStream> pMStream1;
HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &pMStream1);
CComPtr<IUnknown> spOutUnk1;
hr = CoMarshalInterface(pMStream1, IID_IUnknown, pUnkClient,
MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
CoUnmarshalInterface(pMStream1,
IID_IUnknown, (void**)&spOutUnk1 );
/**********/
/**
// Method 2 - client counter keep it's counter value.
CComPtr<IStream> pMStream2;
CComPtr<IDispatch> spOutUnk2;
CoMarshalInterThreadInterfaceInStream(IID_IUnknown,
pUnkClient, &pMStream2);
CoGetInterfaceAndReleaseStream( pMStream2.Detach(),
IID_IUnknown, (void**)&spOutUnk1 );
/**/
}
Thanks a lot for any help!
SeraTJ