Obtain a contact message given a contacts address book entry

Applies to: Outlook 2013 | Outlook 2016

This topic contains an example in C++, HrOpenContact, that shows how to use the CONTAB_ENTRYID structure that identifies an entry in a Contacts Address Book to obtain the associated MAPI Contact message.

HrOpenContact has the following parameters:

  • lpSession is an input parameter representing the current session. LPMAPISESSION is defined in the MAPI header file mapix.h as a pointer to IMAPISession : IUnknown.

  • cbEntryID is an input parameter representing the size of the entry identifier associated with lpEntryID.

  • lpEntryID is an input parameter representing a pointer to the entry identifier of an entry in a Contact Address Book.

  • ulFlags is an input parameter representing a bitmask containing object access flags to the MAPI Contact message.

  • lpContactMessage is an output parameter representing a pointer to the MAPI Contact message.

To open the underlying MAPI Contact message, HrOpenContact first casts lpEntryID to a pointer to CONTAB_ENTRYID. It then calls IMAPISession::OpenEntry to obtain the MAPI Contact message, passing as parameters the cbeid and abeid fields of the entry in the Contacts Address Book that identify respectively the size of the entry identifier and the entry identifier of the MAPI Contact message.

TZDEFINITION* BinToTZDEFINITION(ULONG cbDef, LPBYTE lpbDef) 
{ 
    if (!lpbDef) return NULL; 
 
    // Update this if parsing code is changed. 
    // This checks the size up to the flag member. 
    if (cbDef < 2*sizeof(BYTE) + 2*sizeof(WORD)) return NULL; 
 
    TZDEFINITION tzDef = {0}; 
    TZRULE* lpRules = NULL; 
    LPBYTE lpPtr = lpbDef; 
    WORD cchKeyName = NULL; 
    WCHAR* szKeyName = NULL; 
    WORD i = 0; 
 
    BYTE bMajorVersion = *((BYTE*)lpPtr); 
    lpPtr += sizeof(BYTE); 
    BYTE bMinorVersion = *((BYTE*)lpPtr); 
    lpPtr += sizeof(BYTE); 
 
    // We only understand TZ_BIN_VERSION_MAJOR 
    if (TZ_BIN_VERSION_MAJOR != bMajorVersion) return NULL; 
 
    // We only understand if >= TZ_BIN_VERSION_MINOR 
    if (TZ_BIN_VERSION_MINOR > bMinorVersion) return NULL; 
 
    lpPtr += sizeof(WORD); 
 
    tzDef.wFlags = *((WORD*)lpPtr); 
    lpPtr += sizeof(WORD); 
 
    if (TZDEFINITION_FLAG_VALID_GUID & tzDef.wFlags) 
    { 
        if (lpbDef + cbDef - lpPtr < sizeof(GUID)) return NULL; 
        tzDef.guidTZID = *((GUID*)lpPtr); 
        lpPtr += sizeof(GUID); 
    } 
 
    if (TZDEFINITION_FLAG_VALID_KEYNAME & tzDef.wFlags) 
    { 
        if (lpbDef + cbDef - lpPtr < sizeof(WORD)) return NULL; 
        cchKeyName = *((WORD*)lpPtr); 
        lpPtr += sizeof(WORD); 
        if (cchKeyName) 
        { 
            if (lpbDef + cbDef - lpPtr < (BYTE)sizeof(WORD)*cchKeyName) return NULL; 
            szKeyName = (WCHAR*)lpPtr; 
            lpPtr += cchKeyName*sizeof(WORD); 
        } 
    } 
 
    if (lpbDef+ cbDef - lpPtr < sizeof(WORD)) return NULL; 
    tzDef.cRules = *((WORD*)lpPtr); 
    lpPtr += sizeof(WORD); 
    if (tzDef.cRules) 
    { 
        lpRules = new TZRULE[tzDef.cRules]; 
        if (!lpRules) return NULL; 
 
        LPBYTE lpNextRule = lpPtr; 
        BOOL bRuleOK = false; 

See also