BOOL CMAPIEx::GetExEmail(SBinary entryID, CString& strEmail) {
    BOOL bResult = FALSE;

#ifndef _WIN32_WCE
    if (!m_pSession) return FALSE;

    LPADRBOOK pAddressBook;
    if (m_pSession->OpenAddressBook(0, NULL, AB_NO_DIALOG, &pAddressBook) == S_OK) {
        ULONG     ulObjType;
        IMAPIProp *pItem = NULL;
        if (pAddressBook->OpenEntry(entryID.cb, (ENTRYID *)entryID.lpb, NULL, MAPI_BEST_ACCESS, &ulObjType, (LPUNKNOWN *)&pItem) == S_OK) {
            if (ulObjType == MAPI_MAILUSER) {
                LPSPropValue pProp;
                ULONG        ulPropCount;
                ULONG        p[2] = { 1, PR_SMTP_ADDRESS };

                if (pItem->GetProps((LPSPropTagArray)p, CMAPIEx::cm_nMAPICode, &ulPropCount, &pProp) == S_OK) {
                    strEmail = CMAPIEx::GetValidString(*pProp);
                    MAPIFreeBuffer(pProp);
                    bResult = TRUE;
                }
            }
            RELEASE(pItem);
        }
        RELEASE(pAddressBook);
    }
#endif
    return bResult;
}
Beispiel #2
0
	bool COLAddrBook::SessionLogOn(ULONG hWnd, LPCTSTR profile, LPCTSTR pswd, ULONG flags)
	{
		bool bResult = false;
		LPMAPISESSION pSession = NULL;
		HRESULT hr = g_pMAPIEDK->pMAPILogonEx(hWnd, (LPTSTR)profile, (LPTSTR)pswd, flags, &pSession);

		if (pSession)
		{
			LPADRBOOK pAddrBook = NULL;
			if (SUCCEEDED(pSession->OpenAddressBook(NULL, NULL, AB_NO_DIALOG, &pAddrBook)))
			{
				m_initRef->SetAddrBook(pAddrBook);

				// открываем корневой каталог
				ULONG ulObjType = 0;
				LPUNKNOWN pUnk = NULL;
				if (SUCCEEDED(pAddrBook->OpenEntry(0, NULL, NULL, MAPI_BEST_ACCESS, &ulObjType, &pUnk)))
				{
					if (MAPI_ABCONT == ulObjType)
					{
						LPABCONT pContainer = NULL;
						m_pABCont = (LPABCONT)pUnk;
						m_pABCont->AddRef();
						bResult = true;
					}			
				}

				if (pUnk)
					pUnk->Release();
			}

			if (pAddrBook)
				pAddrBook->Release();
		}

		if (pSession)
			pSession->Release();

		return bResult;
	}
