IMAPITable :: GetContentsTable + GetRowCount检索不一致的结果

时间:2017-01-17 13:45:30

标签: c++ email outlook mapi

我面临着一个尴尬的场景,其中以下API调用会返回不一致的结果,或者更准确地说,在.Release()收益率之前,我必须Contents table GetRowCount几次任何重要的事情

    if(FAILED(hr = spFolder->CreateFolder(FOLDER_SEARCH, &strOutLookSearchFolder[0],
        nullptr, nullptr, MAPI_UNICODE | OPEN_IF_EXISTS, &spSearchFolder)))
    {
        throw Exception("Failed creating search folder", hr);
    }

    if(FAILED(hr = spSearchFolder->SetSearchCriteria(spSRestriction_.get(),
        spEntryList.get(), RESTART_SEARCH | RECURSIVE_SEARCH | FOREGROUND_SEARCH)))
    {
        throw Exception("Failed setting search criteria", hr);
    }

ATL::CComPtr<IMAPITable> spContentsTable;
    if(FAILED(hr = spSearchFolder->GetContentsTable(0, &spContentsTable)))
    {
        LOG(ERR) << "Failed getting contents table";
        return;
    }

    ULONG ulMailCount = 0;
    if(SUCCEEDED(spContentsTable->GetRowCount(0, &ulMailCount)))
        spDataModelSubFolder->AddMessagesCount(ulMailCount);

修改

上面的代码崩溃(DispatchMessage)天气与否,我进入SetSearchCriteria。

ATL::CComPtr<IMAPISession> spSession;
        if(FAILED(hr = pfnMAPILogonEx_(0, &profile.GetProfileName()[0], NULL,
                                MAPI_NEW_SESSION | MAPI_EXTENDED | /*MAPI_NO_MAIL |*/ MAPI_UNICODE, &spSession)))
                            {
                                continue;
                            }

                ....
                ....
                ....

               auto pfnHrAllocAdviseSink = (decltype(&::HrAllocAdviseSink))GetProcAddress(
                        (HMODULE)spMapiModule_.get(), "HrAllocAdviseSink@12"); 

                    ATL::CComPtr<IMAPIAdviseSink>   spAdviseSink;
                    bool                            bSearchCompleted =  false;
                    if(FAILED(hr = pfnHrAllocAdviseSink(SearchCompleteCallBack, 
                        &bSearchCompleted, &spAdviseSink)))
                    {
                        throw OutlookException("Failed allocating advise sink");
                    }

                    ULONG ulConnection = 0;
                    if(FAILED(hr = spMsgStore->Advise(
                        spPropSearchFolderEntryID->Value.bin.cb,
                        (LPENTRYID)spPropSearchFolderEntryID->Value.bin.lpb,
                        fnevSearchComplete,
                        spAdviseSink,
                        &ulConnection)))
                    {
                        throw OutlookException("Failed creating event sink");
                    }   

                    if(FAILED(hr = spSearchFolder->SetSearchCriteria(spSRestriction_.get(),
                        spEntryList.get(), RESTART_SEARCH | RECURSIVE_SEARCH)))
                    {
                        throw OutlookException("Failed setting search criteria", hr);
                    }

                    MSG msg = { 0 };
                    while (GetMessage(&msg, NULL, 0 ,0))
                    {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                        if (bSearchCompleted)
                            break;              
                    }   

    ....
    ....
    ....

    LONG STDAPICALLTYPE SearchCompleteCallBack(
        LPVOID          lpvContext,
        ULONG           cNotif,
        LPNOTIFICATION  lpNotif)
    {
        bool* pbSearchCompleted = (bool*)lpvContext;
        *pbSearchCompleted = true;
        return SUCCESS_SUCCESS;
    }

enter image description here

0 个答案:

没有答案