Define a COM class that can only be created from the same EXE where it is registered

  • If you have an exe with sta in the main thread
  • Inside the EXE, a second thread is created with another STA.
  • This second STA thread should publish a factory class with CoRegisterClassObject, accessible only from this process.

When I use CoRegisterClassObject with CLSCTX_INPROC_SERVER and REGCLS_MULTIPLEUSE, the main thread creates an object with CLSCTX_ALL, I get a "class not registered" error.

When I use CoRegisterClassObject with CLSCTX_LOCAL_SERVER and REGCLS_MULTIPLEUSE, the main thread can create an object. But the next instance of the program will create a thze object inside the first process.

Using the CLSCTX_INPROC flag inside CoCreateInstance will always say that the class is not registered.

BTW: Since I register the class myself, always, when the program is running, there are no registry keys, except for the mandatory one, for typelib.

A more detailed explanation of why I need it: The created class uses pointers and a function internally created in the context of an EXE. Think that I am programming an Application object that should only support functions within this process, as well as files and objects managed by this session. And I need this COM object because it is used inside the VBScripting host and is again exposed to other COM objects.

Is there a way to register a factory class that only supports support inside my exe?

+4
source share
4 answers

CLSCTX_INPROC_SERVER "" , CLSCTX_LOCAL_SERVER - . , , , , 2+- .

, CoRegisterClassObject COM , , , .

, . RegisterActiveObject "! {CLSID}", , "! {CLSID} -processid" ( IRunningObjectTable::Register ROT RegisterActiveObject), . ROT, STA-, , "" STA.

+1

, CoRegisterClassObject . , CoRegisterInitializeSpy.

CoInitialize[Ex].

IInitializeSpy :

HRESULT CRegisterClassesSpy::PostInitialize(HRESULT hrCoInit, DWORD dwCoInit, DWORD dwNewThreadAptRefs)
{
    if (hrCoInit == S_OK && dwNewThreadAptRefs == 1) {
        if (dwCoInit == COINIT_APARTMENTTHREADED ||
            (dwCoInit == COINIT_MULTITHREADED &&
             InterlockedIncrement(&m_MTAThreads) == 1)) {
            hrCoInit = RegisterClassObjects();
        }
    }
    return hrCoInit;
}

HRESULT CRegisterClassesSpy::PreUninitialize(DWORD dwCurThreadAptRefs)
{
    HRESULT hr = S_OK;
    if (dwCurThreadAptRefs == 1) {
        APTTYPE aptType;
        APTTYPEQUALIFIER aptTypeQualifier;
        hr = CoGetApartmentType(&aptType, &aptTypeQualifier);
        if (SUCCEEDED(hr) &&
            (aptType == APTTYPE_STA ||
             aptType == APTTYPE_MAINSTA ||
             (aptType == APTTYPE_MTA && InterlockedDecrement(&m_MTAThreads) == 0))) {
            hr = RevokeClassObjects();
        }
    }
    return hr;
}

, . (? ?) - DLL.

( ):

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH) {
        g_tlsIndex = TlsAlloc();
        TlsSetValue(g_tlsIndex, HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ULARGE_INTEGER)));
    }
    if (fdwReason == DLL_PROCESS_ATTACH || fdwReason == DLL_THREAD_ATTACH) {
        CRegisterClassesSpy *spy = new CRegisterClassesSpy();
        IInitializeSpy *pSpy;
        spy->QueryInterface(IID_IInitializeSpy, reinterpret_cast<void**>(&pSpy));
        spy->Release();
        ULARGE_INTEGER* pCookie = reinterpret_cast<ULARGE_INTEGER*>(TlsGetValue(g_tlsIndex));
        CoRegisterInitializeSpy(pSpy, pCookie));
        pSpy->Release();
    }
    else if ((fdwReason == DLL_PROCESS_DETACH && lpReserved == NULL) || fdwReason == DLL_THREAD_DETACH) {
        ULARGE_INTEGER* pCookie = reinterpret_cast<ULARGE_INTEGER*>(TlsGetValue(g_tlsIndex));
        CoRevokeInitializeSpy(*pCookie);
    }
    if (fdwReason == DLL_PROCESS_DETACH && lpReserved == NULL) {
        TlsFree(g_tlsIndex);
    }
    return TRUE;
}

- DLL DllMain (, COM), IInitializeSpy , , , .

, , EXE DLL .

:

  • CoCreateInstance, ,


PS: factory (, STA), (GIT), - PostInitialize , factory , , factory .

+2

Roman. R , . , . , ROT.

  • Exe , STA2 .
  • factory (STA2),
  • factory CoRegisterClassObject. IClassFactory IROT. ATL.
  • , STA, IClassFactory IROT . STA. EXE factory IROT.

, COM .

0

, CoRegisterClassObject. - " ", . , .

- , IClassFactory::CreateInstance() , ?

( , IClassFactory , IMarshalInterface )

0

All Articles