Calling a COM Object with a VARIANT* param from C# just shows ref object varParam

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I wish to call a COM Object which has a parameter of type VARIANT* on
a method call from C#

The COM Method :-
STDMETHODIMP CClassType::GetTypeIDs(VARIANT * TypeIDs)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
if (DeviceTypeIDs->vt != (VT_I2|VT_ARRAY))
return E_INVALIDARG ;
VARIANT* ptr ;
SafeArrayAccessData (TypeIDs->parray, (void**) &ptr) ;
ptr[0].iVal = 1 ;
ptr[1].iVal = 2;
ptr[2].iVal = 3;
ptr[3].iVal = 4;
ptr[4].iVal = 5;
ptr[5].iVal = 6;
SafeArrayUnaccessData (TypeIDs->parray) ;
return S_OK;
}

How do I create the parameter required within C# in order to call this
COM Method. I've read quite a few postings on marshalling and the
such, but is there anyone that can explain this in simple terms or
code samples would be good.

I think my main problem is C# knowledge, as I have only just started
using it

TIA for even the smallest hint on how to resolve this problem
 
what about

Int16[] arrParam;
//initialize arrParam
comobject.method( arrParam );


/Morten
 
Thanks for this Morten,

Visual Studio automatically puts a wrapper around the COM Object for
me which is great and it specifies the parameter as a "ref Object var"
so when I call it I must specifiy it as follows

Int16[] list = new Int16[6];
//Initialise array

//Have to cast the array to an Object otherwise it will not compile
object test1 = list;

SampleVariant.CSampleClass sv = new CSampleClass();
sv.MySampleCall(ref list);

the Call on the COM method appears to work, as the first part which
checks the arg type works correctly, but when the COM method exits and
returns to the caller it throws an Unhandled Exception.

Any ideas?
Thanks
Dave
 
Back
Top