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;
}
Esempio n. 2
0
HRESULT AddRecipientW(LPMESSAGE lpMessage,
	ULONG ulRecipientType,
	LPWSTR szRecipientName){
	HRESULT			hRes = S_OK;
	LPADRLIST		lpAdrList = NULL;  // ModifyRecips takes LPADRLIST
	LPADRBOOK		lpAddrBook = NULL;

	
	hRes = HrAllocAdrList(NUM_RECIP_PROPS, &lpAdrList);
	if (SUCCEEDED(hRes) && lpAdrList)
	{
		// Set up the recipient by indicating how many recipients
		// and how many properties will be set on each recipient.
		lpAdrList->cEntries = 1;	// How many recipients.
		lpAdrList->aEntries[0].cValues = NUM_RECIP_PROPS;  // How many properties per recipient

		lpAdrList->aEntries[0].rgPropVals[p_PR_DISPLAY_NAME_W].ulPropTag = PR_DISPLAY_NAME_W;
		lpAdrList->aEntries[0].rgPropVals[p_PR_RECIPIENT_TYPE].ulPropTag = PR_RECIPIENT_TYPE;

		lpAdrList->aEntries[0].rgPropVals[p_PR_DISPLAY_NAME_W].Value.lpszW = szRecipientName;
		lpAdrList->aEntries[0].rgPropVals[p_PR_RECIPIENT_TYPE].Value.l = ulRecipientType;

		// If everything goes right, add the new recipient to the message
		// object passed into us.
		hRes = lpMessage->ModifyRecipients(MODRECIP_ADD, lpAdrList);
		
	}
	
	if (lpAdrList) FreePadrlist(lpAdrList);
	if (lpAddrBook) lpAddrBook->Release();
	return hRes;
}
Esempio n. 3
0
void CBaseDialog::OnNotificationsOff()
{
	HRESULT hRes = S_OK;

	if (m_ulBaseAdviseConnection && m_lpMapiObjects)
	{
		switch (m_ulBaseAdviseObjectType)
		{
			case MAPI_SESSION:
			{
				LPMAPISESSION lpMAPISession = m_lpMapiObjects->GetSession(); // do not release
				if (lpMAPISession) EC_MAPI(lpMAPISession->Unadvise(m_ulBaseAdviseConnection));
				break;
			}
			case MAPI_STORE:
			{
				LPMDB lpMDB = m_lpMapiObjects->GetMDB(); // do not release
				if (lpMDB) EC_MAPI(lpMDB->Unadvise(m_ulBaseAdviseConnection));
				break;
			}
			case MAPI_ADDRBOOK:
			{
				LPADRBOOK lpAB = m_lpMapiObjects->GetAddrBook(false); // do not release
				if (lpAB) EC_MAPI(lpAB->Unadvise(m_ulBaseAdviseConnection));
				break;
			}
		}
	}

	if (m_lpBaseAdviseSink) m_lpBaseAdviseSink->Release();
	m_lpBaseAdviseSink = NULL;
	m_ulBaseAdviseObjectType = NULL;
	m_ulBaseAdviseConnection = NULL;
} // CBaseDialog::OnNotificationsOff
Esempio n. 4
0
_Check_return_ HRESULT AddRecipient(
	_In_ LPMAPISESSION lpMAPISession,
	_In_ LPMESSAGE lpMessage,
	_In_z_ LPCTSTR szName,
	ULONG ulRecipientType)
{
	HRESULT			hRes = S_OK;
	LPADRLIST		lpAdrList = NULL; // ModifyRecips takes LPADRLIST
	LPADRBOOK		lpAddrBook = NULL;

	enum
	{
		NAME,
		RECIP,
		NUM_RECIP_PROPS
	};

	if (!lpMessage || !lpMAPISession) return MAPI_E_INVALID_PARAMETER;

	EC_MAPI(lpMAPISession->OpenAddressBook(
		NULL,
		NULL,
		NULL,
		&lpAddrBook));

	EC_MAPI(HrAllocAdrList(NUM_RECIP_PROPS, &lpAdrList));

	if (lpAdrList)
	{
		// Setup the One Time recipient by indicating how many recipients
		// and how many properties will be set on each recipient.
		lpAdrList->cEntries = 1;	// How many recipients.
		lpAdrList->aEntries[0].cValues = NUM_RECIP_PROPS; // How many properties per recipient

		// Set the SPropValue members == the desired values.
		lpAdrList->aEntries[0].rgPropVals[NAME].ulPropTag = PR_DISPLAY_NAME;
		lpAdrList->aEntries[0].rgPropVals[NAME].Value.LPSZ = (LPTSTR)szName;

		lpAdrList->aEntries[0].rgPropVals[RECIP].ulPropTag = PR_RECIPIENT_TYPE;
		lpAdrList->aEntries[0].rgPropVals[RECIP].Value.l = ulRecipientType;

		EC_MAPI(lpAddrBook->ResolveName(
			0L,
			fMapiUnicode,
			NULL,
			lpAdrList));

		// If everything goes right, add the new recipient to the message
		// object passed into us.
		EC_MAPI(lpMessage->ModifyRecipients(MODRECIP_ADD, lpAdrList));

		EC_MAPI(lpMessage->SaveChanges(KEEP_OPEN_READWRITE));
	}

	if (lpAdrList) FreePadrlist(lpAdrList);
	if (lpAddrBook) lpAddrBook->Release();
	return hRes;
} // AddRecipient
Esempio n. 5
0
HRESULT AddRecipientA(LPMAPISESSION lpMAPISession,
	LPMESSAGE lpMessage,
	ULONG ulRecipientType,
	LPSTR szRecipientName)
{
	HRESULT			hRes = S_OK;
	LPADRLIST		lpAdrList = NULL;  // ModifyRecips takes LPADRLIST
	LPADRBOOK		lpAddrBook = NULL;

	if (!lpMessage || !lpMAPISession) return MAPI_E_INVALID_PARAMETER;

	hRes = lpMAPISession->OpenAddressBook(
		NULL,
		NULL,
		NULL,
		&lpAddrBook);
	if (SUCCEEDED(hRes) && lpAddrBook)
	{
		hRes = HrAllocAdrList(NUM_RECIP_PROPS, &lpAdrList);
		if (SUCCEEDED(hRes) && lpAdrList)
		{
			// Set up the recipient by indicating how many recipients
			// and how many properties will be set on each recipient.
			lpAdrList->cEntries = 1;	// How many recipients.
			lpAdrList->aEntries[0].cValues = NUM_RECIP_PROPS;  // How many properties per recipient

			lpAdrList->aEntries[0].rgPropVals[p_PR_DISPLAY_NAME_W].ulPropTag = PR_DISPLAY_NAME_A;
			lpAdrList->aEntries[0].rgPropVals[p_PR_RECIPIENT_TYPE].ulPropTag = PR_RECIPIENT_TYPE;

			lpAdrList->aEntries[0].rgPropVals[p_PR_DISPLAY_NAME_W].Value.lpszA = szRecipientName;
			lpAdrList->aEntries[0].rgPropVals[p_PR_RECIPIENT_TYPE].Value.l = ulRecipientType;

			hRes = lpAddrBook->ResolveName(
				0L,
				MAPI_UNICODE,
				NULL,
				lpAdrList);
			if (SUCCEEDED(hRes))
			{
				// If everything goes right, add the new recipient to the message
				// object passed into us.
				hRes = lpMessage->ModifyRecipients(MODRECIP_ADD, lpAdrList);
			}
		}
	}
	if (lpAdrList) FreePadrlist(lpAdrList);
	if (lpAddrBook) lpAddrBook->Release();
	return hRes;
}
Esempio n. 6
0
void FreeWAB(HINSTANCE hWAB, 
			 LPADRBOOK pAdrBook, 
			 LPWABOBJECT pWABObject,
			 AdrBookTable *pTable,
			 UINT nNumEntries)
{
	if (pTable && nNumEntries)
	{
		for (UINT i=0; i<nNumEntries; i++)
		{
			if (pTable[i].szName)
				free(pTable[i].szName);

			if (pTable[i].szEmail)
				free(pTable[i].szEmail);
		}

		free(pTable);
	}

	if(pAdrBook)
		pAdrBook->Release();
	
	if(pWABObject)
		pWABObject->Release();
	
	if(hWAB)
		FreeLibrary(hWAB);

	return;
}
Esempio n. 7
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;
	}
