Instantiating UNO Services

From Apache OpenOffice Wiki
Jump to: navigation, search

In Basic, instantiate services using the Basic Runtime Library (RTL) function createUnoService(). This function expects a fully qualified service name and returns an object supporting this service, if it is available:

  oSimpleFileAccess = CreateUnoService( "" )

This call instantiates the service. To ensure that the function was successful, the returned object can be checked with the IsNull function:

  oSimpleFileAccess = CreateUnoService( "" )
  bError = IsNull( oSimpleFileAccess )' bError is set to False
  oNoService = CreateUnoService( "" )
  bError = IsNull( oNoService )' bError is set to True

Instead of using CreateUnoService() to instantiate a service, it is also possible to get the global UNO of the process by calling GetProcessServiceManager(). Once obtained, use createInstance() directly:

  oServiceMgr = GetProcessServiceManager()
  oSimpleFileAccess = oServiceMgr.createInstance( "" )
  ' is the same as
  oSimpleFileAccess = CreateUnoService( "" )

The advantage of GetProcessServiceManager() is that additional information and pass in arguments is received when services are instantiated using the service manager. For instance, to initialize a service with arguments, the createInstanceWithArguments() method of has to be used at the service manager, because there is no appropriate Basic RTL function to do that. Example:

  Dim args(1)
  args(0) = "Important information"
  args(1) = "Even more important information"
  oService = oServiceMgr.createInstanceWithArguments _
      ( "", args() )

The object returned by GetProcessServiceManager() is a normal Basic UNO object supporting Its properties and methods are accessed as described above.

Using new-style service constructors

Beginning with 3.2 Basic supports UNO new-style service constructors. For more details see section Services. For the sample service used above constructors could be defined like this:

  module com { module sun { module star { module nowhere {
  service ServiceThatNeedsInitialization: XFoo { 
      create1([in] long arg);
      create2([in] string important1, [in] string important2);
  }; }; }; };

UNO services are mapped to Basic objects. They have to be addressed by using the complete UNO namespace path. The constructors can be accessed as methods of these service objects:

  Dim oServiceObj
  oServiceObj =
  Dim oInstance As Object
  oInstance = oServiceObj.create2( "Useless", "Invalid" )

Of course the service object doesn't need to be stored in a variable before, the instantiation can also be done in one single statement:

  Dim oInstance As Object
  oInstance = 42 )

Internally the UNO default context is used to create the and passed to the service. It's also possible to use an own context instead by adding it as first argument:

  Dim oMyContext As Object
  oMyContext = GetContextFromSomewhere()
  oInstance = oServiceObj.create1( oMyContext, 42 )

If a new-style service only has an implicit constructor it's mapped to a method "create" without parameters in Basic.

Build in properties

The Basic RTL provides special properties as API entry points. They are described in more detail in Features of Basic: Basic RTL Property Description
ThisComponent Only exists in Basic code which is embedded in a Writer, Calc, Draw or Impress document. It contains the document model the Basic code is embedded in.
ThisDatabaseDocument Only exists in Basic code which is embedded in a Base document. It contains the document model the Basic code is embedded in, i.e. the Base document. Useful when a macro is started by an event from a Form document contained in the Base document.
StarDesktop The singleton of the office application. It loads document components and handles the document windows. For instance, the document in the top window can be retrieved using oDoc = StarDesktop.CurrentComponent.
Tip.png ThisComponent is recommended over StarDesktop.CurrentComponent, for convenience in developing and debugging from the IDE. If the BasicIDE is active, StarDesktop.CurrentComponent refers to the BasicIDE itself while ThisComponent always refers to the component that was active before the BasicIDE became the top window.

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