Re: STA to MTA (once more)

Ian Griffiths <[email protected]> Wed, 19 Jun 2002 12:01:58 -0400
Newsgroups gmane.comp.windows.devel.dotnet.general
Message-ID <016301c217aa$a613a660$9c27d341@imola>
Am I right in thinking that you are presuming that a RCW is only callable
from a single thread?  That's not actually true - in .NET you can pass a
reference to a COM object around from thread to thread and it just works.
The CLR does all the marshalling for you.

I think the silence may well speak volumes about the comprehensiveness of
interop, but not in the way you think - RCWs are callable from any CLR
thread.  The reason you don't have an equivalent of
CoMarshalInterThreadInterfaceInStream is that you don't need it - all of
that work is done for you automatically.

So you can create a COM object on one CLR thread and then call it from
another CLR thread and it will just work.  I just built an experimental
setup:  A classic STA COM component written in C++ prints out its threadID.
I have a C# app with [STAThread] on its Main method that creates the
component, and gets it to print out its thread ID.  I then store a reference
in a static variable.  I then fire up a new thread and force it to be in an
MTA (CurrentThread.ApartmentState = ApartmentState.MTA) and from that thread
I tell the object to print its thread again.

The same thread ID is printed both times.  (If I had tried the exact same
thing with a non-.NET client, i.e. passing a COM reference directly between
two threads, I would have a different thread ID - the wrong thread ID - on
the call from the MTA thread.)  This means that even though I called into
the COM object from two different CLR threads, the call was correctly
marshalled onto the right thread for the component.  So it just works.  I
think you're presuming that you have to do work that is actually done for
you by the CLR.

So interop is *more* comprehensive than you thought, not less comprehensive
than you had hoped.


--
Ian Griffiths

----- Original Message -----
From: "Frank Colbert" <[email protected]>


> I'll try re-posting with a diff spin....
>
> Given a COM object passed to managed code via an interface method param,
is
> there any way for me to tell whether the COM object was created in an STA
or
> MTA? What I really want to do is the managed version of
> CoMarshalInterThreadInterfaceInStream, but there appears to be no way to
do
> this.
>
> Sorry, I'll stop reposting now.  Silence speaks volumes about the
> completeness of System.Runtime.Interop.

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