Главная / FAQ по Builder'у / 183 Как прочесть содержимое контактов...

183 Как прочесть содержимое контактов (адреса страничек, e-mail адреса, телефоны) из OutLook?


это все в MAPI (не simple MAPI)

// mapiscan.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "mapiscan.h"

#include "MAPIASST.H"

#include <mapix.h>
#include <mapiutil.h>
#include <mapitags.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static LPMAPISESSION pSession = NULL;
static LPADRBOOK pAddressBook = NULL;

/////////////////////////////////////////////////////////////////////////////

static BOOL FdgInitialize(
    LPCTSTR lpszProfule,
    LPCTSTR lpszPassword
    )
{
    HRESULT hr;
    HWND hparent = NULL;    // parent window

    hr = ::MAPIInitialize(NULL);
    if (FAILED(hr))
    {
        ::MessageBox(hparent, _T("MAPIInitialize() failure."), _T("mapi"),
MB_OK |
MB_ICONERROR);
        return FALSE;
    }

#if defined (_UNICODE) || defined(UNICODE)

    hr = ::MAPILogonEx((ULONG) hparent, NULL, NULL, 
        MAPI_LOGON_UI | MAPI_EXTENDED | MAPI_NEW_SESSION | MAPI_TIMEOUT_SHORT |
MAPI_UNICODE, 
        & pSession);
#else

    hr = ::MAPILogonEx((ULONG) hparent, NULL, NULL, 
        MAPI_LOGON_UI | MAPI_EXTENDED | MAPI_NEW_SESSION | MAPI_TIMEOUT_SHORT, 
        & pSession);

#endif

    if (FAILED(hr))
    {
        ::MessageBox(hparent, _T("MAPILogonEx() failure."), _T("mapi"), MB_OK |
MB_ICONERROR);
        return FALSE;
    }

    //hr = pSession->OpenAddressBook((ULONG) hparent, NULL, AB_NO_DIALOG, &
//pAddressBook);
    hr = pSession->OpenAddressBook((ULONG) hparent, NULL, 0, & pAddressBook);
    if (SUCCEEDED(hr))
    {

        HWND hdlg = hparent;    // parent window
        //ULONG cbEID;
        //LPENTRYID lpEID = NULL;
        //hr = pAddressBook->GetPAB(& cbEID, &lpEID);
        //ASSERT(SUCCEEDED(hr));

        ADRPARM adrparm = {0};
        LPTSTR rglpszDestTitles[] = { (LPTSTR) "To", (LPTSTR) "From", };
        ULONG rgulDestComps[] = { MAPI_TO, MAPI_ORIG, };

        LPADRLIST lpadr = NULL; // result

        //adrparm.cbABContEntryID = cbEID;
        //adrparm.lpABContEntryID = lpEID;
        //adrparm.ulFlags = ADDRESS_ONE | DIALOG_MODAL | AB_RESOLVE;
        adrparm.ulFlags = DIALOG_MODAL | AB_RESOLVE;
        adrparm.lpszCaption = (LPTSTR) "Select FAX Addresses";  // Only narrow
//characters support.
        adrparm.lpszNewEntryTitle = (LPTSTR) "New FAX Addresses";
        adrparm.lpszDestWellsTitle = (LPTSTR) "FAX Addresses:";
        adrparm.cDestFields = 2;
        adrparm.lppszDestTitles = rglpszDestTitles;
        adrparm.lpulDestComps = rgulDestComps;
        adrparm.lpContRestriction = NULL;
#if 0        
        hr = pAddressBook->Address((ULONG FAR*)& hdlg, &adrparm, &lpadr);


        if (SUCCEEDED(hr))
        {
            // Use array of addresses.

            // Release array
            MAPIFreeBuffer(lpadr);
        }

#endif
        //MAPIFreeBuffer(lpEID);

        return TRUE;
    }

    else
    {
        ::MessageBox(NULL, _T("OpenAddressBook() failure."), _T("mapi"), MB_OK 
|
MB_ICONERROR);
        return FALSE;
    }
}

/////////////////////////////////////////////////////////////////////////////

static void FdgUninitialize()
{
    HRESULT hr;

    pAddressBook->Release();

    hr = pSession->Logoff(0, 0, 0);

    ::MAPIUninitialize();
}

