Message "The object could not be found"

G

Gopi

When I call the method IMAPISession::ShowForm(), I am getting the message
"The object could not be found"

My program, which calls IMAPISession::ShowForm() is working fine on a system
having Outlook 2000 SP-3(9.0.0.6627) on Windows 2000. But the same program
when run on a system having Outlook XP SP3 or Outlook 2003 is giving the
message "The object could not be found", whenever it encounters the call to
IMAPISession::ShowForm(). Pl see the source code below. I am referring to the
line SpSess->ShowForm(...)

-------------------------source code---------------
#include "afxwin.h"
#include <edkafx.h>
#include "safepnt.h"

#include <stdio.h>

void MyItemDoMail( int Item, SpIMAPISession & SpSess, BOOL MailToXYZ = FALSE
);

void Fn()
{
SpIMAPISession SpSess;

printf("before calling MAPIInitialize \n");
::MAPIInitialize( 0 );
printf("after calling MAPIInitialize \n");

HWND m_hWnd = NULL;
if( FAILED( ::MAPILogonEx( (LPARAM)m_hWnd, NULL, NULL, MAPI_LOGON_UI |
MAPI_NO_MAIL, &SpSess ) ))
{
printf("MAPILogonEx failed \n");
::AfxMessageBox( "okok", MB_OK );
return;
}

MyItemDoMail(1, SpSess);

printf("after calling MAPILogonEx \n");


SpSess.Release();
printf("after calling Release \n");

::MAPIUninitialize();
printf("after calling MAPIUninitialize \n");

}


int main()
{
printf("before calling my MAPI function \n");
Fn();
printf("after calling my MAPI function \n");
return 0;
}

HRESULT SetMessageProps(SpIMessage &spMsg)
{
; // Message properties tag array
enum {SUBJECT, CLASS, BODY, MSG_DEL, MSG_PROPS };
SPropValue lpPropValueArray2[MSG_PROPS];
lpPropValueArray2[SUBJECT].ulPropTag = PR_SUBJECT;
CString Subject = "subject";

lpPropValueArray2[SUBJECT].Value.LPSZ = (LPTSTR)(LPCTSTR)Subject;
lpPropValueArray2[CLASS].ulPropTag = PR_MESSAGE_CLASS;
lpPropValueArray2[CLASS].Value.LPSZ = _T("IPM.Note" );

CString BodyText = "BodyText";
lpPropValueArray2[BODY].ulPropTag = PR_BODY;
lpPropValueArray2[BODY].Value.LPSZ = (LPTSTR)(LPCTSTR)BodyText;

lpPropValueArray2[MSG_DEL].ulPropTag = PR_DELETE_AFTER_SUBMIT;
lpPropValueArray2[MSG_DEL].Value.b = TRUE;

return spMsg->SetProps(MSG_PROPS,lpPropValueArray2,NULL);
}

HRESULT ResolveName(LPCTSTR Name, LPADRLIST *lpAdrList, LPADRBOOK pAddrBook)
{
HWND m_hWnd = NULL;

HRESULT hRes = S_OK;
ULONG ulUIParam = 0;
ULONG cbEID = 0L;
LPBYTE lpEID = NULL;
LPADRLIST pAdrList = NULL;

// Allocate memory for new SRowSet structure.
hRes = MAPIAllocateBuffer(CbNewSRowSet(1),(LPVOID*) &pAdrList);

// If memory allocation fails, quit.
if ( FAILED ( hRes ) )
return hRes;
// Zero out allocated memory.
ZeroMemory ( pAdrList, CbNewSRowSet(1));
// Allocate memory for SPropValue structure that indicates what
// recipient properties will be set. To resolve a name that
// already exists in the Address book, this will always be 1.
hRes = MAPIAllocateBuffer( 1 * sizeof(SPropValue),
(LPVOID*) &(pAdrList->aEntries[0].rgPropVals));
// If memory allocation fails, quit.
if ( FAILED ( hRes ) )
{
MAPIFreeBuffer( pAdrList );
return hRes; // Zero out allocated memory.
}
ZeroMemory ( pAdrList -> aEntries[0].rgPropVals,
1 * sizeof(SPropValue) );
// Resolve name will take as many entries in the ADRLIST as you
// want to resolve, but each entry (ADRENTRY) can have only one
// property previously set by the client - this is usually
// PR_DISPLAY_NAME.
// How many recipients to resolve.
pAdrList->cEntries = 1;
// As far as I can tell this must always be 1L and will always
// equal the multiplier in the MAPIAllocateBuffer call above.
pAdrList->aEntries[0].cValues = 1L;
// Set the SPropValue members == the desired values. This should
// be PR_DISPLAY_NAME. You can substitute a display name with an
// alias to search for. You can search by alias and display names
// interchangeably in most address books.
pAdrList->aEntries[0].rgPropVals[0].ulPropTag = PR_DISPLAY_NAME;
pAdrList->aEntries[0].rgPropVals[0].Value.LPSZ = (LPTSTR)Name;


// pAdrList->aEntries[0].rgPropVals[1].ulPropTag = PR_ENTRYID;
// ResolveName is kind enough to redimension this array for us and
// give back a fully filled out ADRLIST and ADRENTRY structures.
hRes = pAddrBook -> ResolveName ( (ULONG) m_hWnd,
MAPI_DIALOG , NULL, pAdrList );
if ( SUCCEEDED ( hRes ) )
{
*lpAdrList = pAdrList;
}
return hRes;
}

