none
system Invalid Cast Exception RRS feed

  • Question

  • We have an add-in in outlook which fires an exception in the line

    Object obj = m_window.CurrentItem

    Caught an exception while notifying that work was done System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Outlook._Inspector'.

    This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063005-0000-0000-C000-000000000046}' failed due to the following error:

    Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)).

    m_window is  defined as

    outlook.inspector m_window

    Is this the case where

    4.go to  HKEY_CLASSES_ROOT\Interface\{00063005-0000-0000-C000-000000000046}\TypeLib

    5.Click on TypeLib  verify that  the  value of  (Default)should be  {00062FFF-0000-0000-C000-000000000046}

    6.Go to  HKEY_CLASSES_ROOT\TypeLib\{00062FFF-0000-0000-C000-000000000046}

     value should be present as 9.4 as outlook 2010 is installed.

    This happens on some win7 machines for clients.

    Should I procced in some other direction.??

    I am speculating some discordance in the registry 00063005-0000-0000-C000-000000000046

    Thanks in Advance.


    Wednesday, June 12, 2013 10:23 AM

Answers

  • In general, when an Inspector is opened, and the NewInspector() event fires the Inspector is what's called a weak object reference. Most properties of the Inspector have not yet been filled in.

    In that event handler you generally check the MessageClass or Class of Inspector.CurrentItem just to see if you want to handle that Inspector. If you do want to handle it you set up to handle the Inspector.Activate() event and the Open() event for the type of item (MailItem.Open for example).

    When those events fire you can retrieve and set the various propeties, as by that time the object references are strong references.


    Ken Slovak MVP - Outlook

    Wednesday, June 19, 2013 2:07 PM
    Moderator
  • basicaly inside loop everything works fine, but once you assing inspector to m_Window and use it somewhere else it breaks down. Where exactly do you use this m_Window? in what outlook event? do you try to use it perhaps in some other thread then main UI one?
    Thursday, June 20, 2013 9:47 AM
  • This was indeed a set up  issue.As i mentioned in my  mail ,many registry entries were missing from the problematic system.

    After performing  a windows upgrade,and re installing outlook,proper registry entries were made in the system..

    and the problem was resolved.

    • Marked as answer by Ankit3111 Friday, June 28, 2013 5:51 AM
    Friday, June 28, 2013 5:51 AM