static BOOL ApplayRestrictions(
    LPMAPITABLE lpAB
    )
{
    HRESULT hr;

    SPropValue   prop1;
    SRestriction Restrict1 [2];
    SRestriction Restrict2;

    prop1.ulPropTag = PR_ADDRTYPE_A;
    prop1.Value.lpszA = "FAX";

    Restrict1[0].rt = RES_PROPERTY;
    Restrict1[0].res.resProperty.relop = RELOP_EQ;
    Restrict1[0].res.resProperty.ulPropTag = PR_ADDRTYPE_A; //PR_ADDRTYPE_W;
    Restrict1[0].res.resProperty.lpProp = & prop1;

    Restrict1[1].rt = RES_EXIST;
    Restrict1[1].res.resExist.ulPropTag = PR_ADDRTYPE_A; //PR_ADDRTYPE_W;

    Restrict2.rt = RES_AND;
    Restrict2.res.resAnd.cRes = 2;
    Restrict2.res.resAnd.lpRes = Restrict1;

    hr = lpAB->Restrict(& Restrict1 [0], 0);

    if (hr == MAPI_E_TOO_COMPLEX)
        return FALSE;

    if (! SUCCEEDED(hr))
        return FALSE;
    return TRUE;
}

static ULONG getabcount(
    LPADRBOOK pAddressBook
    )
{
    ULONG nRows = 0;    

    HRESULT hr;

    ULONG cbEID;
    LPENTRYID lpEID = NULL;
    hr = pAddressBook->GetPAB( &cbEID, &lpEID);
    ASSERT(SUCCEEDED(hr));

    ULONG ulObjType = 0;

    LPABCONT  lpContainer;

    // Open the root PAB container
    // This is where all the WAB contents reside
    //
    hr = pAddressBook->OpenEntry(cbEID,
                            (LPENTRYID)lpEID,
                            NULL,
                            0 ,
                            &ulObjType,
                            (LPUNKNOWN *)&lpContainer);
    if (! SUCCEEDED(hr))
        return 0;

    //IMapiTable
    LPMAPITABLE lpAB =  NULL;
    hr = lpContainer->GetContentsTable(0, & lpAB);
    //hr = lpContainer->GetContentsTable(MAPI_UNICODE, & lpAB);
    ASSERT(SUCCEEDED(hr));

    ApplayRestrictions(lpAB);
    SHOWTABLE(lpAB);

    hr = lpAB->GetRowCount(0, & nRows);
    ASSERT(SUCCEEDED(hr));

    lpAB->Release();
    lpContainer->Release();
    MAPIFreeBuffer(lpEID);

    return nRows;
}