HRESULT CreateAttachment(SpIMessage &spMsg, LPSTREAM pStrm,
LPCTSTR Attachment )
{

SpIAttach pAtt;
ULONG ulAttNum;
HRESULT hr = S_OK;
if (SUCCEEDED(hr = spMsg->CreateAttach( NULL, (ULONG)0, &ulAttNum, &pAtt)))
{
enum {METHOD, RENDERING, FILENAME, NUM_ATT_PROPS};
SPropValue spvAttach[NUM_ATT_PROPS];
spvAttach[METHOD].ulPropTag = PR_ATTACH_METHOD;
spvAttach[METHOD].Value.l = ATTACH_BY_VALUE ;
spvAttach[RENDERING].ulPropTag = PR_RENDERING_POSITION;
spvAttach[RENDERING].Value.l = -1;
spvAttach[FILENAME].ulPropTag = PR_ATTACH_FILENAME;
spvAttach[FILENAME].Value.LPSZ = (LPTSTR)Attachment;
// Save the properties we have set on the attachment
if (SUCCEEDED(hr = pAtt ->SetProps( NUM_ATT_PROPS,
(LPSPropValue)&spvAttach, NULL)))
{
SpIStream pStream;

LARGE_INTEGER Zero = { 0 };

ULARGE_INTEGER Max;
Max.QuadPart = MAXLONGLONG;

ULARGE_INTEGER Result, Read, Written;


pStrm->Seek( Zero, SEEK_SET, & Result );

if( SUCCEEDED( hr = pAtt->OpenProperty( PR_ATTACH_DATA_BIN,
(LPIID) &IID_IStream,
0, MAPI_CREATE | MAPI_MODIFY, (LPUNKNOWN *)&pStream) ) )
{
if( SUCCEEDED( pStrm->CopyTo( pStream, Max, &Read, &Written ) ))
{
ASSERT( Read.QuadPart == Written.QuadPart );
}
pStream.Release();
}
hr = pAtt->SaveChanges(0);
}
}
return hr;
}


