进程外COM单例 [英] Out-of-process COM singletons

查看:270
本文介绍了进程外COM单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理一个单线程库(不是线程安全的),我想包装在一个COM进程外对象绕过它的线程非安全。我需要一个行为,每个新的COM对象在其自己的进程中实例化 - 某种形式的进程外单例。

I am dealing with a single-threaded library (not thread safe) that I want to wrap in a COM out-of-process object to get around its thread non-safety. I need a behavior where each new COM object is instantiated in its own process - some form of out-of-process singleton.

处理硬核COM,所以我不知道如何处理这一点,或者是否这是可能没有一些诡计。

It's been a while since I've been dealing with hardcore COM so I am not sure how to approach this or whether this is at all possible without some trickery.

推荐答案

CoRegisterClassObject(),REGCLS参数设置为REGCLS_SINGLEUSE。注意成本,一个过程不是一个便宜的操作系统对象。

Call CoRegisterClassObject() with the REGCLS argument set to REGCLS_SINGLEUSE. Beware of the cost, a process is not a cheap operating system object.

否则没有理由你不能确保marshaled方法调用是线程安全的,只是调用CoInitializeEx()来请求一个STA。在进程内服务器中也是如此,它在进程内服务器中也是如此。如果你只是这样做以解决一个线程问题,那么肯定有利于在进程STA。需要消息循环。

There is otherwise no reason why you couldn't ensure that marshaled method calls are thread-safe, just call CoInitializeEx() to ask for an STA. Works as well in a out-of-process server as it does in a in-process server. If you are only doing this to solve a threading problem then definitely favor in-process STA. Message loop required.

这篇关于进程外COM单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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