static CString getprop(
    LPADRBOOK pAddressBook,
    ULONG uRowID,
    ULONG uColumnType
    )
{
    HRESULT hr;

    CString jstmp;

    ULONG lpcbEID;
    LPENTRYID lpEID = NULL;
    hr = pAddressBook->GetPAB( &lpcbEID, &lpEID);
    if (!SUCCEEDED(hr))
    {
        return _T("");
    }

    ULONG ulObjType = 0;

    LPABCONT  lpContainer;

    // Open the root PAB container
    // This is where all the WAB contents reside
    //
    hr = pAddressBook->OpenEntry(lpcbEID,
                            (LPENTRYID)lpEID,
                            NULL,
                            0,
                            &ulObjType,
                            (LPUNKNOWN *)&lpContainer);
    if (! SUCCEEDED(hr))
    {
        MAPIFreeBuffer(lpEID);
        return _T("");
    }
    //IMapiTable
    LPMAPITABLE lpAB =  NULL;
    hr = lpContainer->GetContentsTable(0, & lpAB);
    if (! SUCCEEDED(hr))
    {
        MAPIFreeBuffer(lpEID);
        return _T("");
    }

    if (!ApplayRestrictions(lpAB))
    {
        MAPIFreeBuffer(lpEID);
        return _T("");
    }

    ///////////////////////////////////
    //
    enum {
        ieidPR_OBJECT_TYPE = 0,
        ieidPR_XXXX,
        ieidMax
    };
    
    SizedSPropTagArray(ieidMax, ptaEid)=
    {
        ieidMax,
        {
            PR_OBJECT_TYPE,
            0,
        }
    };

    ptaEid.aulPropTag [1] = uColumnType;    // !!!!!!!!!!!!!!

    hr = lpAB->SetColumns((LPSPropTagArray)& ptaEid, 0);
    ASSERT(SUCCEEDED(hr));

    // Reset to the beginning of the table
    //
    hr = lpAB->SeekRow(BOOKMARK_BEGINNING, uRowID, NULL);
    ASSERT(SUCCEEDED(hr));

    // Read all the rows of the table one by one
    //
    LPSRowSet lpRowAB;
    //
    hr = lpAB->QueryRows(1, TBL_NOADVANCE, & lpRowAB);
    if (SUCCEEDED(hr))
    {
        ULONG n;
        //
        for (n = 0; n < lpRowAB->cRows; ++ n)
        {
            // We will now take the entry-id of each object and cache it
            // on the listview item representing that object. This enables
            // us to uniquely identify the object later if we need to
            //
            if (lpRowAB->aRow[n].lpProps[ieidPR_XXXX].ulPropTag == uColumnType)
            {
                // Workarounds for explicity passed _A and _W suffixes
                // in property tag.
                //jstmp = (lpRowAB->aRow[n].lpProps[ieidPR_XXXX].Value.LPSZ);
                if (PROP_TYPE(uColumnType) == PT_UNICODE)
                    jstmp =
(lpRowAB->aRow[n].lpProps[ieidPR_XXXX].Value.lpszW);
                else
                    jstmp =
(lpRowAB->aRow[n].lpProps[ieidPR_XXXX].Value.lpszA);
            }
            else
            {
                //jstmp.Format(_T("!!!type error!!! %lx"),
//lpRowAB->aRow[n].lpProps[ieidPR_EMAIL_ADDRESS].ulPropTag);
                jstmp = _T("");
            }

        }
        FreeProws(lpRowAB);     
    }


    lpContainer->Release();
    lpAB->Release();
    MAPIFreeBuffer(lpEID);

    return jstmp;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

void WriteUnicodeHeader()
{
    HANDLE hFile = ::GetStdHandle(STD_OUTPUT_HANDLE);
    USHORT signature = 0xfeff;
    DWORD ndwUnused;

    if (::GetFileType(hFile) != FILE_TYPE_CHAR)
        ::WriteFile(hFile, & signature, 2, & ndwUnused, NULL);
}

void DisplayString(
    LPCTSTR s
    )
{
    HANDLE hFile = ::GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD ndwUnused;

    if (::GetFileType(hFile) != FILE_TYPE_CHAR)
        ::WriteFile(hFile, s, _tcslen(s) * sizeof (* s), & ndwUnused, NULL);
    else
        ::WriteConsole(hFile, s, _tcslen(s), & ndwUnused, NULL);
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        nRetCode = 1;
    }
    else
    {
        if (FdgInitialize(NULL, NULL) == FALSE)
            return 1;

        ULONG nrows = getabcount(pAddressBook);
        ULONG n;

#if defined (_UNICODE) || defined(UNICODE)
        WriteUnicodeHeader();
#endif
        //for (;;)
            for (n = 0; n < nrows; ++ n)
            {
                CString s0;
                CString s1;
                CString s2;

                s0 = getprop(pAddressBook, n, PR_DISPLAY_NAME_A);
                s1 = getprop(pAddressBook, n, PR_EMAIL_ADDRESS_A);
                s2 = getprop(pAddressBook, n, PR_BUSINESS_FAX_NUMBER_A);
                

                DisplayString(_T("'"));
                DisplayString(s0);
                DisplayString(_T("','"));
                DisplayString(s1);
                DisplayString(_T("','"));
                DisplayString(s2);
                DisplayString(_T("'"));
                DisplayString(_T("\r\n"));
            }
        //
        FdgUninitialize();
    }
    return nRetCode;
}

Genadi Zawidowski 2:5020/175.2

Предыдущий вопрос   |  Список вопросов   |   Следующий вопрос

источник      На главную      by jenyok
Copyright © 2003 JTSOFT
Сайт управляется системой uCoz