C++ Get IDispatch Pointer from SafeArray of VT_DISPATCH type
See the question and my original answer on StackOverflowWhen using SafeArrayPtrOfIndex there's an extra level of indirection, so you should change your code like this:
void* data;
SafeArrayPtrOfIndex(sa, &idx, &data);
auto disp = *(IDispatch**)data;
disp->AddRef();
Or just use SafeArrayGetElement (and you're supposed to lock the array when using SafeArrayPtrOfIndex
):
IDispatch* disp;
SafeArrayGetElement(sa, &idx, &disp);
disp->AddRef();