void MyItemDoMail(int Item, SpIMAPISession & SpSess, BOOL MailToXYZ /* =
FALSE */ )
{
SpIMAPIFolder SpFld;
SpIMsgStore SpMdb;
CString logString;

HWND m_hWnd = NULL;


ULONG cbInEntryID = 0;
LPENTRYID lpInEntryID = NULL;
ULONG ulObjType;
HRESULT hr = S_OK;

SpIMessage spMyMsg;
hr = HrMAPIFindDefaultMsgStore( SpSess, &cbInEntryID, &lpInEntryID);

logString.Format( _T("Creating COnDmdRptMsg (HrMAPIFindDefaultMsgStore) hr
= %x"), hr);
// MTRACE(logString);

if(SUCCEEDED(hr))
{
hr = SpSess->OpenMsgStore(0L, cbInEntryID, lpInEntryID, NULL,
MDB_WRITE | MAPI_DEFERRED_ERRORS, &SpMdb );

MAPIFreeBuffer(lpInEntryID);
logString.Format( _T("Creating COnDmdRptMsg (HrMAPIFindDefaultMsgStore) hr
= %x" ), hr);
// MTRACE(logString);

if(SUCCEEDED(hr))
{
cbInEntryID = 0;
lpInEntryID = NULL;

hr = HrMAPIFindOutbox( SpMdb, &cbInEntryID, &lpInEntryID);
logString.Format(_T("Creating COnDmdRptMsg (HrMAPIFindOutbox) hr = %x"),
hr);
// MTRACE(logString);

if(SUCCEEDED(hr))
{
hr = SpSess->OpenEntry( cbInEntryID,
lpInEntryID,
NULL,
MAPI_MODIFY,
&ulObjType,
(LPUNKNOWN FAR *)&SpFld);

MAPIFreeBuffer(lpInEntryID);

logString.Format( _T("Creating COnDmdRptMsg (OpenEntry) hr = %x" ), hr);
// MTRACE(logString);

if(SUCCEEDED(hr))
{
if( SUCCEEDED( hr = SpFld->CreateMessage(NULL, 0, &spMyMsg) ))
{
CString Destination = "(e-mail address removed)";

SetMessageProps( spMyMsg );
if( !Destination.IsEmpty() )
{
LPADRLIST pal = NULL;
SpIAddrBook spAddrBook;

hr = SpSess->OpenAddressBook((ULONG)m_hWnd, NULL,
AB_NO_DIALOG, &spAddrBook );
if( SUCCEEDED( hr ) )
{
if( SUCCEEDED( ResolveName( Destination, &pal, spAddrBook ) ))
{

// We now have a resolved name. we need to add the MAPI_TO property
to it.
int i;
int NumProps = pal->aEntries[0].cValues + 1;
LPADRLIST pAdrList = NULL;
// Allocate memory for new SRowSet structure.
hr = MAPIAllocateBuffer(CbNewSRowSet(1),(LPVOID*) &pAdrList);

// If memory allocation fails, quit.
if ( FAILED ( hr) )
{
FreePadrlist( pal );
return;
}
// Zero out allocated memory.
ZeroMemory ( pAdrList, CbNewSRowSet(1));
hr = MAPIAllocateBuffer( NumProps * sizeof(SPropValue),
(LPVOID*) &(pAdrList->aEntries[0].rgPropVals));
ZeroMemory ( pAdrList-> aEntries[0].rgPropVals,
NumProps * sizeof(SPropValue) );

pAdrList->cEntries = 1;
pAdrList->aEntries[0].cValues = NumProps;

for( i = 0; i < NumProps -1; i++ )
{
pAdrList->aEntries[0].rgPropVals =
pal->aEntries[0].rgPropVals;
}
pAdrList->aEntries[0].rgPropVals[ NumProps -1].ulPropTag =
PR_RECIPIENT_TYPE;
pAdrList->aEntries[0].rgPropVals[ NumProps -1].Value.ul = MAPI_TO;

spMyMsg->ModifyRecipients( MODRECIP_ADD , pAdrList );

// Remove memory references.
for( i = 0; i < NumProps -1; i++ )
{

pAdrList->aEntries[0].rgPropVals.ulPropTag = PROP_TAG(
PT_ERROR, 0 );
pAdrList->aEntries[0].rgPropVals.Value.ul = 0;
}
FreePadrlist( pAdrList );


FreePadrlist( pal );
}
}
}
LPSTREAM pStrm;

if( SUCCEEDED( ::CreateStreamOnHGlobal( NULL, TRUE, &pStrm ) ) )
{
if( SUCCEEDED( hr = CreateAttachment( spMyMsg, pStrm, "filename.txt"
) ))
{
ULONG Token;
enum { MSG_STATUS, MSG_FLAGS, MSG_ACCESS, MSG_CLASS, NUM_PROPS };
const SizedSPropTagArray( NUM_PROPS, taProp ) = {NUM_PROPS,
PR_MSG_STATUS,
PR_MESSAGE_FLAGS,
PR_ACCESS,
PR_MESSAGE_CLASS };

SpSPropValue SpVal;
ULONG ulCnt = 0;
hr = spMyMsg->SaveChanges( FORCE_SAVE | KEEP_OPEN_READWRITE );

hr = spMyMsg->GetProps((LPSPropTagArray)&taProp, 0,
&ulCnt, &SpVal);

if( hr == S_OK )
{
printf("before PrepareForm \n");
::AfxMessageBox( "before PrepareForm", MB_OK );
SpSess->PrepareForm( NULL, spMyMsg, &Token );
printf("after PrepareForm \n");
::AfxMessageBox( "after PrepareForm", MB_OK );

spMyMsg.Release();
printf("after release2 \n");
::AfxMessageBox( "after release2", MB_OK );

HRESULT hr2 = S_OK;
hr2 = SpSess->ShowForm( (LPARAM)m_hWnd, SpMdb, SpFld, NULL,
Token, NULL, MAPI_NEW_MESSAGE,
SpVal[ MSG_STATUS].Value.ul,
SpVal[ MSG_FLAGS].Value.ul,
SpVal[ MSG_ACCESS].Value.ul,
SpVal[ MSG_CLASS].Value.LPSZ );


DWORD dw = GetLastError();
char str2[180];
sprintf(str2,"error. %d \n",dw);
::AfxMessageBox( str2, MB_OK );

if(hr2 != S_OK)
{
char str[180];
sprintf(str,"got screwed. %x \n",hr2);
::AfxMessageBox( str, MB_OK );
}
else
{
char str[180];
sprintf(str,"fine. %x \n",hr2);
::AfxMessageBox( str, MB_OK );
}

printf("after ShowForm \n");
::AfxMessageBox( "after ShowForm", MB_OK );
}
}
}
}

}
}
}
}


}
 

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

Top