1

I have written a proof-of-concept COM Callable Wrapper in order to replace an existing COM component with new functionality. A constraint of my project is that I cannot change the calling code; the calling code should be ignorant of the presence of the COM Callable Wrapper. The little proof-of-concept solution I have created works if the calling code is .NET 2.0 but fails with an InvalidCastException if the calling code is in .NET 4.0. Can someone help me discover the cause of this .NET 4.0-specific InvalidCastException?

COM Callable Wrapper:

using System.EnterpriseServices;
using System.Runtime.InteropServices;

namespace DNNXPOC.CCWTestA
{
    [ComVisible(true)]
    [Guid("39D50FA3-DF73-4A3B-A4F8-4D21F5A27E83")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface ItestA
    {
        [DispId(1)]
        int process();
    }

    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    [Guid("1A6ECD84-5147-4EF8-89A4-0F9AC5F3915B")]
    [ProgId("NNXPOC.testA")]
    [ComDefaultInterface(typeof(ItestA))]
    public class testA : ServicedComponent, ItestA
    {
        public int process()
        {
            const int result = 3;
            return result;
        }
    }
}

Calling code (exception is throw at new line):

using System;
using System.Runtime.InteropServices;
using NNXPOCLib;

namespace DNNXPOC
{
    public class TestAAccessWrapper : IDisposable
    {
        private ItestA _testA;
        private bool _isDisposed;

        public TestAAccessWrapper()
        {
            _testA = new testAClass();
        }

        public ItestA testA { ... }

        public void Dispose() { ... }
    }
}
Mark
  • 1,884
  • 2
  • 25
  • 36
  • Why are you testing a CCW from the runtime? I suggest using WScript to test the ability to call a CCW. – Ben Apr 23 '12 at 12:53
  • But I know the CCW can be called because the wrapper works under .NET 2.0. It's when I use .NET 4.0 that the wrapper throws an exception when it tries to instantiate `testAClass`. I have a feeling it is something to do with the Interop.NNXPOCLib.dll RCW but I'm uncertain and I don't know how to prove. – Mark Apr 23 '12 at 13:30
  • Is it inproc or out of proc? Are both the caller and the callee running the same version of the runtime? And to clarify, you are replacing a legacy COM object not written in .Net, with a new COM object with the same signature, but written in .Net? – Ben Apr 23 '12 at 13:48
  • Out of proc. If both caller and callee are in .NET 4, it doesn't work. If caller is .NET 2 and callee is .NET 4, it works fine. I'm writing a COM object with the same interface & GUIDs to "intercept" calls to the old/legacy C++ COM object. The new COM object is in C#.NET, yes. – Mark Apr 23 '12 at 14:01
  • And is the caller using the same RCW (not regenerating it) in both cases? I.e. using an RCW which was built from the type library of the native legacy DLL? – Ben Apr 23 '12 at 14:06
  • Correct. (I'll bounce into C# chat in case it's easier to discuss there...) – Mark Apr 23 '12 at 14:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10394/discussion-between-ben-and-mark-ingram) – Ben Apr 23 '12 at 14:42

0 Answers0