Question

I'm working on an application (a separate .exe) that communicates with MS Outlook (2007 & 2010) and captures some events. With Outlook 2010 everything works fine, however with Outlook 2007 I have noticed that when I close Outlook only the window is being closed, Outlook process keeps running. Before showing any code, I'd like to know if someone knows where would such behaviour come from? N.B, when I close my application Outlook process is stopped.

this link talks about a similar issue

UPDATE

e.g: Here is how I'm capturing application events : (In my main thread)

CAppEventListener m_pAppEventListener = new CAppEventListener();
m_pAppEventListener->Initialize();

Here is how Initialize method is defined :

void CAppEventListener::Initialize()
{
    COleException l_oleExcep;
    BOOL bResult = m_OutlookApplicationInternal.CreateDispatch( _T("Outlook.Application"),&l_oleExcep );
    //m_LogTrace = new CLogTrace();
    //m_LogTrace->OnStartup(TRUE, TRUE);

    if ( !bResult )
    {
        //CString l_csError;
        //l_csError.Format("0x%08lx",l_oleExcep.m_sc);
        //m_LogTrace->WriteLine("\tERROR\tEchec lors de l'appel de \"Create Dispatch\" code erreur "+l_csError);
        //m_LogTrace->WriteLine("\tINFO\tFermeture d'IAOutlookMonior");
        exit(0);
    }

    AddRef();
    HRESULT hr = AttachToSource(m_OutlookApplicationInternal.m_lpDispatch );
    //m_LogTrace->WriteLine(_T("\tINFO\tSuccès de connexion à Outlook"));
}

And here is the AttachToSource method

STDMETHODIMP CAppEventListener::AttachToSource
                         ( IUnknown* pEventSource )
{
    HRESULT hr = S_OK;
    IConnectionPointContainer* pCPC = NULL;

    hr = pEventSource->QueryInterface( IID_IConnectionPointContainer, (void**)&pCPC );

    if (SUCCEEDED(hr))
    {
        hr = pCPC->FindConnectionPoint( IID_ApplicationEvents, &m_pConnectionPoint );
        if (SUCCEEDED(hr))
        {
            hr = m_pConnectionPoint->Advise( this, &m_dwConnection );
        }

        pCPC->Release();
    }

   return hr;
}

CAppEventListener implements the IDispatch interface. And the m_OutlookApplicationInternal object is an instance of CApplication class. How should I deal with it?

UPDATE 2

If my application process is killed (accidentally) before releasing the references to outlook objects, Outlook will keep running isn't it? how should I deal with this issue?

Was it helpful?

Solution

You need to make sure you do not keep outstanding references to any Outlook objects for any prolonged periods of time - make all variables local to a method, create an instance of the Outlook.Application object, etc. As soon as you are done, release all objects (Marshal.ReleaseComObjects if you are using .Net works great).

OTHER TIPS

Outlook is still running because you still have references to Outlook objects, so it is running in order to service any requests your application may make.

You need to count the number of windows (Explorers and Inspectors) that Outlook has open. This number is available from the Outlook object model.

When this number reaches zero your application should quit.

Alternatively there is an Application_Quit event. You should handle that event and quit when it occurs.

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