//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;
}