C# Build remote connection to LibreOffice


I’m trying to write a program that start LibreOffice with a pipe connection and then connects to it.
But what ever I try I always end up with the exception Unable to cast transparent proxy to type ‘unoidl.com.sun.star.frame.XComponentLoader’.

I’m starting LibreOffice this way:

        var process = new Process
            StartInfo =
                Arguments =
                    $"-invisible -nofirststartwizard -minimized -nologo -nolockcheck -env:UserInstallation=file:///{_userFolder} --accept=pipe,name={_pipeName};urp;StarOffice.ServiceManager",
                FileName = installPath + @"\soffice.exe",
                CreateNoWindow = true

        if (!process.Start())
            throw new InvalidProgramException("Could not start LibreOffice");

        _libreOfficeProcess = process;

And then try to connect to it this way:

            var localContext = Bootstrap.defaultBootstrap_InitialComponentContext();
            var localServiceManager = localContext.getServiceManager();
            var urlResolver = (XUnoUrlResolver) localServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext);
            var remoteContext = (XComponentContext) urlResolver.resolve($"uno:pipe,name={_pipeName};urp;StarOffice.ComponentContext");
            var remoteFactory = (XMultiServiceFactory) remoteContext.getServiceManager();
            var componentLoader = (XComponentLoader) remoteFactory.createInstance("com.sun.star.frame.Desktop"); 
            component = InitDocument(componentLoader, ConvertToUrl(inputFile), "_blank");

But the line var componentLoader = (XComponentLoader) remoteFactory.createInstance(“com.sun.star.frame.Desktop”); then throws the exception.

What am I doing wrong?

Kees van Spelde

You need to set the URE_BOOTSTRAP environment variable before starting the LibreOffice process:

Environment.SetEnvironmentVariable("URE_BOOTSTRAP", $"vnd.sun.star.pathname:{InstallationPath}/fundamental.ini");