Can’t Open A Stream Twice

A customer asked me to document this, as they had run in to it while developing a COM Add-in for Outlook. They were trying to do some work with attachments, and this led them to listen to the Outlook Object Model’s BeforeAttachmentAdd event. Inside this event, they used MAPIOBJECT to get the attachment, and called OpenProperty to open a stream on the PR_ATTACH_DATA_OBJ property. This worked fine in cached mode, or against a PST, but when they ran their code in online mode, OpenProperty failed with MAPI_E_CALL_FAILED.

Digging in to the problem, we found that before firing the event, Outlook had itself already opened a stream on this property. Perhaps this was the reason for the failure? I did a quick test with MFCMAPI, modifying the CAttachmentsDlg::OpenItemProp function to call OpenProperty twice in a row. Sure enough, I saw the same thing they saw. Against a PST/OST, the second call succeeded, but against Exchange online mode it failed. Exchange won’t let you open two streams on this property at the same time. Not even if the second stream is read only.

Ultimately, this isn’t much of a surprise. These are two different store providers. One operates against a local database file, while the other operates across a network. The PST provider is in a better position to negotiate two different stream interfaces operating against the same backend data than the Exchange provider would be. So we can chalk this up as another implementation difference between the two providers.