locked
How to stop my IEnumDebugModules interface from raising a System.Reflection.TargetInvocationException. RRS feed

  • Question

  • Hi - 

    Implementing a custom debug engine, and I've run into a situation where my implementation of the IEnumDebugModules raises the System.Reflection.TargetInvocationException.  

    The order of events:

    The program receives the IDebugProgram2.EnumModules(out IEnumDebugModules2 ppEnum) call, which I answer like this:

    int IDebugProgram2.EnumModules(out IEnumDebugModules2 ppEnum){       

    ppEnum = new DebugEngine.TestModuleEnum(ref m_TestModules);       

    return VSConstants.S_OK;   

    }

    The DebugEngine.TestModuleEnum class is defined like this (and if you're familiar with writing VS debug extensions, it probably looks familiar):

        public class TestModuleEnum : IEnumDebugModules2
        {
            readonly TestModule[] m_data;
            uint m_position;
    
            public TestModuleEnum(ref List<TestModule> listOfModules)
            {
                m_data = listOfModules.ToArray();
                m_position = 0;
            }
    
            int IEnumDebugModules2.Clone(out IEnumDebugModules2 ppEnum)
            {
                ppEnum = null;
                return VSConstants.E_NOTIMPL;
            }
    
            int IEnumDebugModules2.GetCount(out uint pcelt)
            {
                pcelt = (uint)m_data.Length;
                return VSConstants.S_OK;
            }
    
            int IEnumDebugModules2.Next(uint celt, IDebugModule2[] rgelt, ref uint celtFetched)
            {
                return Move(celt, rgelt, out celtFetched);
            }
    
            int IEnumDebugModules2.Reset()
            {
                lock (this)
                {
                    m_position = 0;
    
                    return VSConstants.S_OK;
                }
            }
    
            int IEnumDebugModules2.Skip(uint celt)
            {
                uint celtFetched;
    
                return Move(celt, null, out celtFetched);
            }
    
            private int Move(uint celt, IDebugModule2[] rgelt, out uint celtFetched)
            {
                lock (this)
                {
                    int hr = VSConstants.S_OK;
                    celtFetched = (uint)m_data.Length - m_position;
    
                    if (celt > celtFetched)
                    {
                        hr = VSConstants.S_FALSE;
                    }
                    else if (celt < celtFetched)
                    {
                        celtFetched = celt;
                    }
    
                    if (rgelt != null)
                    {
                        for (int c = 0; c < celtFetched; c++)
                        {
                            rgelt[c] = m_data[m_position + c];
                        }
                    }
    
                    m_position += celtFetched;
    
                    return hr;
                }
            }
        }

    The inner exception message for the System.Reflection.TargetInvocationException is:

    "Unable to cast object of type 'DebugEngine.IEnumDebugModules2' to type 'Microsoft.VisualStudio.Debugger.Interop.IEnumDebugModules2'."

    Perhaps I've stared at this too long, but it seems like the interface is properly defined (it does build without a complaint).  Further, while debugging, I've successfully called Invoke on the interface without a problem.  

    In advance, thanks for the help!

    Scott Osborn


    Friday, May 23, 2014 9:27 PM

Answers

  • Hi - 

    First, thanks for the suggestions.  However, I'd already read these articles (and, as it turns out, went down the wrong path as a result).  

    For the sake of the next guy or gal who might run into this problem:

    Turns out that I was referring to the wrong assembly.  The final fix was:

    1)Remove the reference to Microsoft.VisualStudio.Debugger.Interop
    2)Add references to Microsoft.VisualStudio.Debugger.Interop10.0, Microsoft.VisualStudio.Debugger.Interop11.0, and Microsoft.VisualStudio.Debugger.InteropA.

    Thanks!

    • Marked as answer by ScottEOsborn Tuesday, May 27, 2014 3:37 PM
    Tuesday, May 27, 2014 3:24 PM

All replies

  • Hi,

    Welcome here!

    Please refer to this thread System.Reflection.TargetInvocationException, what's the problem? to know what the exception.

    The exception that is thrown by methods invoked through reflection. This class cannot be inherited.

    Also, please read the content below "Using Visual Studio Watch Window to Get Exception Details" of this blog

    http://denvycom.com/blog/debug-targentinvocationexception/

    I think the above information will help troubleshot this issue.

    Best regards,

    Monday, May 26, 2014 7:34 AM
  • Hi - 

    First, thanks for the suggestions.  However, I'd already read these articles (and, as it turns out, went down the wrong path as a result).  

    For the sake of the next guy or gal who might run into this problem:

    Turns out that I was referring to the wrong assembly.  The final fix was:

    1)Remove the reference to Microsoft.VisualStudio.Debugger.Interop
    2)Add references to Microsoft.VisualStudio.Debugger.Interop10.0, Microsoft.VisualStudio.Debugger.Interop11.0, and Microsoft.VisualStudio.Debugger.InteropA.

    Thanks!

    • Marked as answer by ScottEOsborn Tuesday, May 27, 2014 3:37 PM
    Tuesday, May 27, 2014 3:24 PM
  • Hi,

    Glad to hear that you got it working!

    And thank you for sharing the solution here!

    Best regards,

    Wednesday, May 28, 2014 1:53 AM