Threading Model (Both) and cross apartment calls
Hi
I am developing this component and struck with a cross apartment call
issue ...
Component : COM- ATL
Threading model - "Both"
FTM / Free Threaded Marshalling supported
This component should support events using Connection points ..
The connection point /outgoing interface looks like this :
[id(1), helpstring("method MyEvent")] HRESULT MyEvent([in]IMyObj
*pMyObj);
The events may fired from another thread other than Main Thread , and
the outgoing argument IMyObj *pMyObj will be created in the event
thread (multithreaded apartment)
Here are the 2 situations the components will be used :
Case 1
Multi Threaded client, initialized with CoInitializeEx(NULL,
COINIT_MULTITHREADED);
All threads belongs to same apartment (multi thread) ,there is no cross
apartment calls and everything seems fine
Case 2
Single Threaded Apartment client , ie initialized with
CoInitialize(NULL)
In this case, we have 2 apartments
1) event thread(multi thread apartment )
2) main/client thread (single thread apartment)
These are two scenarios of cross apartment calls and it should be
resolved :
a) Sink/outgoing interface pointer will be called from event thread
b) The outgoing argument(IMyObj) will be created in "event thread"
which is going to be called from the "main/client thread ".
Case (a) could be resolved using IConnectionPointImplMT ,which use the
marshaled sink .
I am using IConnectionPointImplMT instead of IConnectionPointImpl to
perform interface (outgoing) marshalling
Ref : http://support.microsoft.com/kb/280512/EN-US/
Can any one suggest how can we resolve Case (b) ?
I am looking for a simple solution , don't want to marshal IMyObj
*pMyObj and send a stream or DWORD arguments to client and ask to
narwhal it .
How about if change the argument type to say :
[id(1), helpstring("method MyEvent")] HRESULT MyEvent([in]VARIANT
varMyObj);
Will the COM Standard marshalling do the trick and the client could
safely use Interface by casting it to IDispatch* ?
MyEventImplementaion (VARIANT varMyObj)
{
IMyObj *pMyObj =(IMyObj *) V_DISPATCH(varMyObj);
}
Thanks in advance
buddie