none
Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Runtime.InteropServices.ComTypes.IConnectionPointContainer' RRS feed

  • Question

  • Hello,

    According to Cor Ligthert advice I am creating new question regarding my VSTO addin Outlook problem.

    I'm creating add-in (Outlook 2010, Win 10 Prof) which will be check outlook item on selection change, and in case of Mailtem (and some other contintions) will add event to this mailitem to capture "Send" button click. Part of my code below:

            void explorer_SelectionChange()
            {
                try
                {
                    if (this.Application.ActiveExplorer().Selection.Count > 0)
                    {
                        Object selObject = this.Application.ActiveExplorer().Selection[1];
                        if (selObject is Outlook.MailItem)
                        {
                            Outlook.MailItem mailItem =
                                (selObject as Outlook.MailItem).........................

    Everything works fine with no error, but when I select in my Outlook profile "Public Folders" and then I will try to open some outlook items then I have received an error:

    System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Runtime.InteropServices.ComTypes.IConnectionPointContainer'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B196B284-BAB4-101A-B69C-00AA00341D07}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)

    The same problem was describe in below question but there no answer on it:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/fb11f14b-4565-4e24-85cb-b53e97e85367/unable-to-cast-com-object-of-type-systemcomobject-to-interface-type?forum=vsto

    After setting up breakpoints in code it looks like during reading of the same item (only with Public Folder Outlook view) my addin cannot read some attributies for Mailitem, returning for many of them (not all - can read mailitem.Subject for example) error:

    A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in MyOutlookProject.DLL
    A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

    The problem is reproduced on more the one machine.

    Can you help what's going on?

    Thanks in advice

    iziz1


    Sunday, January 22, 2017 2:37 PM

Answers

  • Hi,

    Public folders permission are OK.

    I find out that problem is caused because add-in cannot read some Outlook.Mailiem attributes returning for them error:

    System.Runtime.InteropServices.COMException: Your Digital Id Name Cannot Be Found By The Underlying Security

    This error doesn't caused that that on try-catch any exception appears.

    So I manage to add a workaround/bypass and I check if Outlook.Mailitem is read correctly or not by function:

            public bool EM_check_mailitem(Outlook.MailItem mailItem)
            {
                try
                {
                    Outlook.PropertyAccessor pa = mailItem.PropertyAccessor;
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pa);
                    return true;
                }
                catch
                {
                    return false;
                }
            }

    If Outlook.Mailitem contains some attributes with "System.Runtime.InteropServices.COMException: Your Digital Id Name Cannot Be Found By The Underlying Security" function returns false and I don’t to add SendEventHandler for such Outlook.Mailitem.

    This function has solved my problem.

    Thanks

    iziz1


    • Marked as answer by iziz1 Tuesday, January 24, 2017 2:44 PM
    Tuesday, January 24, 2017 12:51 PM

All replies

  • Hello,

    First of all, I have noticed the following lines of code:

    (this.Application.ActiveExplorer().Selection.Count

    and

    this.Application.ActiveExplorer().Selection[1]

    Don't create a chain of property and method calls in the single line of code. In that case you will not be able to identify the source of the exception if any. Also COM objects will be left alive if don't release them in a timely manner. For example:

     Application.ActiveExplorer()

    The ActiveExplorer method of the Application class returns an instance of the Explorer class which should be released after.

    Explorer.Selection

    The Selection property of the Explorer  class returns an instance of the corresponding class which should be released after as well.

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it.  Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

    I'd suggest removing any extra code from your add-in and disabling any other COM add-ins in Outlook. Does it help? Is it an Exchange profile? Do you use the cached mode?

    Also you may try to use the late-binding technology for calling property and methods dynamically without casting objects to the actual types.  See Type.InvokeMember for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    Sunday, January 22, 2017 3:37 PM
  • He Eugene,

    Thank you for your answer and advices. I'm new to VS and make some basic mistakes.

    According to your suggestion I have created new add-in only to check this error.

    Please find whole code of new add-in ThisAddin.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Reflection;
    using System.Windows.Forms;

    namespace test_error
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.ActiveExplorer().SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(explorer_SelectionChange);
            }

            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }

            void explorer_SelectionChange()
            {
                try
                {
                    Outlook.Explorer currentExplorer = this.Application.ActiveExplorer();
                    if (currentExplorer.Selection.Count > 0)
                    {
                        Object selObject = currentExplorer.Selection[1];

                        if (selObject is Outlook.MailItem)
                        {
                            Outlook.MailItem mailItem =
                                (selObject as Outlook.MailItem);
                            ((Outlook.ItemEvents_10_Event)mailItem).Send += new Outlook.ItemEvents_10_SendEventHandler(mySend);
                        }
                            
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(selObject);
                        selObject = null;
                        currentExplorer = null;
                    }   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            public static void mySend(ref bool cancel)
            {
                //Do someting
            }

            #region VSTO generated code

            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }

    I'm not sure if I correct it according to your advices, if no please feel free to correct me.I have disabled other COM add-ins and still I got this error on line (only when I change seleciton on Public Folder view):

                            ((Outlook.ItemEvents_10_Event)mailItem).Send += new Outlook.ItemEvents_10_SendEventHandler(mySend);

    My Outlook is Exchange 2010 profile without cached mode.

    Sunday, January 22, 2017 4:36 PM
  • Hi,

    Sorry that we don’t have a public folder to check if I could reproduce the issue.

    According to your description, do you check the public folder permission of the mail account?


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 24, 2017 9:31 AM
    Moderator
  • Hi,

    Public folders permission are OK.

    I find out that problem is caused because add-in cannot read some Outlook.Mailiem attributes returning for them error:

    System.Runtime.InteropServices.COMException: Your Digital Id Name Cannot Be Found By The Underlying Security

    This error doesn't caused that that on try-catch any exception appears.

    So I manage to add a workaround/bypass and I check if Outlook.Mailitem is read correctly or not by function:

            public bool EM_check_mailitem(Outlook.MailItem mailItem)
            {
                try
                {
                    Outlook.PropertyAccessor pa = mailItem.PropertyAccessor;
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pa);
                    return true;
                }
                catch
                {
                    return false;
                }
            }

    If Outlook.Mailitem contains some attributes with "System.Runtime.InteropServices.COMException: Your Digital Id Name Cannot Be Found By The Underlying Security" function returns false and I don’t to add SendEventHandler for such Outlook.Mailitem.

    This function has solved my problem.

    Thanks

    iziz1


    • Marked as answer by iziz1 Tuesday, January 24, 2017 2:44 PM
    Tuesday, January 24, 2017 12:51 PM