E_NOINTERFACE error for saving message as MSG file

May 27, 2008
Reaction score
i am programming Extended MAPI in C++. and trying to save message items as msg files.
But i am getting error "E_NOINTERFACE" each time i try to execute CopyTo method of IMessage.
any idea about the solution here is my source code ..

#include <initguid.h> //this is needed,
#define USES_IID_IMessage
#include <mapiguid.h> //then this
#include <mapix.h>
#include <mapiutil.h>
#include <mspst.h>


[font=&quot]HRESULT hRes = S_OK;[/font]

[font=&quot] LPSPropValue pSubject = NULL;[/font]

[font=&quot] LPSTORAGE pStorage = NULL;[/font]

[font=&quot] LPMSGSESS pMsgSession = NULL;[/font]

[font=&quot] LPMESSAGE pIMsg = NULL;[/font]

[font=&quot] SizedSPropTagArray ( 7, excludeTags );[/font]

[font=&quot] char szPath[_MAX_PATH] = {0};[/font]

[font=&quot] strcpy(szPath, m_outPath.GetBuffer(10));[/font]

[font=&quot] char strAttachmentFile[_MAX_PATH];[/font]

[font=&quot] LPWSTR lpWideCharStr = NULL;[/font]

[font=&quot] ULONG cbStrSize = 0L;[/font]

[font=&quot] [/font]

[font=&quot] // create the file name in the directory where "TMP" is defined[/font]

[font=&quot] // with subject as the filename and ".msg" extension.[/font]

[font=&quot] [/font]

[font=&quot] // get temp file directory[/font]

[font=&quot] //GetTempPath(_MAX_PATH, szPath);[/font]

[font=&quot] [/font]

[font=&quot] // get subject line of message to copy. This will be used as the[/font]

[font=&quot] // new file name.[/font]

[font=&quot] HrGetOneProp( pMessage, PR_SUBJECT, &pSubject );[/font]

[font=&quot] [/font]

[font=&quot] // fuse path, subject, and suffix into one string[/font]

[font=&quot] char name[MAX_PATH];[/font]

[font=&quot] memset(name, 0, MAX_PATH);[/font]

[font=&quot] [/font]

[font=&quot] itoa(count, name, 10);[/font]

[font=&quot] [/font]

[font=&quot] strcpy(strAttachmentFile, szPath);[/font]

[font=&quot] //strcat ( strAttachmentFile, "\\"); [/font]

[font=&quot] strcat ( strAttachmentFile, name); [/font]

[font=&quot] strcat ( strAttachmentFile, ".msg");[/font]

[font=&quot] [/font]

[font=&quot] // get memory allocation function[/font]

[font=&quot] LPMALLOC pMalloc = MAPIGetDefaultMalloc();[/font]

[font=&quot] [/font]

[font=&quot] // Convert new file name to WideChar[/font]

[font=&quot] cbStrSize = MultiByteToWideChar (CP_ACP,[/font]

[font=&quot] MB_PRECOMPOSED,[/font]

[font=&quot] strAttachmentFile,[/font]

[font=&quot] -1, lpWideCharStr, 0);[/font]

[font=&quot] [/font]

[font=&quot] MAPIAllocateBuffer ( cbStrSize * sizeof(WCHAR),[/font]

[font=&quot] (LPVOID *)&lpWideCharStr );[/font]

[font=&quot] [/font]

[font=&quot] MultiByteToWideChar (CP_ACP,[/font]

[font=&quot] MB_PRECOMPOSED,[/font]

[font=&quot] strAttachmentFile,[/font]

[font=&quot] -1, lpWideCharStr, cbStrSize );[/font]

[font=&quot] [/font]

[font=&quot] // create compound file[/font]

[font=&quot] hRes = ::StgCreateStorageEx(lpWideCharStr,[/font]

[font=&quot] STGM_READWRITE |[/font]

[font=&quot] STGM_TRANSACTED |[/font]

[font=&quot] STGM_CREATE, STGFMT_STORAGE, 0, NULL, NULL, IID_IStorage, (void**)&pStorage);[/font]

[font=&quot] [/font]

[font=&quot] // Open an IMessage session.[/font]

[font=&quot] hRes = ::OpenIMsgSession(pMalloc, 0, &pMsgSession); [/font]

[font=&quot] [/font]

