Article ID: 252649 - Last Review: August 25, 2005 - Revision: 3.4

How To Open a MAPI IMessage Interface with CDO Entry ID

This article was previously published under Q252649
Expand all | Collapse all

SUMMARY

When you work with CDO using Microsoft Visual C++, it may sometimes be necessary to switch over to MAPI. The code in the "More Information" section shows how to take a message in CDO and open that same message in MAPI.

MORE INFORMATION

//This code assumes that CDO has been imported with the following statement
//#import "cdo.dll" no_namespace
//and that we have a MAPI session opened with the same profile as the CDO session.
HRESULT OpenMAPIMessageFromCDOMessage(
	LPMAPISESSION lpMAPISession,
	MessagePtr pCDOMessage,
	LPMESSAGE* lppMessage)
{
	HRESULT hRes = S_OK;
	int cbSEID;
	LPENTRYID pbSEID;
	int cbEID;
	LPENTRYID pbEID;

	//Find CDO's StoreID
	bstr_t szStoreEID = pCDOMessage->StoreID;

	//Convert the StoreID to a byte array for OpenMsgStore
	cbSEID = strlen(szStoreEID)/2;
	hRes = MAPIAllocateBuffer(cbSEID, (void **)&pbSEID);
	FBinFromHex(szStoreEID, (LPBYTE)pbSEID);

	hRes = lpMAPISession->OpenMsgStore(
		0,
		cbSEID,
		pbSEID,
		0,
		MAPI_BEST_ACCESS,
		&lpMDB);
	MAPIFreeBuffer(pbSEID);
	if (FAILED(hRes)) return hRes;

	_variant_t lClass = pMessage->Class;

	//Find the CDO message's Entry ID
	bstr_t szEID
	if ((LONG) lClass == 26)//that is, we have an appointment item
	{
		//CDO tacks on 80 extra bytes to appointment item IDs. Skip them.
		szEID = ((char *)(bstr_t)pMessage->ID)+80;
	}
	else 
	{
		szEID = pCDOMessage->ID;
	}

	//Convert the ID to a byte array for OpenMsgStore
	cbEID = strlen(szEID)/2;
	hRes = MAPIAllocateBuffer(cbEID, (void **)&pbEID);
	FBinFromHex(szEID, (LPBYTE) pbEID);

	hRes = lpMDB->OpenEntry(
		cbEID,
		pbEID,
		0,
		MAPI_BEST_ACCESS,
		&lpObjType,
		(LPUNKNOWN *) lppMessage);
	MAPIFreeBuffer(pbEID);
	if (FAILED(hRes)) return hRes;

	if (lpMDB) lpMDB->Release();
	return hRes;
}
				

APPLIES TO
  • Microsoft Office Outlook 2007
  • Microsoft Messaging Application Programming Interface
  • Microsoft Collaboration Data Objects 1.2
  • Microsoft Collaboration Data Objects 1.21
Keywords: 
kbhowto kbmsg KB252649
 

Article Translations

 

Related Support Centers