是否需要编组支持线程模型“Both"的 com 对象? [英] Does it require to marshal the com object supporting threading model "Both"

查看:27
本文介绍了是否需要编组支持线程模型“Both"的 com 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们得到了进程内服务器 (.dll) 线程模型Both".我注意到注册表编辑器中的线程模型.因为我们知道用Both"创建的 com 对象将采用线程的线程模型创建的对象.我在 STA 线程中实例化了对象,以确保对 com 对象的调用应该只通过一个线程.我正在以序列化的方式从子线程调用这个对象的方法,其中对象实际上是在主(STA)线程中创建的

We are given in-proc-server (.dll) with threading model "Both".i noticed the threading model in registry editor.as we know com object created with "Both" will take the threading model of the thread in which the object is created. i instantiated the object in a STA thread to make sure call to com object should go through only one thread. i was invoking the methods of this objects from child thread in a serialized way where object is actually created in main(STA) thread

会不会有副作用?

当我在没有任何编组(GIT)的子线程中使用对象时,我没有收到任何错误并且执行得很好.

I did n't get any errors and executes fine when i used the object in child thread without any marshalling(GIT) .

现在我怀疑它是怎么发生的.当我谷歌时,我没有得到任何正确的答案.请给我一个简短的描述

now my doubt is how come it is happening.i didn't get any staright way answer for this when i google. please give me a brief description abt this

根据我的理解,如果对象支持STA",则需要对对象进行编组,是吗?这里我们的对象支持Both".

as per my understanding marshalling of an object is require if the object supports "STA" is it so? here our object supports "Both".

推荐答案

参见 这个很好的解释.是否使用编组取决于对象和调用者是否在同一个单元中.如果我说得对,您调用 CoInitializeEx() 将线程放入 STA,然后从该线程调用 CoCreateInstance() - 对象将在相同的STA,因此原始创建者线程将直接与其通信.另一个线程将如何使用该对象将取决于单元配置.从您的问题中不清楚其他线程是否调用 CoInitializeEx() 以及如何将对象指针传递给它.如果你只是传递一个原始指针,那么就无法知道如何开启编组.

See this very good explanation. Whether marshalling will be used will depend on whether the object and the caller are in the same apartment. If I get your words right you call CoInitializeEx() to place a thread into STA, then you call CoCreateInstance() from that thread - the object will be created in the same STA, so the original creator thread will communicate with it directly. How the other thread will use that object will depend on apartment configuration. It's unclear from your question whether the other thread calls CoInitializeEx() and how the object pointer is thransferred to it. If you just pass a raw pointer then there're no means for how marshalling would turn on.

当您将指针传递给另一个线程时,您可能会遇到真正的问题.上述文章的第二部分你不应该在公寓之间传递原始指针.但是,您的情况似乎是该规则的一个例外.COM 类被标记为具有Both 线程模型,因此它必须是完全线程安全的,因此它的方法可以从多个线程同时调用.我不确定这一点,但看起来你很安全.

When you pass a pointer to another thread you might encounter real problems. Part two of the above article says that you should never pass raw pointers between apartments. However looks like your case is an exception to that rule. The COM class is marked to have Both threading model, so it must be fully thread-safe and therefore its methods can be called from multiple threads simultaneously. I'm not sure about this, but looks like you're safe.

这篇关于是否需要编组支持线程模型“Both"的 com 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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