[font=&quot] // Open an IMessage interface on an IStorage object[/font]

[font=&quot] hRes = ::OpenIMsgOnIStg(pMsgSession,[/font]

[font=&quot] MAPIAllocateBuffer,[/font]

[font=&quot] MAPIAllocateMore,[/font]

[font=&quot] MAPIFreeBuffer,[/font]

[font=&quot] pMalloc,[/font]

[font=&quot] NULL,[/font]

[font=&quot] pStorage,[/font]

[font=&quot] NULL, 0, 0, &pIMsg);[/font]

[font=&quot] [/font]

[font=&quot] // write the CLSID to the IStorage instance - pStorage. This will[/font]

[font=&quot] // only work with clients that support this compound document type[/font]

[font=&quot] // as the storage medium. If the client does not support[/font]

[font=&quot] // CLSID_MailMessage as the compound document, you will have to use[/font]

[font=&quot] // the CLSID that it does support.[/font]

[font=&quot] hRes = WriteClassStg(pStorage, CLSID_MailMessage );[/font]

[font=&quot] [/font]

[font=&quot] // Specify properties to exclude in the copy operation. These are[/font]

[font=&quot] // the properties that Exchange excludes to save bits and time.[/font]

[font=&quot] // Should not be necessary to exclude these, but speeds the process[/font]

[font=&quot] // when a lot of messages are being copied.[/font]

[font=&quot] excludeTags.cValues = 7;[/font]

[font=&quot] excludeTags.aulPropTag[0] = PR_ACCESS;[/font]

[font=&quot] excludeTags.aulPropTag[1] = PR_BODY;[/font]

[font=&quot] excludeTags.aulPropTag[2] = PR_RTF_SYNC_BODY_COUNT;[/font]

[font=&quot] excludeTags.aulPropTag[3] = PR_RTF_SYNC_BODY_CRC;[/font]

[font=&quot] excludeTags.aulPropTag[4] = PR_RTF_SYNC_BODY_TAG;[/font]

[font=&quot] excludeTags.aulPropTag[5] = PR_RTF_SYNC_PREFIX_COUNT;[/font]

[font=&quot] excludeTags.aulPropTag[6] = PR_RTF_SYNC_TRAILING_COUNT;[/font]

[font=&quot] [/font]

[font=&quot] // copy message properties to IMessage object opened on top of[/font]

[font=&quot] // IStorage.+[/font]

[font=&quot] [/font]

[font=&quot] //HRESULT hResult = CoCreateInstance (myCLSID, NULL, CLSCTX_INPROC_SERVER, myCLSID, (LPVOID*)&myMessage);[/font]

[font=&quot] [/font]

[font=&quot] hRes = pMessage->CopyTo(0, NULL,[/font]

[font=&quot] (LPSPropTagArray)&excludeTags,[/font]

[font=&quot] NULL, NULL,[/font]

[font=&quot] (LPIID)&IID_IMessage,[/font]

[font=&quot] pIMsg, 0, NULL );[/font]

[font=&quot] [/font]

[font=&quot] if (hRes == S_OK)[/font]

[font=&quot] {[/font]

[font=&quot] // save changes to IMessage object.[/font]

[font=&quot] if (pIMsg)[/font]

[font=&quot] hRes = pIMsg->SaveChanges( KEEP_OPEN_READWRITE );[/font]

[font=&quot] [/font]

[font=&quot] // save changes in storage of new doc file[/font]

[font=&quot] hRes = pStorage->Commit(STGC_DEFAULT);[/font]

[font=&quot] count++;[/font]

[font=&quot] }[/font]

[font=&quot] [/font]

[font=&quot] [/font]

[font=&quot] // free objects and clean up memory[/font]

[font=&quot] MAPIFreeBuffer ( lpWideCharStr );[/font]

[font=&quot] pStorage->Release();[/font]

[font=&quot] pIMsg->Release();[/font]

[font=&quot] CloseIMsgSession ( pMsgSession );[/font]

[font=&quot] [/font]

[font=&quot] pStorage = NULL;[/font]

[font=&quot] pIMsg = NULL;[/font]

[font=&quot] pMsgSession = NULL;[/font]

[font=&quot] lpWideCharStr = NULL;[/font]

[font=&quot] [/font]

[font=&quot] //count++;[/font]

[font=&quot] return hRes;[/font]


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question