views:

202

answers:

2

Hi, everyone!

I am developing a program, that handles incoming e-mail and sms through windows-mobile MAPI. The code basically looks like that:

   ulBodyProp = PR_BODY_A;
   hr = piMessage->OpenProperty(ulBodyProp, NULL, STGM_READ, 0, (LPUNKNOWN*)&piStream);

   if (hr == S_OK)
   {
      // ... get body size in bytes ...
      STATSTG statstg;
      piStream->Stat(&statstg, 0);
      ULONG cbBody = statstg.cbSize.LowPart;

      // ... allocate memory for the buffer ...
      BYTE* pszBodyInBytes = NULL;
      boost::scoped_array<BYTE> szBodyInBytesPtr(pszBodyInBytes = new BYTE[cbBody+2]);

      // ... read body into the pszBodyInBytes ...      
   }

That works and I have a message body. The problem is that this body is multibyte encoded and I need to return a Unicode string. I guess, I have to use ::MultiByteToWideChar() function, but how can I guess, what codepage should I apply? Using CP_UTF8 is naive, because it can simply be not in UTF8. Using CP_ACP works, well, sometimes, but sometimes does not. So, my question is: how can I retrieve the information about message codepage. Does MAPI provide any functions for it? Or is there a way to decode multibyte string, other than MultiByteToWideChar()?

Thanks!

A: 

Don't know about mobile, but is PR_BODY_W available? Do you also have the PR_RTF_COMPRESSED property? it contains the code page in the RTF header.

Dmitry Streblechenko (MVP) http://www.dimastr.com/ OutlookSpy - Outlook, CDO and MAPI Developer Tool

PR_BODY_W gives me directly the Unicode, and when it is available, I have no problem. I have a problem, when I have to use PR_BODY_A. As for PR_RTF_COMPRESSED, I'll investigate it, thanks!
SadSido
A: 

Take a look at this article by Stephen Griffin about reading an RTF Stream. It explains how you can go from PR_RTF_COMPRESSED directly to UNICODE text using HrTextFromCompressedRTFStreamEx. It works "as advertised" by Stephen and does indeed have some issues when changing code page.

In my experience this type of issue tends to manifest in regions where the local text is routinely interspersed with English text. We have seen this in the Asia Pacific region.

Paul Arnold