是否可以有一个进程外 COM 服务器,其中每个对象实例使用一个单独的 O/S 进程?

2022-01-14 00:00:00 com winapi c++

我有一个遗留的 C++解决方案引擎",我已经将它包装为一个进程内 COM 对象,供只需要一个单个解决方案引擎"的客户端应用程序使用.

I have a legacy C++ "solution engine" that I have already wrapped as an in-process COM object for use by client applications that only require a single "solution engine".

但是,我现在有一个需要多个解决方案引擎"的客户端应用程序.不幸的是,底层遗留代码有足够的全局数据、单例和线程恐怖,给定可用资源,不可能同时在进程中拥有多个实例.

However I now have a client application that requires multiple "solution engines". Unfortunately the underlying legacy code has enough global data, singletons and threading horrors that given available resources it isn't possible to have multiple instances of it in-process simultaneously.

我希望某个善良的灵魂可以告诉我一些 COM 魔法,通过翻转几个注册表设置,可以有一个单独的进程外 COM 服务器(单独的操作系统进程)用于所请求的 COM 对象的每个实例.

What I am hoping is that some kind soul can tell me of some COM magic where with the flip of a couple of registry settings it is possible to have a separate out-of-process COM server (separate operating system process) for each instance of the COM object requested.

我运气好吗?

推荐答案

是的,这是可能的.关键是通过调用 CoRegisterClassObject 来注册你的 coclass, 并在值 REGCLS_SINGLEUSE在 flags 参数中.

Yes, this is possible. The key is to register your coclass by calling CoRegisterClassObject, and OR-in the value REGCLS_SINGLEUSE in the flags parameter.

如果您的项目是 ATL 7.0+ 项目,您可以通过覆盖 CAtlExeModuleT::PreMessageLoop(),负责注册类对象,因此:

If your project is an ATL 7.0+ project, you can do this by overriding CAtlExeModuleT::PreMessageLoop(), which is responsible for registering the class object, thusly:

HRESULT CATLHacksModule::PreMessageLoop(int nShow)
{
    HRESULT hr = RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE);
    if (hr == S_OK)
    {
        if (m_bDelayShutdown && !StartMonitor())
        {
            hr = E_FAIL;
        }
    }
    else
    {
        m_bDelayShutdown = false;
    }
    return hr;
}

相关文章