捕获AutoDiscoverComplete事件

时间:2013-11-04 14:02:54

标签: c++ outlook

我正在使用以下代码来捕获帐户 AutoDiscoverComplete 事件。 运行捕获此事件的线程时,出现访问冲突错误, QueryInterface 方法失败。 哪个可能是问题?

DWORD WINAPI CaptureAccountDiscovery(LPVOID param)
    {
        CoInitialize(NULL);
        CComPtr<Outlook::_Application> spApplication;

        HRESULT hr = spApplication.CoCreateInstance(__uuidof(Outlook::Application), 0, CLSCTX_LOCAL_SERVER );
        if(SUCCEEDED(hr) && spApplication)
        {
            CComPtr<Outlook::_NameSpace> spSession;

            hr = spApplication->get_Session(reinterpret_cast<Outlook::_NameSpace **>(&spSession));
            if (SUCCEEDED(hr) && spSession)
            {
                CComPtr<Outlook::_Accounts> spAccounts;

                hr = spSession->get_Accounts(reinterpret_cast<Outlook::_Accounts **>(&spAccounts));
                if (SUCCEEDED(hr) && spAccounts)
                {                                       
                    VARIANT index;
                    index.intVal = 1;
                    index.vt = VT_INT;

                    CComPtr<Outlook::_Accounts> spAccounts;
                    hr = spAccounts->Item(index, reinterpret_cast<Outlook::_Account **>(&spAccounts));
                    if (SUCCEEDED(hr) && spAccounts)
                    {                           
                        CComPtr<IConnectionPointContainer> spContainer;
                        HRESULT hr = spAccounts->QueryInterface(__uuidof(IConnectionPointContainer),reinterpret_cast<void **>(&spContainer));
                        if (SUCCEEDED(hr) && spContainer)
                        {   
                            HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

                            CComPtr<CAccountDiscover> spSink = new CAccountDiscover(hEvent);
                            CComPtr<IConnectionPoint> spConnectionPoint;
                            hr = spContainer->FindConnectionPoint(Outlook::CLSID_Accounts, &spConnectionPoint);
                            if (SUCCEEDED(hr) && spConnectionPoint)
                            {
                                DWORD dwCookie = 0;                             
                                CComPtr<IUnknown> spUnknown;
                                hr = spConnectionPoint->QueryInterface(IID_IUnknown, reinterpret_cast<void **>(&spUnknown));
                                if (SUCCEEDED(hr) && spUnknown)
                                {                                   
                                    hr = spConnectionPoint->Advise(spSink, &dwCookie);                                          
                                    if (SUCCEEDED(hr))
                                    {                                   
                                        while(true)
                                        {    
                                            MSG Message;
                                            while(PeekMessage(&Message, NULL, WM_NULL, WM_NULL, PM_REMOVE))
                                            {
                                                TranslateMessage(&Message);
                                                DispatchMessage(&Message);
                                            }   

                                            DWORD dwStatus = WaitForSingleObject(hEvent, 0);
                                            Sleep(1);
                                        }

                                        spConnectionPoint->Unadvise(dwCookie);                                  
                                    }
                                }
                            }
                        }                                               
                    }               
                }
            }

            spApplication.Release();
        }

        CoUninitialize();

    return 0;
    }

1 个答案:

答案 0 :(得分:1)

代码

CComPtr<Outlook::_Accounts> spAccounts;
hr = spAccounts->Item(index, reinterpret_cast<Outlook::_Account **>(&spAccounts));

确实需要

CComPtr<Outlook::_Account> spAccount;
hr = spAccounts->Item(index, &spAccount);

注意单数而不是复数。