2

I seek help with the "generic" part of our C++ spreadsheet server app which connects to a headless Open Office 4.1.2 service.

The "generic" connection part of this application is represented in the code fragment below. We do need the XDesktop and XComponentLoader features to work but they are giving us problems, even when run standalone.

Are we using this features correctly? How do we get them to work, or what is a better alternative?

Code (OpenOffice example.cxx):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/uno/XInterface.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/document/XTypeDetection.hpp>

using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::bridge;
using namespace com::sun::star::beans;
using namespace com::sun::star::frame;
using namespace com::sun::star::container;
using namespace com::sun::star::document;
using namespace rtl;
using namespace cppu;

#define DIAG(s) printf("%s\n",s)
#define OOCHECKREF(x) if (!x.is()){printf("Error: "#x"\n");return 1;}

int main (int argc, char *argv[]) {
   try {
     DIAG("mComponentContext");
     Reference< XComponentContext > mComponentContext
         = defaultBootstrap_InitialComponentContext();

     DIAG("mServiceManager");
     Reference< XMultiComponentFactory > mServiceManager
         = mComponentContext->getServiceManager();

     DIAG("rResolverInstance");
     Reference< XInterface > rResolverInstance
         = mServiceManager->createInstanceWithContext (
             OUString::createFromAscii(
                 "com.sun.star.bridge.UnoUrlResolver") ,
     mComponentContext);

     DIAG("rResolver");
     Reference< XUnoUrlResolver > rResolver (rResolverInstance,
                                             UNO_QUERY);
     OOCHECKREF(rResolver)

     DIAG("rOfficeInstance");    
     Reference< XInterface > rOfficeInstance
         = rResolver->resolve (
             OUString::createFromAscii
                ("uno:socket,host=localhost,port=8199;    
                       urp;StarOffice.ServiceManager")) ;
     OOCHECKREF(rOfficeInstance)

     DIAG("rOfficeServiceManager");
     Reference< XMultiServiceFactory > rOfficeServiceManager (
         rOfficeInstance, UNO_QUERY);
     OOCHECKREF(rOfficeServiceManager)

     DIAG("xPropSet");
     Reference< XPropertySet > xPropSet (rOfficeInstance, UNO_QUERY) ;
     xPropSet->getPropertyValue (
          OUString::createFromAscii("DefaultContext")) 
             >>= mComponentContext;

     DIAG("xMultiComponentFactoryServer");
     Reference< XMultiComponentFactory > xMultiComponentFactoryServer(
         mComponentContext->getServiceManager());

     DIAG("rDesktop");
     Reference<XInterface> rDesktop 
         = rOfficeServiceManager->createInstance(
             OUString::createFromAscii("com.sun.star.frame.Desktop"));
     OOCHECKREF(rDesktop)

     DIAG("mComponentLoader");
     Reference< XComponentLoader > mComponentLoader (rDesktop,  
                                                     UNO_QUERY);
     OOCHECKREF(mComponentLoader)

     DIAG("rFiltersInt");
     Reference< XInterface > rFiltersInt 
         = rOfficeServiceManager->createInstance(
             OUString::createFromAscii(
                 "com.sun.star.document.FilterFactory"));
     OOCHECKREF(rFiltersInt)

     DIAG("mFilterFactory");
     Reference<XNameAccess> mFilterFactory
         = Reference< XNameAccess (rFiltersInt, UNO_QUERY);
     OOCHECKREF(mFilterFactory)

     DIAG("rTypeDetectInt");
     Reference< XInterface > rTypeDetectInt 
         = rOfficeServiceManager->createInstance(
            OUString::createFromAscii(
                "com.sun.star.document.TypeDetection"));
     OOCHECKREF(rTypeDetectInt)

     DIAG("mTypeDetect");
     Reference< XTypeDetection > mTypeDetect 
         = Reference< XTypeDetection  (rTypeDetectInt, UNO_QUERY);
     OOCHECKREF(mTypeDetect)

     DIAG ("Connected successfully to the office - press enter to exit");
     getchar();
   } catch (Exception &e) {
     OString o = OUStringToOString (e.Message,   
                                    RTL_TEXTENCODING_ASCII_US);
     printf ("Error: %s\n", o.pData->buffer);
     return 1;
  }
  return 0;
}

What were the problems?

The code below compiles, runs and connects but seg-faults after the final key-press (after the getchar()):

gdb ../../../LINUXexample.out/bin/ooexample
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ../../../LINUXexample.out/bin/ooexample...(no debugging symbols found)...done.
(gdb) r
Starting program: /opt/libreoffice5.2/sdk/LINUXexample.out/bin/ooexample
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
mComponentContext
[New Thread 0x7ffff5988700 (LWP 12125)]
mServiceManager
rResolverInstance
rResolver
rOfficeInstance
[New Thread 0x7ffff4015700 (LWP 12128)]
[New Thread 0x7ffff3814700 (LWP 12129)]
rOfficeServiceManager
xPropSet
xMultiComponentFactoryServer
rDesktop
mComponentLoader
rFiltersInt
mFilterFactory
rTypeDetectInt
mTypeDetect
Connected successfully to the office - press enter to exit

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff489b723 in cpp_vtable_call ()
   from /opt/libreoffice5.2/program/libgcc3_uno.so
(gdb) bt
#0  0x00007ffff489b723 in cpp_vtable_call ()
   from /opt/libreoffice5.2/program/libgcc3_uno.so
#1  0x00007ffff48a26ea in privateSnippetExecutor ()
   from /opt/libreoffice5.2/program/libgcc3_uno.so
#2  0x0000000000401ea9 in main ()
(gdb) c
Continuing.
[Thread 0x7ffff7fc8780 (LWP 11222) exited]
[Thread 0x7ffff4015700 (LWP 11229) exited]
[Thread 0x7ffff5988700 (LWP 11226) exited]

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.

Why XDesktop and XComponentLoader in particular?

We find that calls on spreadsheet APIs don't seem to work without prior invocation of these features, as in the code sample. The same code embedded in the full application, as well as a somewhat similar example, both fail on the XComponentLoader call (backtrace terminating on that line). We therefore suspect an issue with XComponentLoader.

We have tried this with Libreoffice 4 SDK (as per ubuntu trusty repo), the Libreoffice 5.2 SDK and Openoffice 4.1.2 SDK, with identical results.

What examples did we follow?

First we started in https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/C%2B%2B/Establishing_Interprocess_Communications, then adding the XDesktop and XComponentLoader references using a previous incarnation of our application code. (This previous incarnation failed at the bootstrap stage which used the phased out cppu::createSimpleRegistry call that now leads to program abortion, see https://docs.libreoffice.org/cppuhelper/html/compat_8cxx_source.html, lines 80+). As remarked, running the above code in the full application seg-faults at the XComponentLoader stage.

The "similar" example referred to is from www.linuxjournals.com/article/8608#. Execution seg-faults at the xComponentLoader->loadComponentFromURL call (backtrace terminating on that line; the file and directory parameters passed into that method look good on inspection; flag is 0, property sequence is void).

  • "they are giving us problems" -- What sort of problems? Please include the error message and describe when it occurred. Also, it would help to include a link to the documentation or example that you attempted to follow. Perhaps wiki.openoffice.org/wiki/UNO_registery_and_Bootstrapping? – Jim K Sep 21 '16 at 5:33
  • Thanks for your prompt response. I have tried to clarify the question. – hamish Sep 21 '16 at 10:42
  • It looks like you need to close the connection properly, as shown in listing 7 at linuxjournal.com/article/8608. – Jim K Sep 21 '16 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.