类型的用法

From Apache OpenOffice Wiki
Jump to: navigation, search



接口

许多 UNO 接口函数将接口作为参数。如果是这样,可以通过三种方法获取支持所需接口的实例:


如果通过服务管理器或返回接口的另一个 UNO 函数来调用 createInstance(),则包装返回的对象,这样该对象就看起来像一个 COM 分发对象。如果调用某个 UNO 函数时传送该对象,就会从包装程序中提取原始 UNO 对象,而且桥确保将正确的接口传送给该函数。如果使用 UNO 对象,就无需处理 UNO 接口。确保在通过调用某个 UNO 对象而获取的对象实现了正确的接口后,再将该接口传送回另一个 UNO 调用。


结构

Automation 不识别结构,因为结构存在于其他语言,例如 C++。而是使用 Automation 对象,此类对象包含一组与 C++ 结构字段类似的属性。设定或读取成员时最终需要调用 IDispatch::Invoke。但在 VB、VBScript 和 JScript 等语言中,接口调用由于编程语言而变得模糊。访问属性像使用 C++ 结构一样容易。

 // VB. obj is an object that implements a UNO struct
 obj.Width= 100
 obj.Height= 100


无论 UNO 函数何时需要把结构作为参数,必须从 UNO 环境获取该结构。不能声明一个结构。例如,假定有一个办公软件函数 setSize(),该函数需要一个类型为 Size 的结构。该结构的声明如下:

 // UNO IDL
 struct Size
 {
     long Width;
     long Height;
 }
 
 // the interface function, that will be called from script
 void XShape::setSize( Size aSize)


不能编写类似以下示例所示的代码 (VBScript):

 Class Size
     Dim Width
     Dim Height
 End Class
 
 'obtain object that implements Xshape
 
 'now set the size
 call objXShape.setSize( new Size) // wrong


可以通过调用服务管理器对象的 com.sun.star.reflection.CoreReflection 服务或 Bridge_GetStruct 函数来创建结构。以下示例使用的是 CoreReflection 服务

 'VBScript in Windows Scripting Host
 Set objServiceManager= Wscript.CreateObject("com.sun.star.ServiceManager")
 
 'Create the CoreReflection service that is later used to create structs
 Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
 'get a type description class for Size
 Set classSize= objCoreReflection.forName("com.sun.star.awt.Size")
 'create the actual object
 Dim aSize
 classSize.createObject aSize
 'use aSize
 aSize.Width= 100
 aSize.Height= 12
 
 'pass the struct into the function
 objXShape.setSize aSize


而下一个示例则说明如何使用 Bridge_GetStruct

 Set objServiceManager= Wscript.CreateObject("com.sun.star.ServiceManager")
 Set aSize= objServiceManager.Bridge_GetStruct("com.sun.star.awt.Size")
 'use aSize
 aSize.Width= 100
 aSize.Height= 12
 
 objXShape.setSize aSize


Bridge_GetStruct 函数由服务管理器对象提供,该对象最初由 CreateObject (Visual Basic) 或 CoCreateInstance[Ex] (VC++) 创建。


对应的 C++ 示例看起来比较复杂,但归根结底都是必需的相同步骤。通过调用 CoreReflection 服务的方法 forName(),将返回一个 com.sun.star.reflection.XIdlClass,可以请求其使用 createObject() 创建实例:

 // create the service manager of OpenOffice
 IDispatch* pdispFactory= NULL; 
 CLSID clsFactory= {0x82154420,0x0FBF,0x11d4,{0x83, 0x13,0x00,0x50,0x04,0x52,0x6A,0xB4}}; 
 hr= CoCreateInstance( clsFactory, NULL, CLSCTX_ALL, __uuidof(IDispatch), (void**)&pdispFactory); 
 
 // create the CoreReflection service 
 OLECHAR* funcName= L"createInstance"; 
 DISPID id; 
 pdispFactory->GetIDsOfNames( IID_NULL, &funcName, 1, LOCALE_USER_DEFAULT, &id); 
 
 VARIANT param1; 
 VariantInit( &param1); 
 param1.vt= VT_BSTR; 
 param1.bstrVal= SysAllocString( L"com.sun.star.reflection.CoreReflection"); 
 DISPPARAMS dispparams= { &param1, 0, 1, 0}; 
 VARIANT result; 
 VariantInit( &result); 
 hr= pdispFactory->Invoke( id, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, 
 &dispparams, &result, NULL, 0); 
 IDispatch* pdispCoreReflection= result.pdispVal; 
 pdispCoreReflection->AddRef(); 
 VariantClear( &result); 
 
 // create the struct's idl class object 
 OLECHAR* strforName= L"forName"; 
 hr= pdispCoreReflection->GetIDsOfNames( IID_NULL, &strforName, 1, LOCALE_USER_DEFAULT, &id); 
 VariantClear( &param1); 
 param1.vt= VT_BSTR; 
 param1.bstrVal= SysAllocString(L"com.sun.star.beans.PropertyValue"); 
 hr= pdispCoreReflection->Invoke( id, IID_NULL, LOCALE_USER_DEFAULT, 
 DISPATCH_METHOD, &dispparams, &result, NULL, 0); 
 
 IDispatch* pdispClass= result.pdispVal;
 pdispClass->AddRef();
 VariantClear( &result);
 
 // create the struct
 OLECHAR* strcreateObject= L"createObject";
 hr= pdispClass->GetIDsOfNames( IID_NULL,&strcreateObject, 1, LOCALE_USER_DEFAULT, &id)
 
 IDispatch* pdispPropertyValue= NULL;
 VariantClear( &param1);
 param1.vt= VT_DISPATCH | VT_BYREF;
 param1.ppdispVal= &pdispPropertyValue;
 hr= pdispClass->Invoke( id, IID_NULL, LOCALE_USER_DEFAULT, 
 DISPATCH_METHOD, &dispparams, NULL, NULL, 0);
 
 // do something with the struct pdispPropertyValue contained in dispparams
 // ...
 
 pdispPropertyValue->Release();
 pdispClass->Release();
 pdispCoreReflection->Release();
 pdispFactory->Release();


Bridge_GetStruct 示例。

 // objectServiceManager is the service manager of the office
 OLECHAR* strstructFunc= L"Bridge_GetStruct";
 hr= objServiceManager->GetIDsOfNames( IID_NULL, &strstructFunc, 1, LOCALE_USER_DEFAULT, &id);
 
 VariantClear(&result);
 VariantClear( &param1);
 param1.vt= VT_BSTR;
 param1.bstrVal= SysAllocString(
 L"com.sun.star.beans.PropertyValue");
 hr= objServiceManager->Invoke( id, IID_NULL,LOCALE_USER_DEFAULT, DISPATCH_METHOD, 
 &dispparams, &result, NULL, 0);
 
 IDispatch* pdispPropertyValue= result.pdispVal;
 pdispPropertyValue->AddRef();
 
 // do something with the struct pdispPropertyValue
 ...


JScript:

 // struct creation via CoreReflection
 var objServiceManager= new ActiveXObject("com.sun.star.ServiceManager");
 var objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection");
 
 var classSize= objCoreReflection.forName("com.sun.star.awt.Size");
 var outParam= new Array();
 classSize.createObject( outParam);
 var size= outParam[0];
 //use the struct
 size.Width=111;
 size.Height=112;
 // ----------------------------------------------------
 // struct creation by bridge function
 var objServiceManager= new ActiveXObject("com.sun.star.ServiceManager");
 var size= objServiceManager.Bridge_GetStruct("com.sun.star.awt.Size");
 size.Width=111;
 size.Height=112;


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