2013-11-04 111 views
0

我使用以下代碼捕獲帳戶AutoDiscoverComplete event。 運行捕獲此事件的線程時,出現訪問衝突錯誤,QueryInterface方法失敗。 哪裏可能是問題?捕獲AutoDiscoverComplete事件

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; 
    } 
+0

哪個QueryInetrface?你爲什麼要去IUnknown? –

+0

抱歉,代碼在此指令失敗:'hr = spAccounts-> Item(index,reinterpret_cast (&spAccounts));'' – user12

回答

1

代碼

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

真正需要

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

注單數而不是複數。