C++ COM中IDispatch之Invoke获取对象时,注意点

DISPATCH_PROPERTYGET

The member is retrieved as a property or data member.

这是本文最关键的地址,如果你返回的值是一个COM对象,调用者会增加AddRef吗?不会,所以你需要自己添加计数,否则程序将很快崩溃

补充一条,js脚本在从C++本地语言获取对象时,IE js引擎的做法是,不会记住改对象地址,而是下次要用时重新取,这也就更需要注意引用计数这个问题了

Provides access to properties and methods exposed by an object. The dispatch functionDispInvokeprovides a standard implementation ofInvoke.

Syntax

C++

HRESULT Invoke( [in]DISPID dispIdMember, [in]REFIID riid, [in]LCID lcid, [in]WORD wFlags, [in, out]DISPPARAMS *pDispParams, [out]VARIANT *pVarResult, [out]EXCEPINFO *pExcepInfo, [out]UINT *puArgErr);

ParametersdispIdMember[in]

Identifies the member. UseGetIDsOfNamesor the object’s documentation to obtain the dispatch identifier.

riid[in]

Reserved for future use. Must be IID_NULL.

lcid[in]

The locale context in which to interpret arguments. Thelcidis used by theGetIDsOfNamesfunction, and is also passed toInvoketo allow the object to interpret its arguments specific to a locale.

Applications that do not support multiple national languages can ignore this parameter. For more information, refer toSupporting Multiple National LanguagesandExposing ActiveX Objects.

wFlags[in]

Flags describing the context of theInvokecall.

ValueMeaning

DISPATCH_METHOD

The member is invoked as a method. If a property has the same name, both this and the DISPATCH_PROPERTYGET flag can be set.

DISPATCH_PROPERTYGET

The member is retrieved as a property or data member.

这是本文最关键的地址,如果你返回的值是一个COM对象,,调用者会增加AddRef吗?不会,所以你需要自己添加计数,否则程序将很快崩溃

DISPATCH_PROPERTYPUT

The member is changed as a property or data member.

DISPATCH_PROPERTYPUTREF

The member is changed by a reference assignment, rather than a value assignment. This flag is valid only when the property accepts a reference to an object.

pDispParams[in, out]

Pointer to a DISPPARAMS structure containing an array of arguments, an array of argument DISPIDs for named arguments, and counts for the number of elements in the arrays.

pVarResult[out]

Pointer to the location where the result is to be stored, or NULL if the caller expects no result. This argument is ignored if DISPATCH_PROPERTYPUT or DISPATCH_PROPERTYPUTREF is specified.

pExcepInfo[out]

Pointer to a structure that contains exception information. This structure should be filled in if DISP_E_EXCEPTION is returned. Can be NULL.

puArgErr[out]

The index within rgvarg of the first argument that has an error. Arguments are stored in pDispParams->rgvarg in reverse order, so the first argument is the one with the highest index in the array. This parameter is returned only when the resulting return value is DISP_E_TYPEMISMATCH or DISP_E_PARAMNOTFOUND. This argument can be set to null. For details, seeReturning Errors.

Return value

This method can return one of these values.

Return codeDescription

S_OK

Success.

DISP_E_BADPARAMCOUNT

The number of elements provided to DISPPARAMS is different from the number of arguments accepted by the method or property.

DISP_E_BADVARTYPE

One of the arguments in DISPPARAMS is not a valid variant type.

DISP_E_EXCEPTION

The application needs to raise an exception. In this case, the structure passed inpexcepinfoshould be filled in.

DISP_E_MEMBERNOTFOUND

The requested member does not exist.

DISP_E_NONAMEDARGS

This implementation ofIDispatchdoes not support named arguments.

DISP_E_OVERFLOW

One of the arguments in DISPPARAMS could not be coerced to the specified type.

DISP_E_PARAMNOTFOUND

One of the parameter IDs does not correspond to a parameter on the method. In this case,puArgErris set to the first argument that contains the error.

DISP_E_TYPEMISMATCH

One or more of the arguments could not be coerced. The index of the first parameter with the incorrect type within rgvarg is returned inpuArgErr.

DISP_E_UNKNOWNINTERFACE

The interface identifier passed in riid is not IID_NULL.

DISP_E_UNKNOWNLCID对于旅行,从来都记忆模糊。记不得都去了哪些地方,

C++ COM中IDispatch之Invoke获取对象时,注意点

相关文章:

你感兴趣的文章:

标签云: