English 中文(简体)
How do I reconnect to OpenOffice.org once it has been closed?
原标题:

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]); 
      } 
   } 
}
最佳回答

I was never able to figure this out, but I did find a way to hack around it:

I created a separate executable to do the generating. My main application then calls this executable, passing it the necessary parameters (just a path to the file to generate from, and the mode to generate in).

Since the crash only occurs when OOo has been closed since the application started running (and generated at least one chart), this avoids the whole issue. It s a very ugly hack, but it gets the job done.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签