Does it require to marshal the com object supporti

2019-05-26 01:58发布

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

will it make any side effects?

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

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

标签: com atl
2条回答
孤傲高冷的网名
2楼-- · 2019-05-26 02:17

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.

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.

查看更多
Fickle 薄情
3楼-- · 2019-05-26 02:24

Yes, because you are creating an object in one apartment and invoking methods in another one. You have faced no side effects yet. I think because your object is simple (no callbacks, connection points, etc). But it may be changed.

查看更多
登录 后发表回答