1
votes

I have created a COM object server exe which implements a COM Object, and calls to CoRegisterClassObject, and then sleeps for a long time (to prevent the process from exiting)

After running it, I have another COM client exe which calls to CoCreateInstance with the CLSID of the object registered previously on CoRegisterClassObject,

CoCreateInstance freezes the thread, but if I close the COM Server process - then CoCreateInstance returns immediately with "Class not registered.".

Do any of you know what's going on?

Thank you.

1

1 Answers

1
votes

and then sleeps for a long time (to prevent the process from exiting)

Supposedly, server side object lives in STA, which in turn requires message pump/dispatching on its thread to be in good standing. Freezing the thread by sleeping there you block its operation. Client process waits for communication with server process/apartment and expectedly locks as well.

You need to replace your Sleep call with a message pump loop:

    MSG Message;
    while(GetMessage(&Message, NULL, WM_NULL, WM_NULL) > 0)
    {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }

A typical process exit prevention is periodic check for amount of outstanding external COM references. If the counter was ever greater than zero, and is zero at the check moment - it's a good time for exiting then.