none
InvalidCastException When Passing Object From Office COM to Managed Assembly RRS feed

  • Question

  • I am having a failure with my project and the COM Shim as detailed here...

    http://blogs.msdn.com/b/mshneer/archive/2010/03/19/com-shim-wizards-for-vs-2010.aspx

    My project executes ok when the Shim is not installed (and the COM Shared Addin executes in the mscoree.dll app domain.  but this is clearly unacceptable.)  as soon as i wrap the addin with the Shim i get a casting exception when i try and pass a managed object created in VBA back into my managed assembly.

    in order to test that this wasn't a failure with my code i tested the same principle using andrew whitechapels example here, and i produced the same exception....

    http://blogs.msdn.com/b/andreww/archive/2009/03/01/exposing-events-from-non-vsto-add-in-automation-objects.aspx

    what i have done is to add a MyWidget class to andrew's example which i then create an instance of in VBA and pass it back an instance of the AddInUtils class.  It is at this point that the exception occurs.

    MyWidget class is as follows...

    namespace SharedAddInEvents
    {
        [Guid("2EA7E58B-7C2A-4B8E-83B6-E7279E02179B")]
        [InterfaceType(ComInterfaceType.InterfaceIsDual)]
        [ComVisible(true)]
        public interface IMyWidget
        {
            IAddInUtilities AiUtils { get; set; }
            void MyMethod(IAddInUtilities aiUtils);
            string Name { get; set; }
        }

        [Guid("ACDCBC50-FC85-444A-B183-874A5F0F8D4C")]
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        public class MyWidget : StandardOleMarshalObject, IMyWidget
        {
            public MyWidget()
            {
            }

            private string m_name;
            public string Name
            {
                get { return m_name; }
                set { m_name = value; }
            }

            private IAddInUtilities m_aiUtils;
            public IAddInUtilities AiUtils
            {
                get { return m_aiUtils; }
                set { m_aiUtils = value; }
            }

            public void MyMethod(IAddInUtilities aiUtils)
            {
                try
                {
                    m_aiUtils = (AddInUtilities)aiUtils;
                    System.Windows.Forms.MessageBox.Show("Name: " + this.Name);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
            }
        }
    }

    and i'm extending the VBA connection code to this...

        Dim addin As Office.COMAddIn
        Set addin = Application.COMAddIns("SharedAddInEvents.Connect")
        Set addInUtils = addin.Object
        addInUtils.CreateCustomTaskPane
        
        Set myWidg = New SharedAddInEvents.MyWidget
        myWidg.Name = "FooBar Widget"
        myWidg.MyMethod addInUtils

    where a System.InvalidCastExceptionn is raised at the line myWidg.MyMethod addInUtils

    any pointers as to what i'm doing wrong, or how i can fix this?

    Monday, March 25, 2013 4:43 PM

Answers

  • ok, no probs.

    i managed to find a workaround, which was suitable for use within the application design.  

    Instead of allowing the widget class to be created directly in VBA, i provided a creator method within the managed addInUtils class.  This passed an instance of the widget back to VBA, which could then be used without raising any invalid cast exceptions.

    It works well and is a clean solution, without requiring any major code changes.

    • Marked as answer by Matt Cope Tuesday, May 14, 2013 9:46 AM
    Tuesday, May 14, 2013 9:46 AM

All replies

  • Hi Matt,

    Thank you for posting in the MSDN Forum.

    I'll consult my colleague on your issue. You'll be informed if there's any update.

    Thank you for your patience and understanding.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 27, 2013 11:12 AM
    Moderator
  • Hello Matt,

    Could you please share the sample that you used to test this issue? I can have a look into this and check what is the cause.

    Thanks,

    Sreerenj G Nair

    Thursday, April 4, 2013 1:39 AM
  • hi

    not sure what you require here.

    do you want me to send you the full SharedAddinEvents solution and my test Word file?  If so, please let me know how i can send this to you

    thanks, matt

    Monday, April 8, 2013 3:14 PM
  • Hello Matt,

    You mentioned that

    "what i have done is to add a MyWidget class to andrew's example which i then create an instance of in VBA and pass it back an instance of the AddInUtils class.  It is at this point that the exception occurs."

    I would require the sample where you added your MyWidget Class. Please upload the scaled down sample project to skydrive and share the link.

    Thanks,

    Sreerenj G Nair

    Tuesday, April 9, 2013 12:47 AM
  • righty

    the solution can be downloaded from here...

    https://skydrive.live.com/?cid=8735110FE4D584D8&id=8735110FE4D584D8%21115

    i've included the Shim, ManagedAggregator & Setup projects, along with a Word 2010 addin that adds a tab 'MCDevNET' to the ribbon.  the button opens the dialog from which the CTP can be opened.

    The casting error does not occur if the Shim is not used and the addin simply runs in the mscoree.dll app domain.
    thanks, matt

    Wednesday, April 10, 2013 9:54 AM
  • just to clarify my previous post....

    when i said "the solution can be downloaded from here..." i was actually referring to the visual studio solution and associated project files, and not "a solution" to my problem.

    the problem still exists, and any help or information as to why this happens would be much appreciated.  I'm sure i'm just not understanding fully the complexities of working between managed, unmanaged and back to managed code across different application domains.

    cheers 

    Thursday, April 11, 2013 11:35 AM
  • hi

    did you manage to have a look at my files that i uploaded to here...

    https://skydrive.live.com/?cid=8735110FE4D584D8&id=8735110FE4D584D8%21115

    matt

    Wednesday, April 17, 2013 8:22 AM
  • Hello Matt,

    I checked the sample that you uploaded. I was getting the exception while loading the add-in. I did some research on it. Unfortunately, I couldn't find the cause of the issue.

    This issue would require more in-depth level of support and falls into a paid support category. Please visit the below link to see the various paid support options that are available to better meet your needs.

    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Thanks,

    Sreerenj G Nair

    Tuesday, April 30, 2013 1:45 PM
  • ok, no probs.

    i managed to find a workaround, which was suitable for use within the application design.  

    Instead of allowing the widget class to be created directly in VBA, i provided a creator method within the managed addInUtils class.  This passed an instance of the widget back to VBA, which could then be used without raising any invalid cast exceptions.

    It works well and is a clean solution, without requiring any major code changes.

    • Marked as answer by Matt Cope Tuesday, May 14, 2013 9:46 AM
    Tuesday, May 14, 2013 9:46 AM