接口和结构的映射

From Apache OpenOffice Wiki
< Zh‎ | Documentation
Revision as of 03:07, 14 May 2009 by Jirong (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search



UNO 接口或结构在 Automation 环境中表示为分发对象。也就是说,转换的值可成为实现 IDispatch 的对象。如果映射了 UNO 接口,您还可以通过 IDispatch 访问该对象的其他所有 UNO 接口。换句话说,分发对象可表示 UNO 对象及其所有接口,而不是只表示被转换的那个接口。


现在,如果传送回 UNO 的的分发对象实际上为 UNO 对象或结构,则桥将提取原始的 UNO 接口或结构并进行传送。由于 UNO 分发对象表示整个 UNO 对象,即支持其包含的所有接口,因此,对于所有接口类型,您都可以将该分发对象用作参数。例如:

 //UNO IDL methods
 XFoo getFoo();
 void doSomething(XBar arg);
 
 'VB
 Dim objUno As Object
 Set objUno = objOtherUnoObject.getFoo()
 
 'The returned interface belongs to an UNO object which implements XFoo and XBar.
 'Therefore we can use objUno in this call:
 call objOtherUnoObject.doSomething(objUno)


如果从 UNO 调用 Automation 对象,则调用的方法可能会将其他 Automation 对象作为 IUnknown*IDispatch* 返回。然后,可以在后面的 Automation 对象调用中将它们用作参数,也可以直接调用。如果为 IUnknown,则该对象还必须支持 IDispatch。要 从 UNO 调用,必须先从 XInterface 中查询 XInvocation。当某一方法返回 IDispatch 时,在 UNO 端将接收到 XInvocation,并且可以立即进行调用。


当将这些接口作为参数传送回对 Automation 对象进行的调用时,桥将传递原始的 IUnknownIDispatch 指针。具体情况取决于参数的类型。请记住:只能对 Automation 对象执行调用。因此,IUnknownIDispatch 是唯一可能的 COM 接口。如果所需的参数为 VARIANT,则将 Automation 对象作为 IUnknown* 传送到 UNO 环境中,该参数将包含 IUnknown*。如果将该对象作为 IDispatch* 传送,则该参数将包含 IDispatch*。例如:

 //MIDL
 HRESULT getUnknown([out,retval] IUnknown ** arg);
 HRESULT getDispatch([out, retval] IDispatch ** arg);
 
 HRESULT setUnknown([in] IUnknown * arg);
 HRESULT setDispatch([in] IDispatch * arg);
 HRESULT setVariant([in] VARIANT arg);
 
 'StarBasic
 Dim objUnknown As Object
 Dim objDispatch As Object
 
 Set objUnknown = objAutomation.getUnknown()
 Set objDispatch = objAutomation.getDispatch()
 
 
 objAutomation.setUnknown objUnknown 'Ok
 objAutomation.setDispatch objUnknown 'Ok, if objUnknow supports IDispatch, 
                                        otherwise a CannotConvertException will be thrown.
 objAutomation.setUnknown objDispatch 'OK
 
 objAutomation.setVariant objUnknown 'VARTYPE is VT_Unknown
 objAutomation.setVariant objDispatch 'VARTYPE is VT_DISPATCH


为了接收事件(侦听器),可以将 UNO 接口作为分发对象实现 (专业 UNO - UNO 语言绑定 - Automation 桥 - 具有 UNO 接口的 Automation 对象)。该类型的对象可用作 UNO 函数中的参数,该函数需要特定的接口类型。桥将确保为 UNO 函数提供正确的接口。随后,如果将 UNO 接口传送到 Automation 环境中,则将传送原始的 Automation 对象。


如果将 Automation 对象作为 any 的参数传送,则将该对象作为 IUnknown 传送时,该 any 将包含 XInterface;将该对象作为 IDispatch 传送时,该 any 将包含 XInvocation。例如,如果将 UNO 接口 XFoo 作为分发对象实现,将实例作为 Any 参数传送给 UNO,并且 Any 包含 XFoo 而不包含 XInvocation,则必须将该分发对象放在值对象中 (专业 UNO - UNO 语言绑定 - Automation 桥 - 类型映射 - 值对象)。例如:

 //UNO method
 void foo([in] any)
 
 'objUno contains an interface with the method foo.
 'It expects that the argument with of type any contains an XFoo
 
 'objFoo is a dispatch object implementing XFoo.
 
 Dim objValueObject As Object
 Set objValueObject = objServiceManager.Bridge_GetValueObject()
 objValueObject.set "XFoo", objFoo
 
 objUno.foo objValueObject


空指针将被转换成所需类型的空指针。也就是说,如果将值为空的 IDispatch 指针作为参数传递到 UNO 方法,则最终参数是所需类型的空指针。这也适用于 UNO 接口指针,这些指针通过调用被传递到 Automation 对象。当 UNO 方法接受结构作为参数,并且从提供空指针(IDispatchIUnknown)的 Automation 环境中进行调用时,该 UNO 方法将接受默认构造的结构。


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages