segmentation fault in C++ example

asked 2018-07-17 20:25:02 +0100

olk gravatar image

Hi, I get an segmentation fault from XComponentLoader::loadComponentFromURL() - could you tell me how to fix it, please? I'm using libreoffice SDK 6.0.5 on Arch Linux.

#include <stdio.h>

#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>

#include <cppuhelper/bootstrap.hxx>

#include <osl/file.hxx>
#include <osl/process.h>
#include <sal/config.h>
#include <sal/main.h>

#include <bootstrap.hxx>

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

// a procedure for what the so called boostrap
Reference< XMultiServiceFactory > ooConnect(){
   // create the initial component context
   Reference< XComponentContext > rComponentContext = defaultBootstrap_InitialComponentContext();

   // retrieve the servicemanager from the context
   Reference< XMultiComponentFactory > rServiceManager = rComponentContext->getServiceManager();

   // instantiate a sample service with the servicemanager.
   Reference< XInterface > rInstance =  rServiceManager->createInstanceWithContext(
         OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver"), rComponentContext);

   // Query for the XUnoUrlResolver interface
   Reference< XUnoUrlResolver > rResolver{ rInstance, UNO_QUERY };
   if ( ! rResolver.is() ) {
      printf( "Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n" );
      return nullptr;
   }
   try {
      // resolve the uno-url
      rInstance = rResolver->resolve( OUString::createFromAscii(
         "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager") );

      if ( ! rInstance.is() ) {
         printf("StarOffice.ServiceManager is not exported from remote counterpart\n");
         return nullptr;
      }

      // query for the simpler XMultiServiceFactory interface, sufficient for scripting
      Reference< XMultiServiceFactory > rOfficeServiceManager{ rInstance, UNO_QUERY };

      if ( ! rOfficeServiceManager.is() ) {
            printf("XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n");
            return nullptr;
        }
        return rOfficeServiceManager;
   }
   catch ( Exception const& e) {
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US);
      printf("Error: %s\n", o.pData->buffer);
      return nullptr;
   }
   return nullptr;
}

//Listing 0b Again our starting main Code
int main() {
    OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl;
    osl_getProcessWorkingDir(&sWorkingDir.pData);
    osl::FileBase::getFileURLFromSystemPath(
            OUString::createFromAscii("./offapi.rdb"),
            sDocPathUrl);
    osl::FileBase::getAbsoluteFileURL( sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);
    rtl::Bootstrap::set(
            OUString::createFromAscii("URE_MORE_TYPES"),
            rtl::Bootstrap::encode(sAbsoluteDocUrl)
            );

    //retrieve an instance of the remote service manager
    Reference< XMultiServiceFactory > rOfficeServiceManager = ooConnect();
    if ( ! rOfficeServiceManager.is()  ){
        printf("Not sucessfully connected to the office\n");
        return 1;
    }

//get the desktop service using createInstance returns an XInterface type
    Reference< XInterface  > Desktop = rOfficeServiceManager->createInstance(
        OUString::createFromAscii("com.sun.star.frame.Desktop") );

//query for the XComponentLoader interface
    Reference< XComponentLoader > rComponentLoader{ Desktop, UNO_QUERY };
    if ( ! rComponentLoader.is() ) {
            printf("XComponentloader not successfully instanciated\n");
            return 1;
    }

//get an instance of the spreadsheet
    Reference< XComponent > xcomponent = rComponentLoader->loadComponentFromURL(
            OUString::createFromAscii("private:factory/scalc"),
            OUString::createFromAscii("_blank"),
            0,
            Sequence< ::com::sun::star::beans::PropertyValue >{} );
    // add code here
    return 0;
}
edit retag flag offensive close merge delete

Comments

Perhaps rComponentLoader was not initialized correctly. What documentation or examples did you look at to write this code? The example code from DocumentLoader.cxx in the SDK shows Reference < XDesktop2 > xComponentLoader = Desktop::create(xComponentContext);.

Jim K gravatar imageJim K ( 2018-07-18 16:02:32 +0100 )edit

Also if LibreOffice is 32-bit then use the 32-bit SDK as well, with versions that match.

Jim K gravatar imageJim K ( 2018-07-18 16:05:42 +0100 )edit