I have an object written in traditional C++ COM, it is an Apartment threaded
object, this aggregates any number of other COM objects, written in any
language capable of implementing COM.
I have encountered marshalling problems when one of the COM objects being
aggregated is written in .NET, like most traditional COM objects my outer
object does not implement IMarshal therefore the QI by the
marshaling/interop layer for this interface gets serviced by the aggregatee
written in .NET. This effectively confuses the marshaller which now
incorrectly gets the inner objects implementation of IMarshal, which due to
all .NET COM objects behaving as if they aggregate the free-threaded
marshaller effectively makes it look like the outer object aggregates the
free threaded marshaller. Therefore when inter apartment calls occur the
call executes on the wrong thread, the calling thread, rather than being
marshalled back onto the correct thread. I have also had similar problem
with IProvideClassInfo/IProvideClassInfo2 where the .NET aggregates
implementation is getting incorrectly passed out.
My solution has been firstly to implement
IProvideClassInfo2/IProvideClassInfo on the outer aggregator object, which
was easy to do. Secondly, and the part I am not so comfortable with, I have
stopped the QI for IMarshal getting serviced by the aggregates, I now just
return E_NOINTERFACE from my outer object. Even though this works perfectly
and as expected the reason why I am uncomfortable with the fix is because I
have never seen any official documentation anywhere saying that you should
be carefull not to forward on certain interface requests. The fix I have
done seems completely reasonable but it would be nice to have this confirmed
as being the correct solution.
"Peter Huang" [MSFT] - 29 Apr 2005 09:46 GMT
Hi Andrew,
All managed objects other than those derived from ServicedComponent, when
exposed to COM, behave as if they have aggregated the free threaded
marshaler (FTM). In other words, they can be called on any thread without
any cross-apartment marshaling.
Although managed objects act as if they aggregate the FTM, they don¡¯t
actually aggregate it because that would be inefficient. Instead, the CLR
implements IMarshal on behalf of all managed objects that don¡¯t provide
their own implementation. The IMarshal implementation we provide is
similar to the one the FTM would do.
Managed objects & COM
http://blogs.msdn.com/cbrumme/archive/2003/04/15/51335.aspx
Since the .NET exposed COM's IMarshal is implemented by .NET runtime CCW, I
think your approach is valid.
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.