All replies

  • How do you initialize the m_window variable?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, June 12, 2013 2:27 PM
  •  for (int i = 1; i <= inspectors.Count; i++)
                                {
                                    Outlook.Inspector e = inspectors.Item(i);
                                    try
                                    {
                                        IntPtr checkPtr;
                                        IOleWindow oleWinPossible = e as IOleWindow;
                                        if (oleWinPossible != null)
                                        {
                                            oleWinPossible.GetWindow(out checkPtr);
                                            if (checkPtr == targetPtr)
                                            {
                                                m_Window = e;

     <summary>
        /// Implemented and used by containers and objects to obtain window handles
        /// and manage context-sensitive help.
        /// </summary>
        /// <remarks>
        /// The IOleWindow interface provides methods that allow an application to obtain
        /// the handle to the various windows that participate in in-place activation,
        /// and also to enter and exit context-sensitive help mode.
        /// </remarks>
        [ComImport]
        [Guid("00000114-0000-0000-C000-000000000046")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IOleWindow
        {
            /// <summary>
            /// Returns the window handle to one of the windows participating in in-place activation
            /// (frame, document, parent, or in-place object window).
            /// </summary>
            /// <param name="phwnd">Pointer to where to return the window handle.</param>
            void GetWindow(out IntPtr phwnd);

            /// <summary>
            /// Determines whether context-sensitive help mode should be entered during an
            /// in-place activation session.
            /// </summary>
            /// <param name="fEnterMode"><c>true</c> if help mode should be entered;
            /// <c>false</c> if it should be exited.</param>

    Thursday, June 13, 2013 9:15 AM
  • I don't know if this will help, but the interface declaration I use for GetWindow() looks like this:

    [ComImport]       

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]       

    [Guid("00000114-0000-0000-C000-000000000046")]       

    internal interface IOleWindow { void GetWindow(out System.IntPtr phwnd); void ContextSensitiveHelp([In] bool fEnterMode);}

    Also, where are you getting the targetPtr you're using for comparison?


    Ken Slovak MVP - Outlook

    Thursday, June 13, 2013 2:25 PM
    Moderator
  • if you directly call .CurrentItem in your code inside loop

     for (int i = 1; i <= inspectors.Count; i++)
                                {
                                    Outlook.Inspector e = inspectors.Item(i);

    Object o = e.CurrentItem
                                   

    does it crash also there? What type of item is opened in inspector?

    Thursday, June 13, 2013 2:48 PM
  • @Ken Slovak

    I am using the definition of GetWindow that you pointed out.

    I forgot to mention it in the code

    targetPtr is initializesd just above the for loop

    as

    IntPtr targetPtr;
                            IOleWindow oleWin = inspector as IOleWindow;

    oleWin.GetWindow(out targetPtr);

    Friday, June 14, 2013 5:38 AM
  • Have you looked at the open Inspector windows using Spy++ to see their actual window hWnd's and see if what you see there is the same as what you see in your code if you step the code in the debugger?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Ankit3111" <=?utf-8?B?QW5raXQzMTEx?=> wrote in message news:621eefc3-9e26-4895-9b9e-4aec1bf25961...

    @Ken Slovak

    I am using the definition of GetWindow that you pointed out.

    I forgot to mention it in the code

    targetPtr is initializesd just above the for loop

    as

    IntPtr targetPtr;
                            IOleWindow oleWin = inspector as IOleWindow;

    oleWin.GetWindow(out targetPtr);


    Ken Slovak MVP - Outlook
    Monday, June 17, 2013 3:37 PM
    Moderator
  • @DamianD

    if i use  e.currentItem in the for loop as you suggested ,no exception is thrown

    But in the place where the exception is thrown,if i use any property of  m_window which is of outlook.inspector type ,exception is thrown.

            public MailItem Message
            {
                get
                {
                    if (m_Window == null)
                    {
                        return null;
                    }
                    
                    object obj = m_Window.CurrentItem;
                    if (obj != null)
    object obj = m_Window.CurrentItem;----HERE ,the same exception is thrown, if any outlook.inspector property is used

    like  m_window.WindowState

     

    Wednesday, June 19, 2013 5:36 AM
  • In general, when an Inspector is opened, and the NewInspector() event fires the Inspector is what's called a weak object reference. Most properties of the Inspector have not yet been filled in.

    In that event handler you generally check the MessageClass or Class of Inspector.CurrentItem just to see if you want to handle that Inspector. If you do want to handle it you set up to handle the Inspector.Activate() event and the Open() event for the type of item (MailItem.Open for example).

    When those events fire you can retrieve and set the various propeties, as by that time the object references are strong references.


    Ken Slovak MVP - Outlook

    Wednesday, June 19, 2013 2:07 PM
    Moderator
  • i found out that on problematic systems the entries for the following  registries are missing

    HKEY_CLASSES_ROOT\Interface\{00063005-0000-0000-C000-000000000046}

    HKEY_CLASSES_ROOT\Interface\{00063006-0000-0000-C000-000000000046}

    HKEY_CLASSES_ROOT\Interface\{00063007-0000-0000-C000-000000000046}

    and more like this

    How could these be missing?these registries are needed for marshaling COM interfaces between
    client and server, and that includes type library marshaling.

    Thursday, June 20, 2013 6:38 AM
  • but these keys are present in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Classes\Interface

    how that's possible?

    Because The content of HKEY_CLASSES_ROOT comes from two sources: HKEY_LOCAL_MACHINE\SOFTWARE\Classes and HKEY_CURRENT_USER\SOFTWARE\Classes. If a subkey or entry appears in either location, then it also appears in HKEY_CLASSES_ROOT.

    Thursday, June 20, 2013 9:33 AM
  • basicaly inside loop everything works fine, but once you assing inspector to m_Window and use it somewhere else it breaks down. Where exactly do you use this m_Window? in what outlook event? do you try to use it perhaps in some other thread then main UI one?
    Thursday, June 20, 2013 9:47 AM
  • This was indeed a set up  issue.As i mentioned in my  mail ,many registry entries were missing from the problematic system.

    After performing  a windows upgrade,and re installing outlook,proper registry entries were made in the system..

    and the problem was resolved.

    • Marked as answer by Ankit3111 Friday, June 28, 2013 5:51 AM
    Friday, June 28, 2013 5:51 AM