间接使用的COM对象的STA或MTA [英] STA or MTA for indirectly used COM Objects

查看:98
本文介绍了间接使用的COM对象的STA或MTA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我了解公寓的概念以及为什么要使用STA或MTA,但这是一个问题 到目前为止,我没有找到解决方案.

I think I understand the apartment concept and why STA or MTA is used, however a question came up for which I found no solution so far.

如果我的应用程序在后台使用COM对象,例如从第三开始 党库,我怎么知道我是否可以使用多线程公寓(MTA)? 在那种情况下,我不知道那些对象是否是线程安全的,所以我需要去 STA,只是为了安全起见?

If my application uses COM Objects under the hood, for example from third party libraries, how do I know if I can use MultiThreaded-Appartment (MTA)? In that case, I have no Idea if those objects are thread safe, so do I need to go with STA, just to be on the safe side?

推荐答案

在STA或MTA线程代码上使用某些COM接口指针的能力取决于您是否完全在该线程上获得此指针.如果您能在STA(或MTA)线程上找到要点-最好进一步使用它,前提是您不直接将它传递给另一个单元.

Your ability to use certain COM interface pointer on STA or MTA thread code depends on whether you at all cab obtain this pointer on this thread. If you can get the point on STA (or MTA) thread - you are good to use it further, provided that you don't pass it directly to another apartment.

如果由于单元类型不匹配而以某种方式注册了COM服务器,而您无法获得指针(典型情况:STA线程和COM服务器使用免费"单元模型进行了注册),则COM将尝试为您编组指针.如果封送处理成功,您的代码将收到指针,您可以从那里开始.否则,您将得到一个错误,并且您尝试从另一间公寓进行相同操作的尝试将成功.基本上,这是在STA或MTA线程上能够使用特定COM接口的唯一可靠的通用方法.

If COM server is registered in certain way that you cannot obtain a pointer due to apartment type mismatch (typical case: STA thread and COM server is registered using "free" apartment model) then COM will attempt to marshal the pointer for you. If marshaling is successful, your code receives the pointer and you are good to go from there. Otherwise you get an error, and your attempt to do the same from another apartment succeeds. Basically, this is the only reliable generic method to obtain the answer on being able to use specific COM interface on STA or MTA thread.

在实例化COM对象的更特定的情况下,您可以查找其注册表信息以查看实例化是否与您的单元类型匹配.进程外服务器在任何情况下都会通过封送处理为您提供COM接口指针,因此任何客户端单元都可以使用此服务器.

In more specific case of instantiating a COM object, you can look up its registry information to see whether instantiation matches your apartment type. Out-of-process servers will provide you COM interface pointers through marshaling in any event, so any client side apartment will be able to consume this server.

毫无疑问,服务器对象是否是线程安全的.如果他们的COM注册正确(尤其是STA进程内服务器未宣布免费/两者"注册)并且可以进行封送处理,则可以免费包含线程安全.

There is no question of whether server objects are thread safe. If their COM registration is correct (esp. that STA in-proc server does not announce Free/Both registration) and marshaling is available, thread safety is included for free.

这篇关于间接使用的COM对象的STA或MTA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