有许多关于如何使用 BSTR* 参数调用 IDispatch::Invoke 的示例。我有这个与许多其他“SomeType *”参数一起使用,但无论我尝试什么,我都会得到类型不匹配的 HRESULT、E_OUTOFMEMORY 或访问冲突。在我看来,我在记忆方面做错了,但我正在遵循我发现的不同示例......作为旁注,最终的“[out] UINT puArgErr”参数永远不会填充导致参数索引问题。但是,我知道这是 BSTR 类型的第三个参数(我已经成功调用了另一个方法,它采用了前面的两个参数。)
VARIANTARG* v = new VARIANTARG[3];
//...Init my first 2 args
//Code omitted for initializing args 1 and 2 and wrapping everything up to call IDispatch->Invoke
//... Variation 1
VariantInit(v[2]);
BSTR val = SysAllocString(L"");
v[2].vt = VT_BSTR | BT_BYREF;
v[2].pbstrVal = &val;
//When I wrap everything up in the call to IDispatch::Invoke
//this yields a HRESULT of Type Mismatch
*
//...Variation 2
VariantInit(v[2]);
BSTR val = SysAllocString(L"");
v[2].vt = VT_BSTR | BT_BYREF;
v[2].bstrVal = val;
//When I wrap everything up in the call to IDispatch::Invoke
//this yields a HRESULT of E_OUTOFMEMORY
*
//...Variation 3
VariantInit(v[2]);
BSTR val = SysAllocString(L"RandomStringLargerThanTheMethodWillPlaceInTheOutParam");
v[2].vt = VT_BSTR | BT_BYREF;
v[2].bstrVal = val;
//When I wrap everything up in the call to IDispatch::Invoke
//this yields an access violation
*
//...Variation 4
VariantInit(v[2]);
BSTR val = 0;
v[2].vt = VT_BSTR | BT_BYREF;
v[2].bstrVal = val;
//When I wrap everything up in the call to IDispatch::Invoke
//this yields and HRESULT of 0x800706f4 A null reference pointer
//was passed to the stub.
我不明白为什么当我关注 BTR* 参数的其他示例时会发生这种情况……此外,我还有许多其他成功调用 IDispatch::Invoke 的方法,但是这个 BTR* 让我停了下来。
绝望,请帮助!
添加:
IDL 为:[id(0x00000171)] short GetCategory(short nIndx, short* nCat, BSTR* bszName);