sashoalm
sashoalm

Reputation: 79537

Why doesn't MS Word close after I release its last COM interface?

I thought that COM automation objects close themselves when their last interface is released.

However this code, which opens and then releases an interface to MS Word leaves the WINWORD.EXE process running. I've traced it and I know that iUnkn->Release() is being called.

What am I doing wrong here?

if (SUCCEEDED(CoInitialize(NULL)))
{
    CLSID clsid;
    if (SUCCEEDED(CLSIDFromProgID(OLESTR("Word.Application"), &clsid)))
    {
        IUnknown* iUnkn = NULL;
        if (SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_SERVER, Word::IID__Application, (void**) &iUnkn)) && iUnkn)
        {
            iUnkn->Release();
        }
    }

    CoUninitialize();
}

Upvotes: 1

Views: 409

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262999

This is by design.

Nothing forces Word to exit when the last reference to its Application COM interface is released (assuming the reference your code holds actually is the last one). The application can and will remain around to service future requests.

If you want the process to exit, you will have to query the IDispatch interface of Word.Application and call its Quit() method.

Upvotes: 1

Related Questions