Getting Information about UNO Objects

From Apache OpenOffice Wiki
< Documentation‎ | DevGuide
Revision as of 13:28, 23 December 2020 by DiGro (Talk | contribs)

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

The Basic RTL retrieves information about UNO objects. There are functions to evaluate objects during runtime and object properties used to inspect objects during debugging.

Checking for interfaces during runtime

Although Basic does not support the queryInterface concept like C++ and Java, it can be useful to know if a certain interface is supported by a UNO Basic object or not. The function HasUnoInterfaces() detects this.

The first parameter HasUnoInterfaces() expects is the object that should be tested. Parameter(s) of one or more fully qualified interface names can be passed to the function next. The function returns True if all these interfaces are supported by the object, otherwise False.

  Sub Main
    Dim oSimpleFileAccess
    oSimpleFileAccess = CreateUnoService( "" )
  Dim bSuccess
      Dim IfaceName1$, IfaceName2$, IfaceName3$
      IfaceName1$ = ""
      IfaceName2$ = ""
      IfaceName3$ = ""
      bSuccess = HasUnoInterfaces( oSimpleFileAccess, IfaceName1$ )
      MsgBox bSuccess ' Displays True because XInterface is supported
      bSuccess = HasUnoInterfaces( oSimpleFileAccess, IfaceName1$, IfaceName2$ )
      MsgBox bSuccess ' Displays True because XInterface
      ' and XSimpleFileAccess2 are supported
      bSuccess = HasUnoInterfaces( oSimpleFileAccess, IfaceName3$ )
      MsgBox bSuccess ' Displays False because XPropertySet is NOT supported
      bSuccess = HasUnoInterfaces( oSimpleFileAccess, IfaceName1$, IfaceName2$, IfaceName3$ )
      MsgBox bSuccess ' Displays False because XPropertySet is NOT supported
  End Sub

Testing if an object is a struct during runtime

As described in the section Mapping of Structs above, structs are handled differently from objects, because they are treated as values. Use the IsUnoStruct() function to check it the UNO Basic object represents an object or a struct. This function expects one parameter and returns True if this parameter is a UNO struct, otherwise False. Example:

  Sub Main
      Dim bIsStruct
          ' Instantiate a service
      Dim oSimpleFileAccess
      oSimpleFileAccess = CreateUnoService( "" )
      bIsStruct = IsUnoStruct( oSimpleFileAccess )
      MsgBox bIsStruct ' Displays False because oSimpleFileAccess is NO struct
                       ' Instantiate a Property struct
      Dim aProperty As New
      bIsStruct = IsUnoStruct( aProperty )
      MsgBox bIsStruct ' Displays True because aProperty is a struct
      bIsStruct = IsUnoStruct( 42 )
      MsgBox bIsStruct ' Displays False because 42 is NO struct
  End Sub

Testing objects for identity during runtime

To find out if two UNO Apache OpenOffice Basic objects refer to the same UNO object instance, use the function EqualUnoObjects(). Basic is not able to apply the comparison operator = to arguments of type object, for example, If Obj1 = Obj2 Then will produce a runtime error.

  Sub Main
      Dim bIdentical
      Dim oSimpleFileAccess, oSimpleFileAccess2, oSimpleFileAccess3
          ' Instantiate a service
      oSimpleFileAccess = CreateUnoService( "" )
      oSimpleFileAccess2 = oSimpleFileAccess ' Copy the object reference
      bIdentical = EqualUnoObjects( oSimpleFileAccess, oSimpleFileAccess2 )
      MsgBox bIdentical ' Displays True because the objects are identical
          ' Instantiate the service a second time
      oSimpleFileAccess3 = CreateUnoService( "" )
      bIdentical = EqualUnoObjects( oSimpleFileAccess, oSimpleFileAccess3 )
      MsgBox bIdentical ' Displays False, oSimpleFileAccess3 is another instance
      bIdentical = EqualUnoObjects( oSimpleFileAccess, 42 )
      MsgBox bIdentical ' Displays False, 42 is not even an object
          ' Instantiate a Property struct
      Dim aProperty As New
      Dim aProperty2
      aProperty2 = aProperty ' Copy the struct
      bIdentical = EqualUnoObjects( aProperty, aProperty2 )
      MsgBox bIdentical ' Displays False because structs are values
          ' and so aProperty2 is a copy of aProperty
  End Sub

Basic hides interfaces behind Apache OpenOffice Basic objects that could lead to problems when developers are using API structures. It can be difficult to understand the API reference and find the correct method of accessing an object to reach a certain goal.

To assist during development and debugging, every UNO object in Apache OpenOffice Basic has special properties that provide information about the object structure. These properties are all prefixed with Dbg_ to emphasize their use for development and debugging purposes. The type of these properties is String. To display the properties use the MsgBox function.

Inspecting interfaces during debugging

The Dbg_SupportedInterfaces lists all interfaces supported by the object. In the following example, the object returned by the function GetProcessServiceManager() described in the previous section is taken as an example object.

  oServiceManager = GetProcessServiceManager()
  MsgBox oServiceManager.Dbg_SupportedInterfaces

This call displays a message box:

Dbg_SupportedInterfaces Property

The list contains all interfaces supported by the object. For interfaces that are derived from other interfaces, the super interfaces are indented as shown above for, which is derived from based upon

Documentation note.png If the text "(ERROR: Not really supported!)" is printed behind an interface name, the implementation of the object usually has a bug, because the object pretends to support this interface (per, but a query for it fails. For details, see UNO Reflection API).

Inspecting properties during debugging

The Dbg_Properties lists all properties supported by the object through and through get and set methods that could be mapped to Basic object properties:

  oServiceManager = GetProcessServiceManager()
  MsgBox oServiceManager.Dbg_Properties

This code produces a message box like the following example:


Inspecting Methods During Debugging

The Dbg_Methods lists all methods supported by an object. Example:

  oServiceManager = GetProcessServiceManager()
  MsgBox oServiceManager.Dbg_Methods

This code displays:


The notations used in Dbg_Properties and Dbg_Methods refer to internal implementation type names in Basic. The Sbx prefix can be ignored. The remaining names correspond with the normal Basic type notation. The SbxEMPTY is the same type as Variant. Additional information about Basic types is available in the next chapter.

Documentation note.png Basic uses the interface to detect which interfaces an object supports. Therefore, it is important to support this interface when implementing a component that should be accessible from Basic. For details, see Writing UNO Components.

Inspector tools

Using these Dbg_ properties is a very crude method to discover the contents of an API objects. Use instead Xray tool or MRI tool.

The list of elements may be inconveniently long for MsgBox display. Code is available to write all Dbg_ elements to a new Writer document. See Apache OpenOffice Macros > Tools > Debug > WritedbgInfo. The Tools library must be loaded to use this routine:

GlobalScope.BasicLibraries.LoadLibrary( "Tools" )
Call Tools.WritedbgInfo(MyObject)
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages