Question

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();
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top