5

With the following code:

static void Main()
{
    try
    {
        var context = uno.util.Bootstrap.bootstrap();
    }
    catch (Exception ex)
    {
       Console.WriteLine(ex.toString());
    }
}

I can start Writer of LibreOffice. This works fine with Version 4.4.4 but after installing version 5.0.0 and with new SDK Bootstrap.bootstrap() throws the exception:

"External component has thrown an exception"

Has anyone faced the same problem or some solution? (.NET 4.0, Windows 7 64-bit, LibreOffice 5.0 Lite)

Christian Landgren
  • 13,127
  • 6
  • 35
  • 31
Entwickler
  • 255
  • 2
  • 12
  • Is there any inner exception on the exception thrown that might contain more information, like *which* external component originated it? – Chris Mantle Aug 06 '15 at 12:44
  • Following is the exception A first chance exception of type 'System.Runtime.InteropServices.SEHException' occurred in cli_cppuhelper.dll cppu.bootstrap(Reference* – Entwickler Aug 06 '15 at 14:12
  • This issue is referenced on [bug tracker](https://bugs.documentfoundation.org/show_bug.cgi?id=94265). It confirms that adding `C:\Program Files\LibreOffice\program` to environment variable "PATH" solves the issue for most people. – Cyril Waechter Apr 07 '18 at 00:48

3 Answers3

8

I have managed to solve the problem by setting the UNO_PATH environment variable before starting the soffice.exe server:

using static System.Environment;

var unoPath = @"C:\Program Files\LibreOffice 5\program"
// when running 32-bit LibreOffice on a 64-bit system, the path will be in Program Files (x86)
// var unoPath = @"C:\Program Files (x86)\LibreOffice 5\program"

SetEnvironmentVariable("UNO_PATH", unoPath, EnvironmentVariableTarget.Process);
SetEnvironmentVariable("PATH", GetEnvironmentVariable("PATH") + @";" + unoPath, EnvironmentVariableTarget.Process);

This was required because LibreOffice 5's program directory does not have "URE" subdirectory anymore (previous versions did) which is required for UNO layer.

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
Funbit
  • 1,591
  • 2
  • 14
  • 15
  • Thanks, but this solution not always work. I have tested under different Windows versions and on some it work and on some not. – Entwickler Sep 23 '15 at 09:20
  • 5
    Thanks for the notice, I'm using it on Windows Server 2012 R2, works perfectly. Here is the code: Environment.SetEnvironmentVariable("UNO_PATH", unoPath, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + @";" + unoPath, EnvironmentVariableTarget.Process); – Funbit Sep 24 '15 at 10:23
  • 1
    Thanks it worked perfectly, but only if I have libreoffice 32 bit, with libreoffice 64 bit I still got errors, any suggestion? – Alkampfer Oct 20 '15 at 10:06
  • 1
    @Alkampfer Have you tried to use "Program Files" instead of "Program Files (x86)" for x64 version? – Funbit Oct 21 '15 at 11:04
5

To get the path to the LibreOffice installation you can ask e.g. the Windows registry. In C# this is smoething like that:

    String unoPath = "";
    // access 32bit registry entry for latest LibreOffice for Current User
    Microsoft.Win32.RegistryKey hkcuView32 = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, Microsoft.Win32.RegistryView.Registry32);
    Microsoft.Win32.RegistryKey hkcuUnoInstallPathKey = hkcuView32.OpenSubKey(@"SOFTWARE\LibreOffice\UNO\InstallPath", false);
    if (hkcuUnoInstallPathKey != null && hkcuUnoInstallPathKey.ValueCount > 0)
    {
        unoPath = (string)hkcuUnoInstallPathKey.GetValue(hkcuUnoInstallPathKey.GetValueNames()[hkcuUnoInstallPathKey.ValueCount - 1]);
    }
    else
    {
        // access 32bit registry entry for latest LibreOffice for Local Machine (All Users)
        Microsoft.Win32.RegistryKey hklmView32 = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry32);
        Microsoft.Win32.RegistryKey hklmUnoInstallPathKey = hklmView32.OpenSubKey(@"SOFTWARE\LibreOffice\UNO\InstallPath", false);
        if (hklmUnoInstallPathKey != null && hklmUnoInstallPathKey.ValueCount > 0)
        {
            unoPath = (string)hklmUnoInstallPathKey.GetValue(hklmUnoInstallPathKey.GetValueNames()[hklmUnoInstallPathKey.ValueCount - 1]);
        }
    }

Then you can use the answer of Funbit [ https://stackoverflow.com/a/31937114/2936206 ]

Community
  • 1
  • 1
Jens Bornschein
  • 163
  • 2
  • 11
0

The most easiest way I found is just copy the URE folder from previous LibreOffice version to LibreOffice 5.

Entwickler
  • 255
  • 2
  • 12