none
how to get _MailItem interface from IDispatch interface in outlook2003? RRS feed

  • Question

  •      I am developing a add-in for Outlook2003. The following code is fragment.

    HRESULT hr = S_OK;
    CComPtr <IDispatch> spDisp;
    CComQIPtr <Outlook::_MailItem> m_spMailItem;
    CComPtr <Outlook::Selection> spSelect;	
    hr = spExplorer->get_Selection(&spSelect);
    
    		if (spSelect == NULL)
    		{
    			delete[] name;
    			name = NULL;
    			return;
    		}
    
    		hr = spSelect->get_Count(&count);
    
    		VARIANT index;
    		index.vt=VT_I4;
    
    		for (long i = 1;i<=count;i++)
    		{
    			index.lVal=i;
    			BSTR Subject_bstr;
    			HRESULT hr = spSelect->Item(index,&spDisp);	
    			spDisp->AddRef();
    			char* subject_char = NULL;	
    
    			LPTYPEINFO lpMyTypeInfo = NULL;
    			BSTR szName;
    			spDisp->GetTypeInfo(0, 0, &lpMyTypeInfo);
    			if (lpMyTypeInfo != NULL)
    				lpMyTypeInfo->GetDocumentation(MEMBERID_NIL, &szName, 0,	0, 0);
    
    			if (0 == strcmp("_MailItem",(char *) (_bstr_t)szName))
    				m_spMailItem = spDisp;
    
    
    			spDisp->Release();
    			spDisp = NULL;

    However, I found szName is "MailItem" when I select a Email in inbox folder and I think this result should be "_MailItem",because I can get _MailItem interface in outlook 2007 or outlook 2010 by using the same code. If m_spMailItem point to MailItem I only can use Addref and Release method. Can you tell me a reason or help me to correct this code? 

    Thanks!

    Tuesday, December 17, 2013 4:28 AM

Answers

  • I think I found the key point of this problem, when I select a encrypted mail, the QueryInterface method will return E_NOINTERFACE by query IID__MailItem interface, and when I select a unencrypted mail,this method return S_OK. So I think Microsoft make access control to read an encrypted mail in outlook2003.
    Wednesday, December 18, 2013 5:32 AM

All replies

  • Hello,

    The IDispatch interface inherits from the IUnknown interface. The latter provides the QueryInterface method. It looks like you need to use the QueryInterface method for casting an object to the MailItem interface. Is it what you are looking for?

    Note, the IDispatch interface provides the Invoke method which can be used for the late binding (you can call properties and methods without casting).

    Tuesday, December 17, 2013 11:20 AM
  • Actually I want to know whythe type returned by GetTypeInfo function is "MailItem",I think this result should be "_Mailitem" ,so I compare szName with a "_MailItem" string. For example, when I create a new mail message and press "send" button, I can get a pointer point to IDispatch interface , I use GetypeInfo function and I get this interface type is "_MailItem" and I can convert IDispatch interface to _MailItem interface. This is what I am looking for.

    Note,I don't want to use IDispatch interace and I perfer to use a defined COM interface.

    Tuesday, December 17, 2013 12:38 PM
  • I think I found the key point of this problem, when I select a encrypted mail, the QueryInterface method will return E_NOINTERFACE by query IID__MailItem interface, and when I select a unencrypted mail,this method return S_OK. So I think Microsoft make access control to read an encrypted mail in outlook2003.
    Wednesday, December 18, 2013 5:32 AM
  • Thank you for sharing the cause for other forum readers. May be somebody will find it helpful. 
    Wednesday, December 18, 2013 11:55 AM