tags:

views:

316

answers:

1

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.

+1  A: 

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?

Kim Gräsman
i want to be able to set **external.obj.x** property to **y** object, that is **IDispatch** implementation.
mojo
and to do it, I have to get concrete implementation instance, interface is not enough, if I am correct.
mojo
I was able to get needed properties via this proxy pDisp->Invoke(). It's not answer to the question, but solves an issue that raised the question.Thanks.
mojo
That sounds like a good, working solution. Glad it worked out!
Kim Gräsman
Downcasting can break quickly with a simple windows update - there's a multitude of reasons why you don't see the actual implementation, but just a proxy.
peterchen