Beispiel #3
0
void CProcessor::ImportWAB()
{
	HINSTANCE hinstLib;
	HRESULT hRes;
	LPADRBOOK lpAdrBook;
	LPWABOBJECT lpWABObject;
	DWORD Reserved2 = NULL;

	fWABOpen procWABOpen;

    {
        TCHAR  szWABDllPath[MAX_PATH];
        DWORD  dwType = 0;
        ULONG  cbData = sizeof(szWABDllPath);
        HKEY hKey = NULL;

        *szWABDllPath = '\0';
        
        // First we look under the default WAB DLL path location in the
        // Registry. 
        // WAB_DLL_PATH_KEY is defined in wabapi.h
        //
        if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, WAB_DLL_PATH_KEY, 0, KEY_READ, &hKey))
            RegQueryValueEx( hKey, "", NULL, &dwType, (LPBYTE) szWABDllPath, &cbData);

        if(hKey) RegCloseKey(hKey);

        // if the Registry came up blank, we do a loadlibrary on the wab32.dll
        // WAB_DLL_NAME is defined in wabapi.h
        //
        hinstLib = LoadLibrary( (lstrlen(szWABDllPath)) ? szWABDllPath : WAB_DLL_NAME );
    }

	if (hinstLib != NULL)
	{
		procWABOpen = (fWABOpen) GetProcAddress(hinstLib, "WABOpen");

		if (procWABOpen != NULL)
		{
			hRes = (procWABOpen)(&lpAdrBook,&lpWABObject,NULL,Reserved2); // WABOpen
			_ASSERTE(hRes == S_OK);
			if (hRes != S_OK) return;

			ULONG lpcbEntryID;
			ENTRYID *lpEntryID;
			hRes = lpAdrBook->GetPAB(
				&lpcbEntryID,
				&lpEntryID
			);
			_ASSERTE(hRes == S_OK);
			if (hRes != S_OK) return;

			ULONG ulFlags = MAPI_BEST_ACCESS;
			ULONG ulObjType = NULL;
			LPUNKNOWN lpUnk = NULL;
			hRes = lpAdrBook->OpenEntry(
				lpcbEntryID,
				lpEntryID,
				NULL,
				ulFlags,
				&ulObjType,
				&lpUnk
			);

			ulFlags = NULL;
			//IABTable *lpTable;
			
			if (ulObjType == MAPI_ABCONT)
			{
				IABContainer *lpContainer = static_cast <IABContainer *>(lpUnk);
				LPMAPITABLE lpTable = NULL;
				hRes = lpContainer->GetContentsTable(
					ulFlags,
					&lpTable
				);

				_ASSERT(lpTable);
				ULONG ulRows, ulFound = 0, ulExisted = 0;
				hRes = lpTable->GetRowCount(0,&ulRows);
				_ASSERTE(hRes == S_OK);

				SRowSet *lpRows;

				hRes = lpTable->SetColumns( (LPSPropTagArray)&ptaEid, 0 );

				hRes = lpTable->QueryRows(
					ulRows,		// Get all Rows
					0,
					&lpRows
				);

				for(ULONG i=0;i<lpRows->cRows;i++)
				{
					bool bBirthdayProcessed = false;
					CBirthday* pbd = new CBirthday;
					SRow *lpRow = &lpRows->aRow[i];
					for(ULONG j=0;j<lpRow->cValues;j++)
					{
						SPropValue *lpProp = &lpRow->lpProps[j];
						if(lpProp->ulPropTag == PR_BIRTHDAY)
						{
							SYSTEMTIME st;
							FileTimeToSystemTime(&lpProp->Value.ft,&st);

							pbd->m_date = CDate((BYTE)st.wDay, (BYTE)st.wMonth, (UINT)st.wYear);
							bBirthdayProcessed = true;
						}
						if(lpProp->ulPropTag == PR_DISPLAY_NAME_A)
						{
							pbd->m_name = lpProp->Value.lpszA;
						}
					}
					if( bBirthdayProcessed && pbd->m_date.ValidDate() )
					{
						ulFound++;
						if( !WABExists(pbd) )
						{
							pbd->m_medium = MEDIUM_WAB;
							Add( pbd );
						}
						else
						{
							delete pbd;
							ulExisted++;
						}
					}
					else
						delete pbd;

					lpWABObject->FreeBuffer(lpRow);
				}
				CString c;
				c.Format( "Processed %d contacts\r\n\r\nFound %d birthdays of which\r\n%d already existed",
					ulRows, ulFound, ulExisted );
				AfxMessageBox( c, MB_OK|MB_SYSTEMMODAL, 0 );

				lpWABObject->FreeBuffer(lpRows);
			}
			if(lpAdrBook)
				lpAdrBook->Release();

			if(lpWABObject)
				lpWABObject->Release();

		}
// This would be nice but leads to crashing Trillian
//		FreeLibrary(hinstLib);
	}
}
// Set address list search order
STDMETHODIMP SetAddressListSearchOrder(IMAPISession &Session, const list<string> &SearchList) {
   HRESULT hr;
   LPADRBOOK lpAddrBook = NULL;
   LPVOID tempLink;
   SRowSet *NewSRowSet = NULL;

   // New SRow list of search path
   list<SRow> NewSRowList;

   // Corresponding SPropValue's for SRow.lpProps in NewSRowList
   list<SPropValue> NewSPropList;

   // Setup struct specifying MAPI fields to query
   enum {
        abPR_ENTRYID,         // Field index for ENTRYID
        abPR_DISPLAY_NAME_A,  // Field index for display name
        abNUM_COLS            // Automatically set to number of fields
   };
   static SizedSPropTagArray(abNUM_COLS, abCols) = {
        abNUM_COLS,        // Num fields to get (2)
        PR_ENTRYID,        // Get ENTRYID struct
        PR_DISPLAY_NAME_A  // Get display name
   };

   // Open address book
   hr = Session.OpenAddressBook(NULL, NULL, NULL, &lpAddrBook);
   if (FAILED(hr)) {
      cerr << "Error getting MAPI Address book." << endl;
      goto Exit;
   }

   TraceSearchPath(*lpAddrBook);

   ULONG ulObjType;
   LPMAPICONTAINER pIABRoot = NULL;
   hr = lpAddrBook->OpenEntry(0, NULL, NULL, 0, &ulObjType, (LPUNKNOWN *)&pIABRoot);
   if (FAILED(hr) || ulObjType != MAPI_ABCONT) {
      cerr << "Error opening MAPI Address book root entry." << endl;
      if (SUCCEEDED(hr)) hr = E_UNEXPECTED;
      goto Cleanup;
   }

   // Setup MAPI memory allocation link
   MAPIAllocateBuffer(0, &tempLink);

   // Query MAPI for all address lists
   LPMAPITABLE pHTable = NULL;
   hr = pIABRoot->GetHierarchyTable(CONVENIENT_DEPTH, &pHTable);
   if (FAILED(hr)) {
      cerr << "Error obtaining MAPI address list hierarchy." << endl;
      goto Cleanup;
   }

   LPSRowSet pQueryRows = NULL;
   hr = HrQueryAllRows(pHTable, (LPSPropTagArray)&abCols, NULL, NULL, 0, &pQueryRows);
   if (FAILED(hr)) {
      cerr << "Error getting MAPI address lists." << endl;
      goto Cleanup;
   }

   // Cross reference pQueryRows with SearchList for matches
   for (list<string>::const_iterator SearchListIter = SearchList.begin(); SearchListIter != SearchList.end(); SearchListIter++) {
      const string &SearchName = *SearchListIter;

      // Is SearchName in the pQueryRows list?
      for (ULONG i = 0; i < pQueryRows->cRows && pQueryRows->aRow[i].lpProps[abPR_DISPLAY_NAME_A].ulPropTag == PR_DISPLAY_NAME_A; i++) {
         SRow &QueryRow = pQueryRows->aRow[i];
         string ContainerName = QueryRow.lpProps[abPR_DISPLAY_NAME_A].Value.lpszA;

         if (ContainerName == SearchName) {
            // Found a match!
            cout << "Adding address list search path: " << SearchName << endl;

            // Build SRow/SPropValue structs
            // Assumptions: SRow contains 1 SPropValue of type SBinary
            SPropValue TmpSPropValue = { QueryRow.lpProps[0].ulPropTag, QueryRow.lpProps[0].dwAlignPad };
            NewSPropList.push_back(TmpSPropValue);
            SPropValue &NewSPropValue = NewSPropList.back();

            SRow TmpSRow = { QueryRow.ulAdrEntryPad, 1, &NewSPropValue };
            NewSRowList.push_back(TmpSRow);
            SRow &NewSRow = NewSRowList.back();

            // Safely copy binary portion of SPropValue
            hr = CopySBinary(
               NewSRow.lpProps[0].Value.bin,
               QueryRow.lpProps[0].Value.bin,
               tempLink);
            if (FAILED(hr)) {
               cerr << "Error while building MAPI data." << endl;
               goto Cleanup;
            }

            // break out of inner pQueryRows loop and continue to next in SearchList
            break;
         }
      } // for (i in pQueryRows)
   } // for (SearchList)

   // Convert NewSRowList to SRowSet
   NewSRowSet = AllocSRowSet(NewSRowList, tempLink);
   if (NewSRowSet == NULL) goto Cleanup;

   hr = lpAddrBook->SetSearchPath(0, NewSRowSet);
   if (FAILED(hr)) {
      cerr << "Error while saving address list search path" << endl;
      goto Cleanup;
   }

   TraceSearchPath(*lpAddrBook);

Cleanup:
   if (NewSRowSet) delete[] NewSRowSet;
   MAPIFreeBuffer(tempLink);
   if (lpAddrBook) lpAddrBook->Release();
Exit:
   if (FAILED(hr)) cerr << "HRESULT = 0x" << hex << hr << endl;
   return hr;
}
BOOL ReadWAB(LPADRBOOK pAdrBook, 
			 LPWABOBJECT pWABObject, 
			 AdrBookTable **ppTable,
			 UINT *pNumEntries)
{
    ULONG ulObjType =   0;
	LPMAPITABLE lpAB =  NULL;
    LPTSTR * lppszArray=NULL;
    ULONG cRows =       0;
    LPSRowSet lpRow =   NULL;
	LPSRowSet lpRowAB = NULL;
    LPABCONT  lpContainer = NULL;
	int cNumRows = 0;
    int nRows=0;
	int nCount = 0;

    HRESULT hr = E_FAIL;

    ULONG lpcbEID;
	LPENTRYID lpEID = NULL;

    // Get the entryid of the root PAB container
    //
    hr = pAdrBook->GetPAB( &lpcbEID, &lpEID);

	ulObjType = 0;

    // Open the root PAB container
    // This is where all the WAB contents reside
    //
    hr = pAdrBook->OpenEntry(lpcbEID,
					    		(LPENTRYID)lpEID,
						    	NULL,
							    0,
							    &ulObjType,
							    (LPUNKNOWN *)&lpContainer);

	pWABObject->FreeBuffer(lpEID);

	lpEID = NULL;
	
    if(HR_FAILED(hr))
        goto exit;

    // Get a contents table of all the contents in the
    // WABs root container
    //
    hr = lpContainer->GetContentsTable( 0,
            							&lpAB);

    if(HR_FAILED(hr))
        goto exit;

    // Order the columns in the ContentsTable to conform to the
    // ones we want - which are mainly DisplayName, EntryID and
    // ObjectType
    // The table is gauranteed to set the columns in the order 
    // requested
    //
	hr =lpAB->SetColumns( (LPSPropTagArray)&ptaEid, 0 );

    if(HR_FAILED(hr))
        goto exit;


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

    if(HR_FAILED(hr))
        goto exit;

    // Read all the rows of the table one by one
    //
	do {

		hr = lpAB->QueryRows(1,	0, &lpRowAB);

        if(HR_FAILED(hr))
            break;

        if(lpRowAB)
        {
            cNumRows = lpRowAB->cRows;

		    if (cNumRows)
		    {
                LPTSTR szName = lpRowAB->aRow[0].lpProps[ieidPR_DISPLAY_NAME].Value.lpszA;
				LPTSTR szEmail = NULL;
                LPENTRYID lpEID = (LPENTRYID) lpRowAB->aRow[0].lpProps[ieidPR_ENTRYID].Value.bin.lpb;
                ULONG cbEID = lpRowAB->aRow[0].lpProps[ieidPR_ENTRYID].Value.bin.cb;
				LPMAILUSER pMailUser = NULL;
				ULONG ulObjType = 0;
				ULONG ulValues;
				LPSPropValue lpPropArray;

				*ppTable = (AdrBookTable *) realloc(*ppTable, 
										sizeof(AdrBookTable) * (nCount+1));

				(*ppTable)[nCount].szName = 
										(char *) calloc(strlen(szName)+1, 1);
				strcpy((*ppTable)[nCount].szName, szName);

				if (lpRowAB->aRow[0].lpProps[ieidPR_OBJECT_TYPE].Value.l == 
					MAPI_MAILUSER)
				{
					pAdrBook->OpenEntry(cbEID,
								lpEID,
								NULL,         // interface
								0,            // flags
								&ulObjType,
								(LPUNKNOWN *)&pMailUser);

					if(pMailUser)
					{
						pMailUser->GetProps((LPSPropTagArray) &ptaEmail, 
									0, 
									&ulValues, 
									&lpPropArray);
						
						pMailUser->Release();
					}
					
					if ((lpPropArray[iemailPR_EMAIL_ADDRESS].ulPropTag & 
							0xffff) == PT_ERROR)
					{
						szEmail = 
							lpPropArray[iemailPR_DISPLAY_NAME].Value.lpszA;
					}
					else
						szEmail = 
							lpPropArray[iemailPR_EMAIL_ADDRESS].Value.lpszA;
					
					if (szEmail != NULL)
					{
						(*ppTable)[nCount].szEmail = 
										(char *) calloc(strlen(szEmail)+1, 1);
	
						strcpy((*ppTable)[nCount].szEmail, szEmail);
					}
					else
					{
						(*ppTable)[nCount].szEmail = 
										(char *) calloc(strlen("")+1, 1);

						strcpy((*ppTable)[nCount].szEmail, "");
					}

					pWABObject->FreeBuffer(lpPropArray);
				}
				else
					(*ppTable)[nCount].szEmail = NULL;
				
				nCount++;
		    }
		    FreeProws(pWABObject, lpRowAB);		
        }

	}while ( SUCCEEDED(hr) && cNumRows && lpRowAB)  ;

exit:

	if ( lpContainer )
		lpContainer->Release();

	if ( lpAB )
		lpAB->Release();

	*pNumEntries = nCount;

	if (SUCCEEDED(hr))
		return TRUE;
	else
		return FALSE;
}
// Resolve address list name to ENTRYID
// Memory is allocated through MAPIAllocateBuffer using pAllocLink
STDMETHODIMP ResolveAddressList(IMAPISession &Session, const string &AddressList, LPVOID pAllocLink, ULONG *cbEntry, LPENTRYID *Entry) {
   HRESULT hr = S_OK;

   // Setup struct specifying MAPI fields to query
   enum {
        abPR_ENTRYID,         // Field index for ENTRYID
        abPR_DISPLAY_NAME_A,  // Field index for display name
        abNUM_COLS            // Automatically set to number of fields
   };
   static SizedSPropTagArray(abNUM_COLS, abCols) = {
        abNUM_COLS,        // Num fields to get (2)
        PR_ENTRYID,        // Get ENTRYID struct
        PR_DISPLAY_NAME_A  // Get display name
   };

   // Open address book
   LPADRBOOK lpAddrBook;
   hr = Session.OpenAddressBook(NULL, NULL, NULL, &lpAddrBook);
   if (FAILED(hr)) {
      cerr << "Error getting MAPI Address book." << endl;
      goto Exit;
   }

   ULONG ulObjType;
   LPMAPICONTAINER pIABRoot = NULL;
   hr = lpAddrBook->OpenEntry(0, NULL, NULL, 0, &ulObjType, (LPUNKNOWN *)&pIABRoot);
   if (FAILED(hr) || ulObjType != MAPI_ABCONT) {
      cerr << "Error opening MAPI Address book root entry." << endl;
      if (SUCCEEDED(hr)) hr = E_UNEXPECTED;
      goto Cleanup;
   }

   // Query MAPI for all address lists
   LPMAPITABLE pHTable = NULL;
   hr = pIABRoot->GetHierarchyTable(CONVENIENT_DEPTH, &pHTable);
   if (FAILED(hr)) {
      cerr << "Error obtaining MAPI address list hierarchy." << endl;
      goto Cleanup;
   }

   LPSRowSet pQueryRows = NULL;
   hr = HrQueryAllRows(pHTable, (LPSPropTagArray)&abCols, NULL, NULL, 0, &pQueryRows);
   if (FAILED(hr)) {
      cerr << "Error getting MAPI address lists." << endl;
      goto Cleanup;
   }

   // Is AddressList in the pQueryRows list?
   for (ULONG i = 0; i < pQueryRows->cRows && pQueryRows->aRow[i].lpProps[abPR_DISPLAY_NAME_A].ulPropTag == PR_DISPLAY_NAME_A; i++) {
      SRow &QueryRow = pQueryRows->aRow[i];
      string ContainerName = QueryRow.lpProps[abPR_DISPLAY_NAME_A].Value.lpszA;

      if (ContainerName == AddressList) {
         // Found a match!
         // Build ENTRYID struct
         ULONG cbNewEntryID = QueryRow.lpProps[abPR_ENTRYID].Value.bin.cb;
         LPENTRYID lpNewEntryID;
         MAPIAllocateMore(cbNewEntryID, pAllocLink, (LPVOID *)&lpNewEntryID);
         memcpy(lpNewEntryID, QueryRow.lpProps[abPR_ENTRYID].Value.bin.lpb, cbNewEntryID);

         // Set return values
         *cbEntry = cbNewEntryID;
         *Entry = lpNewEntryID;

         // Break out
         break;
      }
   }

Cleanup:
   if (lpAddrBook) lpAddrBook->Release();

Exit:
   return hr;
}