Here is a sample application that creates a Window with a single Button inside. When clicked, it connects to OOo (if not already connected) and creates a text document.
This works fine unless all the documents created in OOo are closed. Then, I get a DisposedException when trying to create the next chart. This is understandable, but OOo has been closed. However, trying to reconnect at this point gives me a segfault. Is there a better way to reconnect? I am working on linux (Ubuntu).
Note: This connects properly to OOo even if OOo is not open. It's once OOo has been opened by the application, then closed that we get the error.
All you really need to look at is the Connect method. I just wrapped it in a Gtk interface for easy testing.
using System;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.beans;
using Gtk;
namespace TestOOo {
class MainClass {
static XComponentContext componentContext;
static XMultiServiceFactory multiServiceFactory;
static XComponentLoader loader;
static XTextDocument document;
public static void Main (string[] args)
{
Application.Init();
Window mainWindow = new Window("Test Window");
mainWindow.Visible = true;
mainWindow.Destroyed += delegate { Application.Quit(); };
Button button = new Button(Stock.Ok);
button.Clicked += delegate { Connect(); };
mainWindow.Add(button);
mainWindow.ShowAll();
Application.Run();
}
static void Connect()
{
// Connect to OOo
if (componentContext == null)
componentContext = uno.util.Bootstrap.bootstrap();
try {
multiServiceFactory =
(XMultiServiceFactory) componentContext.getServiceManager();
} catch {
// This is where we want to reconnect, but trying to
// bootstrap() again segfaults.
// componentContext = uno.util.Bootstrap.bootstrap();
// multiServiceFactory =
// (XMultiServiceFactory) componentContext.getServiceManager();
}
loader = (XComponentLoader)
multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
document = (XTextDocument) loader.loadComponentFromURL
("private:factory/swriter", "_blank", 0, new PropertyValue[0]);
}
}
}