PDA

View Full Version : How to create .msg file using QT application?



kishoreksnair
16th December 2011, 05:01
I am new in QT. I want create a .msg file programmatically in QT.I want to create a Qt gui application with MS Outlook integration.After searching from various web resources,I got a bit of source code but its not working properly.Its contain some errors and conversion problems.



#include "mainwindow.h"
#include<QDebug>
#include<QFile>
#include <MAPIX.h>
#include <MAPITags.h>
#include <MAPIDefS.h>
#include <MAPIUtil.h>
#include <MAPIGuid.h>
#include <IMessage.h>
#include <ObjBase.h>
#include<QDir>


#define INITGUID
#define USES_IID_IMessage

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
LPMESSAGE asdfasd = NULL;
qDebug()<<"Sample msg File Open";
SaveToMSG(asdfasd);

}

MainWindow::~MainWindow()
{

}
HRESULT MainWindow::SaveToMSG(LPMESSAGE pMessage)
{
HRESULT hRes = S_OK;
LPSPropValue pSubject = NULL;
LPSTORAGE pStorage = NULL;
LPMSGSESS pMsgSession = NULL;
LPMESSAGE pIMsg = NULL;
SizedSPropTagArray ( 7, excludeTags );
char szPath[_MAX_PATH];
char strAttachmentFile[_MAX_PATH];
LPWSTR lpWideCharStr = NULL;
ULONG cbStrSize = 0L;

// create the file name in the directory where "TMP" is defined
// with subject as the filename and ".msg" extension.

// get temp file directory
GetTempPath(_MAX_PATH, szPath);

// get subject line of message to copy. This will be used as the
// new file name.
HrGetOneProp( pMessage, PR_SUBJECT, &pSubject );

// fuse path, subject, and suffix into one string
strcpy ( strAttachmentFile, szPath );
strcat ( strAttachmentFile, pSubject->Value.lpszA );
strcat ( strAttachmentFile, ".msg");

// get memory allocation function
LPMALLOC pMalloc = MAPIGetDefaultMalloc();

// Convert new file name to WideChar
cbStrSize = MultiByteToWideChar (CP_ACP,
MB_PRECOMPOSED,
strAttachmentFile,
-1, lpWideCharStr, 0);

MAPIAllocateBuffer ( cbStrSize * sizeof(WCHAR),
(LPVOID *)&lpWideCharStr );

MultiByteToWideChar (CP_ACP,
MB_PRECOMPOSED,
strAttachmentFile,
-1, lpWideCharStr, cbStrSize );

// create compound file
hRes = ::StgCreateDocfile(lpWideCharStr,
STGM_READWRITE |
STGM_TRANSACTED |
STGM_CREATE, 0, &pStorage);

// Open an IMessage session.
hRes = ::OpenIMsgSession(pMalloc, 0, &pMsgSession);

// Open an IMessage interface on an IStorage object
hRes = ::OpenIMsgOnIStg(pMsgSession,
MAPIAllocateBuffer,
MAPIAllocateMore,
MAPIFreeBuffer,
pMalloc,
NULL,
pStorage,
NULL, 0, 0, &pIMsg);

// write the CLSID to the IStorage instance - pStorage. This will
// only work with clients that support this compound document type
// as the storage medium. If the client does not support
// CLSID_MailMessage as the compound document, you will have to use
// the CLSID that it does support.
hRes = WriteClassStg(pStorage, CLSID_MailMessage );

// Specify properties to exclude in the copy operation. These are
// the properties that Exchange excludes to save bits and time.
// Should not be necessary to exclude these, but speeds the process
// when a lot of messages are being copied.
excludeTags.cValues = 7;
excludeTags.aulPropTag[0] = PR_ACCESS;
excludeTags.aulPropTag[1] = PR_BODY;
excludeTags.aulPropTag[2] = PR_RTF_SYNC_BODY_COUNT;
excludeTags.aulPropTag[3] = PR_RTF_SYNC_BODY_CRC;
excludeTags.aulPropTag[4] = PR_RTF_SYNC_BODY_TAG;
excludeTags.aulPropTag[5] = PR_RTF_SYNC_PREFIX_COUNT;
excludeTags.aulPropTag[6] = PR_RTF_SYNC_TRAILING_COUNT;

// copy message properties to IMessage object opened on top of
// IStorage.
hRes = pMessage->CopyTo(0, NULL,
(LPSPropTagArray)&excludeTags,
NULL, NULL,
(LPIID)&IID_IMessage,
pIMsg, 0, NULL );

// save changes to IMessage object.
pIMsg -> SaveChanges ( KEEP_OPEN_READWRITE );

// save changes in storage of new doc file
hRes = pStorage -> Commit(STGC_DEFAULT);

// free objects and clean up memory
MAPIFreeBuffer ( lpWideCharStr );
pStorage->Release();
pIMsg->Release();
CloseIMsgSession ( pMsgSession );

pStorage = NULL;
pIMsg = NULL;
pMsgSession = NULL;
lpWideCharStr = NULL;

return hRes;
}



so please help me to rectify these problems.

nish
16th December 2011, 19:41
what errors are you getting. post the error messages.

kishoreksnair
16th December 2011, 19:52
following errors:

D:\ACROWIT_Project\Samplefopen\mainwindow.cpp:47: error: C2664: 'GetTempPathW' : cannot convert parameter 2 from 'char [260]' to 'LPWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

D:\ACROWIT_Project\Samplefopen\mainwindow.cpp:47: error: C2664: 'GetTempPathW' : cannot convert parameter 2 from 'char [260]' to 'LPWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

can you please send the sample code,or how can i run the above code...i want to create a msg file(outlook file format with:to,sub,body,attachment,bcc etc).

nish
16th December 2011, 19:54
the error messages are self explanatory. use WCHAR instead of char

kishoreksnair
16th December 2011, 20:08
thanks for your help...I am new in QT...and also have some problems in strcpy() and strcat()..

D:\ACROWIT_Project\Samplefopen\mainwindow.cpp:65: error: C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'WCHAR [260]' to 'LPCSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


D:\ACROWIT_Project\Samplefopen\mainwindow.cpp:73: error: C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'WCHAR [260]' to 'LPCSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

D:\ACROWIT_Project\Samplefopen\mainwindow.cpp:54: error: C2664: 'strcpy' : cannot convert parameter 1 from 'WCHAR [260]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


D:\ACROWIT_Project\Samplefopen\mainwindow.cpp:54: error: C2664: 'strcpy' : cannot convert parameter 1 from 'WCHAR [260]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

marcvanriet
16th December 2011, 23:28
See this link : http://www.codeproject.com/Tips/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc

The following links may also be usefull : http://www.google.com or http://www.yahoo.com or http://www.bing.com

You could check these out first before asking more questions...