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

DISPATCH_PROPERTYGET

The member is retrieved as a property or data member.

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

Provides access to properties and methods exposed by an object. The dispatch function DispInvoke provides
a standard implementation of Invoke.

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
);

Parameters

dispIdMember [in]

Identifies the member. Use GetIDsOfNames or
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. The lcid is used by the GetIDsOfNames function,
and is also passed to Invoke to 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 to Supporting
Multiple National Languages
 and Exposing
ActiveX Objects
.

wFlags [in]

Flags describing the context of the Invoke call.

Value Meaning
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, see Returning
Errors
.

Return value

This method can return one of these values.

Return code Description
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 in pexcepinfo should be filled in.

DISP_E_MEMBERNOTFOUND

The requested member does not exist.

DISP_E_NONAMEDARGS

This implementation of IDispatch does
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, puArgErr is 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

The member being invoked interprets string arguments according to the LCID, and the LCID is not recognized. If the LCID is not needed to interpret arguments, this error should not be returned

DISP_E_PARAMNOTOPTIONAL

A required parameter was omitted.

Remarks

Generally, you should not implement Invoke directly. Instead, use the dispatch interface to create functionsCreateStdDispatch and DispInvoke.
For details, refer to CreateStdDispatchDispInvokeCreating
the IDispatch Interface
 and Exposing
ActiveX Objects
.

If some application-specific processing needs to be performed before calling a member, the code should perform the necessary actions, and then call ITypeInfo::Invoke to
invoke the member. ITypeInfo::Invoke acts exactly like Invoke. The standard implementations of Invoke created by CreateStdDispatch and DispInvokedefer to ITypeInfo::Invoke.

In an ActiveX client, Invoke should be used to get and set the values of properties, or to call a method of an ActiveX object. The dispIdMember argument identifies the member to invoke. The DISPIDs that identify members are defined
by the implementor of the object and can be determined by using the object‘s documentation, the IDispatch::GetIDsOfNames function,
or the ITypeInfo interface.

When you use IDispatch::Invoke() with DISPATCH_PROPERTYPUT or DISPATCH_PROPERTYPUTREF, you have to specially initialize the cNamedArgs and rgdispidNamedArgs elements of your DISPPARAMS structure with the following:

C++

DISPID dispidNamed = DISPID_PROPERTYPUT;
dispparams.cNamedArgs = 1;
dispparams.rgdispidNamedArgs = &dispidNamed;

The information that follows addresses developers of ActiveX clients and others who use code to expose ActiveX objects. It describes the behavior that users of exposed objects should expect.

Requirements


IDL

OaIdl.idl

See also

IDispatch

Community Additions

ADD

Arguments by reference

If the method invoked returns a value via a VARIANT argument that is by ref or marked as [out] but is not the default return value, then for the argument in question, if you pass in a simple VARIANT, you will not get any value back. The VARIANT passed in will
remain unchanged. You have to pass a VARIANT by reference to the Invoke method. This can be set up as follows:

时间: 2024-10-31 12:26:29

C++ COM中IDispatch之Invoke获取对象时,注意点的相关文章

Effective C++ 条款11,12 在operator= 中处理“自我赋值” || 复制对象时不要忘记每一个成分

1.潜在的自我赋值     a[i] = a[j];     *px = *py; 当两个对象来自同一个继承体系时,他们甚至不需要声明为相同类型就可能造成别名. 现在担心的问题是:假如指向同一个对象,当其中一个对象被删,另一个也被删,这会造成不想要的结果. 该怎么办? 比如:   widget& widget:: operator+ (const widget& rhs) {    delete pd;    pd = new bitmap(*rhs.pb);    return *thi

【Swift】ios开发中巧用 description 打印对象时,打印对象的属性

ios开发中我们打印对象的时候,会直接输出对象地址,这样不方便我们开发.我们可以 巧用 description 打印对象时,输出对象的属性 在oc中直接重写即可.swift中需要遵守Printable协议 看下面的例子 1 override var description: String { 2 let properties = ["属性1", "属性2", "属性3", "属性4"] 3 4 return "\(d

Android开发中解析、创建Bitmap对象时OOM的有效解决方法并附上一些干货

先来点鸡汤: Stay hungry,stay foolish 这句话的的解读:我们必须了解自己的渺小.如果我们不学习,科技发展的速度会让我们五年后被清空.所以,我们必须用初学者谦虚的自觉,饥饿者渴望的求知态度,来拥抱未来的知识. 这几天做的项目中需要从图库选择图片或者拍照生成图片,然后展现在IamgeView控件上.当然,从图库选择图片和拍照选择图片的功能实现起来很简单.直接写上代码: CharSequence[] items = { "拍照", "图库" };

从值栈获取对象

-------------------siwuxie095 从值栈获取对象 1.具体步骤 (1)在 Action 中向值栈放对象 (2)在 JSP 页面中从值栈获取对象 2.具体实现 (1)编写实体类 User.java: package com.siwuxie095.entity; // User 实体类 public class User { private String username; private String password; private String address; p

document获取对象三方法

Document对象中有几个常用的方法,我们在Dom简介中提到过.说到获取javascript对象的方法,最常用的可能就是getElementById了,它是Document中最常用的获取对象的方式之一,另外还有两个常用的获取对象的方法是getElementsByTagName 和getElementsByName.其中getElementById获取到的是单对象,而getElementsByName和 getElementsByTagName 获取到的都是集合. 现在我们有一个form表单,内

javascript获取对象直接量中的属性和属性值

javascript获取对象直接量中的属性和属性值:所谓的对象直接量简单来说就是,由大括号包裹的键值对列表,例如: var antzone={ webName:"蚂蚁部落", address:"青岛市南区", age:2 } 下面介绍一下如何获取对象直接量中的属性和属性值,代码如下: var antzone={ webName:"蚂蚁部落", address:"青岛市南区", age:2 } for(var prop in a

一个关于el中获取对象属性的错误

具体过程描述为: 在JSP页面中通过<%%>定义一个非public类.并定义两个属性相应的get和set方法.在该代码段中实例化该对象,并将该对象放入Request域中. 然后在页面中使用el表达式通过 对象名.属性名 的方式获取 这个对象的私有属性.但是在执行时便出现了上面的异常. 原因: 在JSP页面中定义的类是非公有的.而el表达式获取对象私有属性的机制是通过反射的方式获取这个类的对象,然后将该属性的第一个字符大写并在前面加上get拼接成获取的方法.然后通过调用这个方法了得到这个私有属性

Javascript中获取对象的原型对象

在Javascript中,如果我们有一个对象但是又不知道它的构造函数时,如何获取它的原型对象呢? 在Chrome中或是FireFox浏览器中,我们可以直接使用对象的__proto__属性获取它的原型对象. <!-- lang: js --> function F(){}; var foo = new F(); alert(foo.__proto__ == F.prototype); 但是,__proto__属性在IE浏览器中一直到IE11才被支持. 那么在不支持__proto__属性的浏览器中

Dubbo2.7用curator客户端从Zookeeper中获取对象

实验环境:win7 64,idea2018.3.3 环境搭建 1.新建一个空项目 2.然后添加一个maven模块作为父模块名字为ticketparent 3.然后添加一个spring模块名字为supplier-ticket用来生产对象 4.然后再添加一个spring模块名字为consumer-user用来消费 5.给maven父模块再指定一层父模块 <parent> <groupId>org.springframework.boot</groupId> <arti