Monthly Archives: March 2011

Using GDI+ in C++

#undef WIN32_LEAN_AND_MEAN // Need some rarely-used stuff from Windows headers

#include <Winsock2.h> // Must precede windows.h if used at all.
#include <windows.h> // Needed for gdiplus.h
#include <gdiplus.h>
// Following are more carefully designed and vetted headers:
#include <windowsx.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <stdio.h>
#include <string>
#include <time.h>
#include <iostream> 

Tech-Archive thread.

Another way:

#include <algorithm>
using std::min;
using std::max;
#include <windows.h>
#include <gdiplus.h>

MAPI IMAPIProp SaveChanges call

My application creates a contact, sets properties and saves changes, after that it could not get the last modification time (MAPI_E_NO_ACCESS).
It seems that IMAPIProp::SaveChanges() member function should not be called with FORCE_SAVE flag (for the compatibility reasons with Outlook version less than 2003) if there is further property reading.
UseKEEP_OPEN_READWRITE instead.

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.