I have a legacy c++ component.It consumes ATL Controls of similar nature.Prog ID is input to this component. It uses the progid to create ATL object and it uses dispatchid to call methods from ATL control using InvokeHelper as below,
DISPID methodDispID; // dispatchid
long lValue = 0; // first argument long
short nIdx = 1 // second argument short
VARIANT varValue; // third argument variant
long returnCode = 0; // return code long
static BYTE parms[] = VTS_I4 VTS_I2 VTS_PVARIANT;
InvokeHelper(methodDispID, DISPATCH_METHOD, VT_ERROR, (void*)&returnCode, parms, &varValue, lValue, nIdx);
In C++ ActiveX method looks below,
[id(1), helpstring("method GetValue")] long GetValue(long lValue, short nIdx,VARIANT *varValue);
Now I am trying to introduce Active X in C#, means I have a Usercontrol, I am giving this C# usercontrol's progid to the legacy component, It creates the object of C# usercontrol and tries to call the GeValue method of C# Usercontrol which is defined below,
C#
interface IMyInterface
{
[DispId(1)] Int32 GetValue(System.Int32 lOrder, System.Int16
nIdx,object varval);
}
class MyClass : IMyInterface
{
System.Int32 GetValue(System.Int32 lValue, System.Int16 nIdx,
ref object vCBValue)
{
}
}
The call is coming to GetValue method of C# usercontrol, but it's throwing the exception
System.Runtime.InteropServices.COMException at System.RuntimeType.ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)
I tried to change C# method definitions as below, but nothing worked,
System.Int32 GetValue(System.Int32 lValue, System.Int16 nIdx,
[MarshalAs(UnmanagedType.Struct)] ref object varValue);
System.Int32 GetValue(System.Int32 lValue, System.Int16 nIdx,
ref IntPtr varValue);
How to pass variant* to C# through InvokeHelper ?
Note: The component code to call the method using InvokeHelper should be same for both C++ and C# usercontrols