C# 引导Uno API LibreOffice异常

C# 引导Uno API LibreOffice异常,c#,exception,uno,C#,Exception,Uno,使用以下代码: static void Main() { try { var context = uno.util.Bootstrap.bootstrap(); } catch (Exception ex) { Console.WriteLine(ex.toString()); } } 我可以开始写图书馆。这在版本4.4.4中可以正常工作,但在安装版本5.0.0和新SDK后Bootstrap.Bootstrap()会

使用以下代码:

static void Main()
{
    try
    {
        var context = uno.util.Bootstrap.bootstrap();
    }
    catch (Exception ex)
    {
       Console.WriteLine(ex.toString());
    }
}
我可以开始写图书馆。这在版本4.4.4中可以正常工作,但在安装版本5.0.0和新SDK后
Bootstrap.Bootstrap()
会引发异常:

“外部组件引发了异常”

有没有人面临过同样的问题或解决方案?
(.NET 4.0、Windows 7 64位、LibreOffice 5.0 Lite)

在启动
soffice.exe
服务器之前,我已通过设置
UNO_PATH
环境变量来解决此问题:

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);

这是必需的,因为libreoffice5的程序目录不再有“URE”子目录(以前的版本有)这是UNO层所必需的。

我找到的最简单的方法就是将URE文件夹从以前的LibreOffice版本复制到LibreOffice 5。

要获取LibreOffice安装的路径,您可以询问,例如Windows注册表。在C#中,这是一个类似于斯摩特的故事:

    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]);
        }
    }

然后,您可以使用Funbit[]

的答案:抛出的异常上是否存在任何内部异常,可能包含更多信息,例如是哪个外部组件引发的异常?以下是在cli_cppuhelper.dll cppu.bootstrap中发生的“System.Runtime.InteropServices.SEHException”类型的第一次意外异常(参考*此问题在上被引用。它确认将
C:\Program Files\LibreOffice\Program
添加到环境变量“PATH”为大多数人解决了这个问题。谢谢,但这个解决方案并不总是有效。我在不同的Windows版本下进行了测试,在一些it上有效,在一些非有效。感谢通知,我在Windows Server 2012 R2上使用它,效果非常好。下面是代码:Environment.SetEnvironmentVariable(“UNO_PATH”,unoPath,EnvironmentVariableTarget.Process);Environment.SetEnvironmentVariable(“PATH”,Environment.GetEnvironmentVariable(“PATH”)+@“+unoPath,EnvironmentVariableTarget.Process);谢谢它工作得很好,但只有当我有libreoffice 32位,而libreoffice 64位时,我仍然会出错,有什么建议吗?@Alkampfer你试过使用“程序文件”而不是“程序文件”吗(x86)“适用于x64版本?