接上篇,能否把一个MTA套间里面创建的COM对象传递个STA套间呢?看MSDN上的说明:
A client or server that supports both single-threaded and multithreaded apartments will have one multithreaded apartment, containing all threads initialized as free-threaded, and one or more single-threaded apartments. Interface pointers must be marshaled between apartments but can be used without marshaling within an apartment. Calls to objects in a single-threaded apartment will be synchronized by COM. Calls to objects in the multithreaded apartment will not be synchronized by COM.
上面这段文字已经说的很清楚了。我们不可以直接把一个COM对象传递一个另外一个套间。换句话说,只要涉及跨套间,就一定要marshal。
所以前面一篇文章的例子里面,如果辅助线程初始化成STA,那么就不能直接传递COM对象指针了,需要marshal。但是如果辅助线程也是MTA,那么就没有关系了,因为辅助线程和主线程都在MTA里面,同一个套间,无需marshal。
那么如果辅助线程并不属于某个套间,而是一个普通线程呢?
我找到了这些资料: