Tag Archives: com

Interesting consequence of MAPI threading rules violation

Have faced with an interesting consequence of MAPI threading rules violation.
Consider the following code:

CComPtr<IStream> prop_stream;
hr = props->OpenProperty(prop_tag, &IID_IStream, STGM_READWRITE, MAPI_CREATE | MAPI_MODIFY, (IUnknown**)&prop_stream);
if (FAILED(hr)) {
    return hr;
}

ULONG total_bytes_written = 0;
ULONG bytes_to_write = 0;
ULONG bytes_written = 0;
while (total_bytes_written < buffer.size()) {
    bytes_to_write = min(buffer.size() - total_bytes_written, BLOCK_SIZE);
    hr = prop_stream->Write(&buffer[total_bytes_written], bytes_to_write, &bytes_written);
    if (FAILED(hr)) {
        break;
    }
    total_bytes_written += bytes_written;
}

if (FAILED(hr)) {
    return hr;
}

hr = prop_stream->Commit(STGC_DEFAULT);
if (FAILED(hr)) {
    return hr;
}

The problem: if Outlook version is less 2003, Commit() member function call returns E_OUTOFMEMORY.
The root cause: forgot to initialize/un-initialize MAPI for the worker thread.

See more details: MAPI Multithreading Rules @ SGriffin’s MAPI Internals.