interop marshal problem

Frank Colbert <[email protected]> Wed, 19 Jun 2002 08:32:51 -0700
Newsgroups gmane.comp.windows.devel.dotnet.general
Message-ID <[email protected]>
My scenario:

Component A is COM component marked "Both" for apt type.
Component A is created by VB App (STA - no choice here.) Component A is in
Main STA of VB app
VB App Creates managed component B (via CCW).
Component A is passed to Component B via interface method param.
Component B does some lookup table work among other things and creates COM
components C, D, E.
Component A is passed from B to C, D, E as an IUnknown interface method
param with the help of the RCW.

All this works if I use the calling thread (no multi-threading) to create
and invoke C, D, and E.  As soon as I attempt to multi-thread the creation
and invocation of C, D and E, the QI for A from IUnknown fails in C, D and
E.

I am attempting to create and invoke from B via the
MulticastDelegate.BeginInvoke, EndInvoke which I assume creates an MTA
thread pool.  This, apprently, is my problem.  Component A in Main STA
cannnot be marshalled to MTA system threadpool.  Even if I created my own
STA threads, the problem would still exist since the STA would be different
from the main STA.  In COM we would call CoMarshalInterXXX. In .NET we do
????

So far the best I can do is in B, determine whether A is a Com Object and
determine what apartment the calling thread is.  I can assume that if
calling thread == STA, then it is the main thread and not do multi-threading
- but this seems lame.

I think it is a realistic goal to write generic .NET components that can
work equally well whether callees and callers are managed or unmanaged.
With success, this starts to create a migration path for legacy code while
still pushing forward in the managed space.  That is my goal anyway, maybe
not Microsofts...

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.