Is it possible to have an out-of-process COM serve

2019-01-26 15:49发布

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.

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.

Am I in luck?

标签: c++ com winapi
3条回答
smile是对你的礼貌
2楼-- · 2019-01-26 15:56

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.

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;
}
查看更多
姐就是有狂的资本
3楼-- · 2019-01-26 15:57

You would need a "master" coclass to lock in the EXE instance, similar to an "Application" interface. Locate the CoRegisterClassObject() call for its factory. And change the REGCLS argument to REGCLS_SINGLEUSE.

This will automatically unregister the class factory as soon as the first client connects to it. Calling CoCreateInstance() for that factory again starts a new instance of the server. I think.

查看更多
家丑人穷心不美
4楼-- · 2019-01-26 16:00

I'm pretty sure this is not possible. A COM out-of-proc server has to globally register the class objects it provides (via CoRegisterClassObject); part of this registration is the class GUID. Obviously you cannot register the same GUID twice.

查看更多
登录 后发表回答