BOOKS i'm reading |
Automate Microsoft Outlook from C++Contents
Accessing the Outlook COM interfaces from C++ (Part 2)So now, we are ready for the actual meat:
bool CCOMOutlookStubImpl::Init() { bool res = true; HRESULT hr; try { hr = m_OutlookAppPtr.CreateInstance( __uuidof(Outlook::Application) ); if(OLI_FAILED(hr)) { res = false; } } catch ( _com_error &ce ) { COMMERROR(ce.Error()); res = false; } return res; } bool CCOMOutlookStubImpl::Send(LPCTSTR Subject, LPCTSTR To, LPCTSTR Body, bool bDeleteAfterSubmit) { bool res = true; HRESULT hr; try { IDispatchPtr IMailItemDispatchPtr; IMailItemDispatchPtr = m_OutlookAppPtr->CreateItem(Outlook::olMailItem); // Query the MailItem interface Outlook::_MailItemPtr IMailItemPtr; Outlook::_MailItem *IMailItem = NULL; hr = IMailItemDispatchPtr.QueryInterface(__uuidof(Outlook::_MailItem),&IMailItem); if(OLI_FAILED(hr)) { return false; } IMailItemPtr.Attach(IMailItem); IMailItemPtr->Subject = Subject; IMailItemPtr->To = To; IMailItemPtr->Body = Body; // It is very important to use VARIANT_TRUE as VB represents TRUE as -1 // vs C++ where TRUE is 1. IMailItemPtr->DeleteAfterSubmit = (bDeleteAfterSubmit?VARIANT_TRUE:VARIANT_FALSE); hr = IMailItemPtr->Send(); if(OLI_FAILED(hr)) { res = false; } } catch ( _com_error &ce ) { if( HRESULT_CODE(ce.Error()) == 0x4004 ) { MessageBox(NULL,_T("You did not let this program to send the e-mail"), _T("COMOutlookStub"),MB_OK|MB_ICONINFORMATION); } else { // Unexpected error. COMMERROR(ce.Error()); DiagCOM diag; diag.display( ce, __FILE__, __LINE__ ); } res = false; } return res; } First, the compiler COM support is a blessing by making COM development with C++ almost as easy as in VB. It encapsulates each COM interface pointer in a smart pointer,
IMailItemPtr->Subject = Subject; IMailItemPtr->To = To; IMailItemPtr->Body = Body; // It is very important to use VARIANT_TRUE as VB represents TRUE as -1 // vs C++ where TRUE is 1. IMailItemPtr->DeleteAfterSubmit = (bDeleteAfterSubmit?VARIANT_TRUE:VARIANT_FALSE); The only exception is when a property has the type
I think that the only remaining COM intricacy is: IDispatchPtr IMailItemDispatchPtr; IMailItemDispatchPtr = m_OutlookAppPtr->CreateItem(Outlook::olMailItem); // Query the MailItem interface Outlook::_MailItemPtr IMailItemPtr; Outlook::_MailItem *IMailItem = NULL; hr = IMailItemDispatchPtr.QueryInterface(__uuidof(Outlook::_MailItem),&IMailItem);
|