Esempio n. 8
0
// Shows the default Address Book dialog, return FALSE on failure, IDOK or IDCANCEL on success
// I force narrow strings here because it for some reason doesn't work in UNICODE
int CMAPIEx::ShowAddressBook(LPADRLIST& pAddressList, LPCTSTR szCaption)
{
#ifndef _WIN32_WCE
	if(!m_pSession) return FALSE;

	LPADRBOOK pAddressBook;
	if(m_pSession->OpenAddressBook(0, NULL, 0, &pAddressBook)==S_OK) 
	{
		pAddressList=NULL;

		char* lppszDestTitles[]={ "To" };
		ULONG lpulDestComps[]={ MAPI_TO };

		ADRPARM adrparm;
		memset(&adrparm, 0,sizeof(ADRPARM));
		adrparm.ulFlags=DIALOG_MODAL | AB_RESOLVE;

		adrparm.lpszCaption=(LPTSTR)szCaption;
#ifdef UNICODE
		if(szCaption) 
		{
			char szNarrowCaption[256];
			WideCharToMultiByte(CP_ACP, 0, szCaption,-1, szNarrowCaption,255, NULL, NULL);
			adrparm.lpszCaption=(LPTSTR)szNarrowCaption;
		}
#endif
		adrparm.cDestFields = 1;
		adrparm.lppszDestTitles=(LPTSTR*)lppszDestTitles;
		adrparm.lpulDestComps=lpulDestComps;

		HWND hDesktop=::GetDesktopWindow();
		HRESULT hr=pAddressBook->Address((ULONG_PTR*)&hDesktop, &adrparm, &pAddressList);
		RELEASE(pAddressBook);
		if(hr==S_OK) return IDOK;
		if(hr==MAPI_E_USER_CANCEL) return IDCANCEL;
	}
#endif
	return FALSE;
}
Esempio n. 9
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;
}
/// <summary>
/// <para name='Name'>ResolveDisplayName</para>
/// <para name='Purpose'>Get an email address from a Display Name</para>
/// </summary>
/// <param name='szName'>Display Name to resolve</param>
/// <param name='szEmailAddress'>[out] Returned email address</param>
/// <returns>HRESULT</returns>
/// <remarks>
/// <para name='Notes'></para>
/// <para name='Author'>Kenn Guilstorf</para>
/// <para name='LastModified'>28Jan2016</para>
/// </remarks>
STDMETHODIMP ZybraxiSoft::CMailbox::ResolveDisplayName(TSTRING szName, TSTRING &szEmailAddress)
{
	HRESULT hr = S_OK;
	LPADRBOOK lpAdrBook = NULL;
	LPADRLIST lpAdrList = NULL;
	TSTRING szEX = TSTRING(_T("EX"));
	bool isExchange = false;

	enum
	{
		prDISPLAY_NAME,
		NUM_PROPS
	};

	// Check to make sure we have something in szName
	if (szName.empty() || szName.size() <= 0)
	{
		hr = MAPI_E_INVALID_PARAMETER;
		goto EXIT;
	}

	// Log what we're trying to resolve
	m_log << output::both << level::Informational <<
		"Attempting to resolve '" << szName.c_str() << "'" << endl;

	// Open the address book
	// See: https://msdn.microsoft.com/en-us/library/office/cc815381.aspx
	if (SUCCEEDED(hr = m_lpSession->OpenAddressBook(
		NULL,				// ulUIParam [may be NULL]
		NULL,				// lpInterface [may be NULL; returns IAddrBook:IMAPIProp]
		NULL,				// ulFlags [NULL means no flags]
		&lpAdrBook)))		// lppAdrBook [out]
	{
		// Allocate the address list
		// Note: I like to use 'this' so I know which functions I'm responsible
		//       for if I ever have to work on this...
		if (SUCCEEDED(hr = this->AllocAdrList(
			NUM_PROPS,
			&lpAdrList)))
		{
			// Even though we succeeded, do a check to make sure we have
			//  an address list
			if (lpAdrList)
			{
				lpAdrList->cEntries = 1;	// only looking for 1 entry
				lpAdrList->aEntries[0].cValues = NUM_PROPS;		// Number of props

				// Set the SPropValue to whom we're looking for
				lpAdrList->aEntries[0].rgPropVals[prDISPLAY_NAME].ulPropTag =
					PR_DISPLAY_NAME;
				lpAdrList->aEntries[0].rgPropVals[prDISPLAY_NAME].Value.LPSZ =
					(LPTSTR)szName.c_str();

				// Let's try to resolve the name now
				if (SUCCEEDED(hr = lpAdrBook->ResolveName(
					0L,
					MAPI_UNICODE,
					NULL,
					lpAdrList)))
				{
					m_log << output::both << level::Informational <<
						"Resolve name yielded " <<
						lpAdrList->aEntries[0].cValues <<
						" properties." << endl;

					for (UINT i = 0; i < lpAdrList->aEntries[0].cValues; i++)
					{
						// Store the property so I don't have to type so much...
						SPropValue spvCurrent = lpAdrList->aEntries[0].rgPropVals[i];

						m_log << output::both << level::Informational <<
							"Found Property '0x" <<
							setfill(_T('0')) << setw(8) << setbase(16) <<
							spvCurrent.ulPropTag << "'";

						switch (spvCurrent.ulPropTag)
						{
						case PR_ADDRTYPE:
							if (szEX.compare(spvCurrent.Value.LPSZ) == 0)
								isExchange = true;
							break;
						case PR_DISPLAY_NAME:
							m_log << ": " << spvCurrent.Value.LPSZ;
							break;
						case PR_EMAIL_ADDRESS:
							m_log << ": " << spvCurrent.Value.LPSZ;
							szEmailAddress = TSTRING(spvCurrent.Value.LPSZ);
							break;
						default:
							if (((spvCurrent.ulPropTag & 0x1f) == 0x1f) ||
								((spvCurrent.ulPropTag & 0x1e) == 0x1e))
								m_log << ": " << spvCurrent.Value.LPSZ;
							break;
						}

						m_log << endl;
					}

					if (!isExchange || (szEmailAddress.empty()))
					{
						hr = MAPI_E_NOT_FOUND;
						goto CLEANUP;
					}
				}
				else
				{
					FATAL(_T("lpAdrBook->ResolveName()"), hr);
					goto CLEANUP;
				}
			}
		}
		else
		{
			FATAL(_T("AllocAdrList()"), hr);
			goto CLEANUP;
		}
	}
	else
	{
		FATAL(_T("OpenAddressBook()"), hr);
	}

CLEANUP:
	if (lpAdrList)
	{
		FreePadrlist(lpAdrList);
		lpAdrList = NULL;
	}

	if (lpAdrBook)
	{
		lpAdrBook->Release();
		lpAdrBook = NULL;
	}

EXIT:
	return hr;
}
// 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;
}
Esempio n. 13
0
void CBaseDialog::OnCompareEntryIDs()
{
	HRESULT			hRes = S_OK;
	if (!m_lpMapiObjects) return;

	LPMDB			lpMDB = m_lpMapiObjects->GetMDB(); // do not release
	LPMAPISESSION	lpMAPISession = m_lpMapiObjects->GetSession(); // do not release
	LPADRBOOK		lpAB = m_lpMapiObjects->GetAddrBook(false); // do not release

	CEditor MyEIDs(
		this,
		IDS_COMPAREEIDS,
		IDS_COMPAREEIDSPROMPTS,
		4,
		CEDITOR_BUTTON_OK | CEDITOR_BUTTON_CANCEL);

	MyEIDs.InitPane(0, CreateSingleLinePane(IDS_EID1, NULL, false));
	MyEIDs.InitPane(1, CreateSingleLinePane(IDS_EID2, NULL, false));

	UINT uidDropDown[] = {
		IDS_DDMESSAGESTORE,
		IDS_DDSESSION,
		IDS_DDADDRESSBOOK
	};
	MyEIDs.InitPane(2, CreateDropDownPane(IDS_OBJECTFORCOMPAREEID, _countof(uidDropDown), uidDropDown, true));

	MyEIDs.InitPane(3, CreateCheckPane(IDS_EIDBASE64ENCODED, false, false));

	WC_H(MyEIDs.DisplayDialog());
	if (S_OK != hRes) return;

	if ((0 == MyEIDs.GetDropDown(2) && !lpMDB) ||
		(1 == MyEIDs.GetDropDown(2) && !lpMAPISession) ||
		(2 == MyEIDs.GetDropDown(2) && !lpAB))
	{
		ErrDialog(__FILE__, __LINE__, IDS_EDCOMPAREEID);
		return;
	}
	// Get the entry IDs as a binary
	LPENTRYID lpEntryID1 = NULL;
	size_t cbBin1 = NULL;
	EC_H(MyEIDs.GetEntryID(0, MyEIDs.GetCheck(3), &cbBin1, &lpEntryID1));

	LPENTRYID lpEntryID2 = NULL;
	size_t cbBin2 = NULL;
	EC_H(MyEIDs.GetEntryID(1, MyEIDs.GetCheck(3), &cbBin2, &lpEntryID2));

	ULONG ulResult = NULL;
	switch (MyEIDs.GetDropDown(2))
	{
	case 0: // Message Store
		EC_MAPI(lpMDB->CompareEntryIDs((ULONG)cbBin1, lpEntryID1, (ULONG)cbBin2, lpEntryID2, NULL, &ulResult));
		break;
	case 1: // Session
		EC_MAPI(lpMAPISession->CompareEntryIDs((ULONG)cbBin1, lpEntryID1, (ULONG)cbBin2, lpEntryID2, NULL, &ulResult));
		break;
	case 2: // Address Book
		EC_MAPI(lpAB->CompareEntryIDs((ULONG)cbBin1, lpEntryID1, (ULONG)cbBin2, lpEntryID2, NULL, &ulResult));
		break;
	}

	if (SUCCEEDED(hRes))
	{
		CString szRet;
		CString szResult;
		EC_B(szResult.LoadString(ulResult ? IDS_TRUE : IDS_FALSE));
		szRet.FormatMessage(IDS_COMPAREEIDBOOL, ulResult, szResult);

		CEditor Result(
			this,
			IDS_COMPAREEIDSRESULT,
			NULL,
			(ULONG)0,
			CEDITOR_BUTTON_OK);
		Result.SetPromptPostFix(szRet);
		(void)Result.DisplayDialog();
	}

	delete[] lpEntryID2;
	delete[] lpEntryID1;
} // CBaseDialog::OnCompareEntryIDs
Esempio n. 14
0
// Manually resolve a name in the address book and add it to the message
_Check_return_ HRESULT ManualResolve(
	_In_ LPMAPISESSION lpMAPISession,
	_In_ LPMESSAGE lpMessage,
	_In_z_ LPCTSTR szName,
	ULONG PropTagToCompare)
{
	HRESULT			hRes = S_OK;
	ULONG			ulObjType = 0;
	LPADRBOOK		lpAdrBook = NULL;
	LPSRowSet		lpABRow = NULL;
	LPMAPITABLE		lpABContainerTable = NULL;
	LPADRLIST		lpAdrList = NULL;
	LPABCONT		lpABContainer = NULL;
	LPMAPITABLE		pTable = NULL;
	LPSPropValue	lpFoundRow = NULL;

	enum
	{
		abcPR_ENTRYID,
		abcPR_DISPLAY_NAME,
		abcNUM_COLS
	};

	static const SizedSPropTagArray(abcNUM_COLS, abcCols) =
	{
		abcNUM_COLS,
		PR_ENTRYID,
		PR_DISPLAY_NAME,
	};

	enum
	{
		abPR_ENTRYID,
		abPR_DISPLAY_NAME,
		abPR_RECIPIENT_TYPE,
		abPR_ADDRTYPE,
		abPR_DISPLAY_TYPE,
		abPropTagToCompare,
		abNUM_COLS
	};

	if (!lpMAPISession) return MAPI_E_INVALID_PARAMETER;

	DebugPrint(DBGGeneric, _T("ManualResolve: Asked to resolve \"%s\"\n"), szName);

	EC_MAPI(lpMAPISession->OpenAddressBook(
		NULL,
		NULL,
		NULL,
		&lpAdrBook));

	EC_H(GetABContainerTable(lpAdrBook, &lpABContainerTable));

	if (lpABContainerTable)
	{
		// Restrict the table to the properties that we are interested in.
		EC_MAPI(lpABContainerTable->SetColumns((LPSPropTagArray)&abcCols, TBL_BATCH));

		if (!FAILED(hRes)) for (;;)
		{
			hRes = S_OK;

			FreeProws(lpABRow);
			lpABRow = NULL;
			EC_MAPI(lpABContainerTable->QueryRows(
				1,
				NULL,
				&lpABRow));
			if (FAILED(hRes) || !lpABRow || (lpABRow && !lpABRow->cRows)) break;

			// From this point forward, consider any error an error with the current address book container, so just continue and try the next one.
			if (PR_ENTRYID == lpABRow->aRow->lpProps[abcPR_ENTRYID].ulPropTag)
			{
				DebugPrint(DBGGeneric, _T("ManualResolve: Searching this container\n"));
				DebugPrintBinary(DBGGeneric, &lpABRow->aRow->lpProps[abcPR_ENTRYID].Value.bin);

				if (lpABContainer) lpABContainer->Release();
				lpABContainer = NULL;
				EC_H(CallOpenEntry(
					NULL,
					lpAdrBook,
					NULL,
					NULL,
					lpABRow->aRow->lpProps[abcPR_ENTRYID].Value.bin.cb,
					(ENTRYID*)lpABRow->aRow->lpProps[abcPR_ENTRYID].Value.bin.lpb,
					NULL,
					NULL,
					&ulObjType,
					(LPUNKNOWN*)&lpABContainer));
				if (!lpABContainer) continue;

				DebugPrint(DBGGeneric, _T("ManualResolve: Object opened as 0x%X\n"), ulObjType);

				if (lpABContainer && ulObjType == MAPI_ABCONT)
				{
					if (pTable) pTable->Release();
					pTable = NULL;
					WC_MAPI(lpABContainer->GetContentsTable(fMapiUnicode, &pTable));
					if (!pTable)
					{
						DebugPrint(DBGGeneric, _T("ManualResolve: Container did not support contents table\n"));
						if (MAPI_E_NO_SUPPORT == hRes) hRes = S_OK;
						continue;
					}

					MAPIFreeBuffer(lpFoundRow);
					lpFoundRow = NULL;
					EC_H(SearchContentsTableForName(
						pTable,
						szName,
						PropTagToCompare,
						&lpFoundRow));
					if (!lpFoundRow) continue;

					if (lpAdrList) FreePadrlist(lpAdrList);
					lpAdrList = NULL;
					// Allocate memory for new Address List structure.
					EC_H(MAPIAllocateBuffer(CbNewADRLIST(1), (LPVOID*)&lpAdrList));
					if (!lpAdrList) continue;

					ZeroMemory(lpAdrList, CbNewADRLIST(1));
					lpAdrList->cEntries = 1;
					// Allocate memory for SPropValue structure that indicates what
					// recipient properties will be set. To resolve a name that
					// already exists in the Address book, this will always be 1.

					EC_H(MAPIAllocateBuffer(
						(ULONG)(abNUM_COLS * sizeof(SPropValue)),
						(LPVOID*)&lpAdrList->aEntries->rgPropVals));
					if (!lpAdrList->aEntries->rgPropVals) continue;

					// TODO: We are setting 5 properties below. If this changes, modify these two lines.
					ZeroMemory(lpAdrList->aEntries->rgPropVals, 5 * sizeof(SPropValue));
					lpAdrList->aEntries->cValues = 5;

					// Fill out addresslist with required property values.
					LPSPropValue pProps = lpAdrList->aEntries->rgPropVals;
					LPSPropValue pProp; // Just a pointer, do not free.

					pProp = &pProps[abPR_ENTRYID];
					pProp->ulPropTag = PR_ENTRYID;
					EC_H(CopySBinary(&pProp->Value.bin, &lpFoundRow[abPR_ENTRYID].Value.bin, lpAdrList));

					pProp = &pProps[abPR_RECIPIENT_TYPE];
					pProp->ulPropTag = PR_RECIPIENT_TYPE;
					pProp->Value.l = MAPI_TO;

					pProp = &pProps[abPR_DISPLAY_NAME];
					pProp->ulPropTag = PR_DISPLAY_NAME;

					if (!CheckStringProp(&lpFoundRow[abPR_DISPLAY_NAME], PT_TSTRING)) continue;

					EC_H(CopyString(
						&pProp->Value.LPSZ,
						lpFoundRow[abPR_DISPLAY_NAME].Value.LPSZ,
						lpAdrList));

					pProp = &pProps[abPR_ADDRTYPE];
					pProp->ulPropTag = PR_ADDRTYPE;

					if (!CheckStringProp(&lpFoundRow[abPR_ADDRTYPE], PT_TSTRING)) continue;

					EC_H(CopyString(
						&pProp->Value.LPSZ,
						lpFoundRow[abPR_ADDRTYPE].Value.LPSZ,
						lpAdrList));

					pProp = &pProps[abPR_DISPLAY_TYPE];
					pProp->ulPropTag = PR_DISPLAY_TYPE;
					pProp->Value.l = lpFoundRow[abPR_DISPLAY_TYPE].Value.l;

					EC_MAPI(lpMessage->ModifyRecipients(
						MODRECIP_ADD,
						lpAdrList));

					if (lpAdrList) FreePadrlist(lpAdrList);
					lpAdrList = NULL;

					EC_MAPI(lpMessage->SaveChanges(KEEP_OPEN_READWRITE));

					// since we're done with our work, let's get out of here.
					break;
				}
			}
		}
		lpABContainerTable->Release();
	}
	FreeProws(lpABRow);
	MAPIFreeBuffer(lpFoundRow);
	if (lpAdrList) FreePadrlist(lpAdrList);
	if (pTable) pTable->Release();
	if (lpABContainer) lpABContainer->Release();
	if (lpAdrBook) lpAdrBook->Release();
	return hRes;
} // ManualResolve
Esempio n. 15
0
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;
}
Esempio n. 16
0
void CBaseDialog::OnOpenEntryID(_In_opt_ LPSBinary lpBin)
{
	HRESULT			hRes = S_OK;
	if (!m_lpMapiObjects) return;

	CEditor MyEID(
		this,
		IDS_OPENEID,
		IDS_OPENEIDPROMPT,
		10,
		CEDITOR_BUTTON_OK | CEDITOR_BUTTON_CANCEL);

	MyEID.InitPane(0, CreateSingleLinePane(IDS_EID, BinToHexString(lpBin, false), false));

	LPMDB lpMDB = m_lpMapiObjects->GetMDB(); // do not release
	MyEID.InitPane(1, CreateCheckPane(IDS_USEMDB, lpMDB ? true : false, lpMDB ? false : true));

	LPADRBOOK lpAB = m_lpMapiObjects->GetAddrBook(false); // do not release
	MyEID.InitPane(2, CreateCheckPane(IDS_USEAB, lpAB ? true : false, lpAB ? false : true));

	LPMAPISESSION lpMAPISession = m_lpMapiObjects->GetSession(); // do not release
	MyEID.InitPane(3, CreateCheckPane(IDS_SESSION, lpMAPISession ? true : false, lpMAPISession ? false : true));

	MyEID.InitPane(4, CreateCheckPane(IDS_PASSMAPIMODIFY, false, false));

	MyEID.InitPane(5, CreateCheckPane(IDS_PASSMAPINOCACHE, false, false));

	MyEID.InitPane(6, CreateCheckPane(IDS_PASSMAPICACHEONLY, false, false));

	MyEID.InitPane(7, CreateCheckPane(IDS_EIDBASE64ENCODED, false, false));

	MyEID.InitPane(8, CreateCheckPane(IDS_ATTEMPTIADDRBOOKDETAILSCALL, false, lpAB ? false : true));

	MyEID.InitPane(9, CreateCheckPane(IDS_EIDISCONTAB, false, false));

	WC_H(MyEID.DisplayDialog());
	if (S_OK != hRes) return;

	// Get the entry ID as a binary
	LPENTRYID lpEnteredEntryID = NULL;
	LPENTRYID lpEntryID = NULL;
	size_t cbBin = NULL;
	EC_H(MyEID.GetEntryID(0, MyEID.GetCheck(7), &cbBin, &lpEnteredEntryID));

	if (MyEID.GetCheck(9) && lpEnteredEntryID)
	{
		(void)UnwrapContactEntryID((ULONG)cbBin, (LPBYTE)lpEnteredEntryID, (ULONG*)&cbBin, (LPBYTE*)&lpEntryID);
	}
	else
	{
		lpEntryID = lpEnteredEntryID;
	}

	if (MyEID.GetCheck(8) && lpAB) // Do IAddrBook->Details here
	{
		ULONG_PTR ulUIParam = (ULONG_PTR)(void*)m_hWnd;

		EC_H_CANCEL(lpAB->Details(
			&ulUIParam,
			NULL,
			NULL,
			(ULONG)cbBin,
			lpEntryID,
			NULL,
			NULL,
			NULL,
			DIALOG_MODAL)); // API doesn't like unicode
	}
	else
	{
		LPUNKNOWN lpUnk = NULL;
		ULONG ulObjType = NULL;

		EC_H(CallOpenEntry(
			MyEID.GetCheck(1) ? lpMDB : 0,
			MyEID.GetCheck(2) ? lpAB : 0,
			NULL,
			MyEID.GetCheck(3) ? lpMAPISession : 0,
			(ULONG)cbBin,
			lpEntryID,
			NULL,
			(MyEID.GetCheck(4) ? MAPI_MODIFY : MAPI_BEST_ACCESS) |
			(MyEID.GetCheck(5) ? MAPI_NO_CACHE : 0) |
			(MyEID.GetCheck(6) ? MAPI_CACHE_ONLY : 0),
			&ulObjType,
			&lpUnk));

		if (lpUnk)
		{
			LPWSTR szFlags = NULL;
			InterpretNumberAsStringProp(ulObjType, PR_OBJECT_TYPE, &szFlags);
			DebugPrint(DBGGeneric, _T("OnOpenEntryID: Got object (%p) of type 0x%08X = %ws\n"), lpUnk, ulObjType, szFlags);
			delete[] szFlags;
			szFlags = NULL;

			LPMAPIPROP lpTemp = NULL;
			WC_MAPI(lpUnk->QueryInterface(IID_IMAPIProp, (LPVOID*)&lpTemp));
			if (lpTemp)
			{
				WC_H(DisplayObject(
					lpTemp,
					ulObjType,
					otHierarchy,
					this));
				lpTemp->Release();
			}
			lpUnk->Release();
		}
	}

	delete[] lpEnteredEntryID;
} // CBaseDialog::OnOpenEntryID
Esempio n. 17
0
_Check_return_ HRESULT AddOneOffAddress(
	_In_ LPMAPISESSION lpMAPISession,
	_In_ LPMESSAGE lpMessage,
	_In_z_ LPCTSTR szDisplayName,
	_In_z_ LPCTSTR szAddrType,
	_In_z_ LPCTSTR szEmailAddress,
	ULONG ulRecipientType)
{
	HRESULT hRes = S_OK;
	LPADRLIST lpAdrList = NULL; // ModifyRecips takes LPADRLIST
	LPADRBOOK lpAddrBook = NULL;
	LPENTRYID lpEID = NULL;

	enum
	{
		NAME,
		ADDR,
		EMAIL,
		RECIP,
		EID,
		NUM_RECIP_PROPS
	};

	if (!lpMessage || !lpMAPISession) return MAPI_E_INVALID_PARAMETER;

	EC_MAPI(lpMAPISession->OpenAddressBook(
		NULL,
		NULL,
		NULL,
		&lpAddrBook));

	EC_MAPI(HrAllocAdrList(NUM_RECIP_PROPS, &lpAdrList));

	// Setup the One Time recipient by indicating how many recipients
	// and how many properties will be set on each recipient.

	if (SUCCEEDED(hRes) && lpAdrList)
	{
		lpAdrList->cEntries = 1;	// How many recipients.
		lpAdrList->aEntries[0].cValues = NUM_RECIP_PROPS; // How many properties per recipient

		// Set the SPropValue members == the desired values.
		lpAdrList->aEntries[0].rgPropVals[NAME].ulPropTag = PR_DISPLAY_NAME;
		lpAdrList->aEntries[0].rgPropVals[NAME].Value.LPSZ = (LPTSTR)szDisplayName;

		lpAdrList->aEntries[0].rgPropVals[ADDR].ulPropTag = PR_ADDRTYPE;
		lpAdrList->aEntries[0].rgPropVals[ADDR].Value.LPSZ = (LPTSTR)szAddrType;

		lpAdrList->aEntries[0].rgPropVals[EMAIL].ulPropTag = PR_EMAIL_ADDRESS;
		lpAdrList->aEntries[0].rgPropVals[EMAIL].Value.LPSZ = (LPTSTR)szEmailAddress;

		lpAdrList->aEntries[0].rgPropVals[RECIP].ulPropTag = PR_RECIPIENT_TYPE;
		lpAdrList->aEntries[0].rgPropVals[RECIP].Value.l = ulRecipientType;

		lpAdrList->aEntries[0].rgPropVals[EID].ulPropTag = PR_ENTRYID;

		// Create the One-off address and get an EID for it.
		EC_MAPI(lpAddrBook->CreateOneOff(
			lpAdrList->aEntries[0].rgPropVals[NAME].Value.LPSZ,
			lpAdrList->aEntries[0].rgPropVals[ADDR].Value.LPSZ,
			lpAdrList->aEntries[0].rgPropVals[EMAIL].Value.LPSZ,
			fMapiUnicode,
			&lpAdrList->aEntries[0].rgPropVals[EID].Value.bin.cb,
			&lpEID));
		lpAdrList->aEntries[0].rgPropVals[EID].Value.bin.lpb = (LPBYTE)lpEID;

		EC_MAPI(lpAddrBook->ResolveName(
			0L,
			fMapiUnicode,
			NULL,
			lpAdrList));

		// If everything goes right, add the new recipient to the message
		// object passed into us.
		EC_MAPI(lpMessage->ModifyRecipients(MODRECIP_ADD, lpAdrList));

		EC_MAPI(lpMessage->SaveChanges(KEEP_OPEN_READWRITE));
	}

	MAPIFreeBuffer(lpEID);
	if (lpAdrList) FreePadrlist(lpAdrList);
	if (lpAddrBook) lpAddrBook->Release();
	return hRes;
} // AddOneOffAddress
Esempio n. 18
0
void CBaseDialog::OnNotificationsOn()
{
	HRESULT			hRes = S_OK;

	if (m_lpBaseAdviseSink || !m_lpMapiObjects) return;

	LPMDB			lpMDB = m_lpMapiObjects->GetMDB(); // do not release
	LPMAPISESSION	lpMAPISession = m_lpMapiObjects->GetSession(); // do not release
	LPADRBOOK		lpAB = m_lpMapiObjects->GetAddrBook(false); // do not release

	CEditor MyData(
		this,
		IDS_NOTIFICATIONS,
		IDS_NOTIFICATIONSPROMPT,
		3,
		CEDITOR_BUTTON_OK | CEDITOR_BUTTON_CANCEL);
	MyData.SetPromptPostFix(AllFlagsToString(flagNotifEventType, true));
	MyData.InitPane(0, CreateSingleLinePane(IDS_EID, NULL, false));
	MyData.InitPane(1, CreateSingleLinePane(IDS_ULEVENTMASK, NULL, false));
	MyData.SetHex(1, fnevNewMail);
	UINT uidDropDown[] = {
		IDS_DDMESSAGESTORE,
		IDS_DDSESSION,
		IDS_DDADDRESSBOOK
	};
	MyData.InitPane(2, CreateDropDownPane(IDS_OBJECTFORADVISE, _countof(uidDropDown), uidDropDown, true));

	WC_H(MyData.DisplayDialog());

	if (S_OK == hRes)
	{
		if ((0 == MyData.GetDropDown(2) && !lpMDB) ||
			(1 == MyData.GetDropDown(2) && !lpMAPISession) ||
			(2 == MyData.GetDropDown(2) && !lpAB))
		{
			ErrDialog(__FILE__, __LINE__, IDS_EDADVISE);
			return;
		}

		LPENTRYID	lpEntryID = NULL;
		size_t		cbBin = NULL;
		WC_H(MyData.GetEntryID(0, false, &cbBin, &lpEntryID));
		// don't actually care if the returning lpEntryID is NULL - Advise can work with that

		m_lpBaseAdviseSink = new CAdviseSink(m_hWnd, NULL);

		if (m_lpBaseAdviseSink)
		{
			switch (MyData.GetDropDown(2))
			{
			case 0:
				EC_MAPI(lpMDB->Advise(
					(ULONG)cbBin,
					lpEntryID,
					MyData.GetHex(1),
					(IMAPIAdviseSink *)m_lpBaseAdviseSink,
					&m_ulBaseAdviseConnection));
				m_lpBaseAdviseSink->SetAdviseTarget(lpMDB);
				m_ulBaseAdviseObjectType = MAPI_STORE;
				break;
			case 1:
				EC_MAPI(lpMAPISession->Advise(
					(ULONG)cbBin,
					lpEntryID,
					MyData.GetHex(1),
					(IMAPIAdviseSink *)m_lpBaseAdviseSink,
					&m_ulBaseAdviseConnection));
				m_ulBaseAdviseObjectType = MAPI_SESSION;
				break;
			case 2:
				EC_MAPI(lpAB->Advise(
					(ULONG)cbBin,
					lpEntryID,
					MyData.GetHex(1),
					(IMAPIAdviseSink *)m_lpBaseAdviseSink,
					&m_ulBaseAdviseConnection));
				m_lpBaseAdviseSink->SetAdviseTarget(lpAB);
				m_ulBaseAdviseObjectType = MAPI_ADDRBOOK;
				break;
			}

			if (SUCCEEDED(hRes))
			{
				if (0 == MyData.GetDropDown(2) && lpMDB)
				{
					// Try to trigger some RPC to get the notifications going
					LPSPropValue lpProp = NULL;
					WC_MAPI(HrGetOneProp(
						lpMDB,
						PR_TEST_LINE_SPEED,
						&lpProp));
					if (MAPI_E_NOT_FOUND == hRes)
					{
						// We're not on an Exchange server. We don't need to generate RPC after all.
						hRes = S_OK;
					}
					MAPIFreeBuffer(lpProp);
				}
			}
			else // if we failed to advise, then we don't need the advise sink object
			{
				if (m_lpBaseAdviseSink) m_lpBaseAdviseSink->Release();
				m_lpBaseAdviseSink = NULL;
				m_ulBaseAdviseObjectType = NULL;
				m_ulBaseAdviseConnection = NULL;
			}
		}
		delete[] lpEntryID;
	}
} // CBaseDialog::OnNotificationsOn
// Set default address list by name
STDMETHODIMP SetDefaultAddressList(IMAPISession &Session, const string &AddressList) {
   HRESULT hr = S_OK;

   // Initialize memory allocation
   LPVOID pAllocLink = NULL;
   MAPIAllocateBuffer(0, &pAllocLink);

   // Resolve address list name to ENTRYID
   ULONG cbEntryID;
   LPENTRYID lpEntryID;
   hr = ResolveAddressList(Session, AddressList, pAllocLink, &cbEntryID, &lpEntryID);
   if (FAILED(hr)) {
      cerr << "Unable to resolve address list name '" << AddressList << "'." << endl;
      return hr;
   }

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

   // Fix IID_CAPONE_PROF See note here: (http://msdn.microsoft.com/en-us/library/office/cc839797)
   // Turning off the PR_AB_CHOOSE_DIRECTORY_AUTOMATICALLY property
   // Set default address list

   cout << "Resetting choose automatically property" <<endl;

   LPPROFSECT lpProfileSection;
   hr = Session.OpenProfileSection((LPMAPIUID)&IID_CAPONE_PROF, NULL, MAPI_MODIFY  , &lpProfileSection);
   if (FAILED(hr)) {
	   cerr << "Error opening profile section" << endl;
	   goto Exit;
   }


   SPropValue lpPropValue;
   lpPropValue.ulPropTag = PR_AB_CHOOSE_DIRECTORY_AUTOMATICALLY;
   lpPropValue.Value.l = 0;

   hr = HrSetOneProp(lpProfileSection, &lpPropValue);
   if (FAILED(hr)) {
	   cerr << "Error setting property for automatic choosing to off with error" << hr << endl;
	   goto Exit;
   }

   //End modified by CL

   // Display feedback
   TraceDefaultDir(*lpAddrBook);
   cout << "Setting default address list: " << AddressList << endl;

   // Set default address list
   hr = lpAddrBook->SetDefaultDir(cbEntryID, lpEntryID);
   if (FAILED(hr)) {
      cerr << "Error setting default address list" << endl;
      goto Exit;
   }


   // Start modified by CL

   //TraceDefaultDir(*lpAddrBook);

   // End modified by CL

Exit:
   if (pAllocLink) MAPIFreeBuffer(pAllocLink);
   return hr;
}