Question

I want to get a proper IDispatch pointer then cast it to CMyDispatch pointer and have my way with it later.

i.e. in javascript I want to do something like this:

var x = external.obj.x;
var y = external.obj.y;
external.obj.x = y;

where x and y are instances of CMyDispatch.

CMyDispatch is returned to javascript this way:

STDMETHODIMP CMyDispatch::Invoke(DISPID dispIdMember, REFIID, LCID, WORD wFlags,
                                 DISPPARAMS* pDispParams, VARIANT* pVarResult,
                                 EXCEPINFO*, UINT*) {
  if( pVarResult )
  {
    CMyDispatch* pDisp = new CMyDispatch();
    CComVariant val( pDisp );
    val.Detach( pVarResult );
  }
  return S_OK;
}

In CMyDispatch.Invoke() with DISPATCH_PROPERTYPUT flag I want to get CMyDispatch instance that holds y value.

When using the following code, pDispatch is set to some garbage:

STDMETHODIMP CMyDispatch::Invoke(DISPID dispIdMember, REFIID, LCID, WORD wFlags,
                                 DISPPARAMS* pDispParams, VARIANT* pVarResult,
                                 EXCEPINFO*, UINT*) {
  ASSERT( pDispParams->cArgs == 1 );
  ASSERT( VT_DISPATCH == pDispParams->rgvarg[0].vt );
  IDispatch* pDisp = ( pDispParams->rgvarg[0].pdispVal ); // <-- garbage
  CMyDispatch* pDispatch = (CMyDispatch*) pDisp; // <-- garbage
  return S_OK;
}

What should I do to get proper CMyDispatch pointer? Thank you.

Was it helpful?

Solution

You really shouldn't, downcasting from an interface to a concrete implementation is the first step on the road to doom.

That said, what you are doing should work, unless the javascript and the COM object run in different apartments, and you get a proxy passed to you, instead of the real object.

Why do you need to downcast?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top