A day with .Net

My day to day experince in .net

Archive for December, 2014

Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)) ,COM Exception in C#

Posted by vivekcek on December 30, 2014

I was trying to use an ActiveX(OCX) control in a windows forms application, I am not sure in which language this control was developed, may be c++ or VB.

Anyway i added this control to my toolbox by browsing the OCX.

When i was trying to access a method in that control ,i got the above type mis-match COM exception.
The method signature is given below. The control is kind of grid, and i am trying to access a cell value. Here the last reference parameter will be filled with result

bool success=OcxControl.GetValue(int row, int coulmn, ref object cellValue)

i used this method as below in c#.

object value=null;
bool success=OcxControl.GetValue(1, 2, ref Value)

Then i got the exception , Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)) ,COM Exception

The problem is with reference variable, so how to call this method. Try the below code.

ParameterModifier modifier = new ParameterModifier(3);
modifier[0] = false;
modifier[1] = false;
modifier[2] = true; // third parameter is refrence type

object value = null;

Object[] args = new Object[3];
args[0] = 1;
args[1] = 2;
args[2] = value;

Object result = OcxControl.GetType().InvokeMember(
                "GetValue",                           // name
                BindingFlags.InvokeMethod,            // invokeAttr
                null,                                 // binder
                OcxControl,                           // target
                args,                                 // args
                new ParameterModifier[] { modifier }, // modifiers
                null,                                 // culture
                null                                  // namedParameters


ParameterModifier is used to indicate 3rd parameter is of reference type.


Posted in c#.net | Tagged: , | 2 Comments »