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() { ... }
}
}