Difficulty to adapt SDK Java sample into C# (cast exception)

Java, C++, C#, Delphi, ??? - Using the UNO bridges

Difficulty to adapt SDK Java sample into C# (cast exception)

Postby Burzum » Thu Dec 29, 2016 6:47 pm

Hi,

I have built a C# application that performs mail merges with MS Office (using Interop API). I am now trying to have it support Open office. I want to use OpenOffice SDK:

Does not look crystal clear to me right now....

I somehow managed to get the mail merge code to work.
But I have difficulties to create the "DataSource", which is necesary to generate prior we can actually perform the MailMerge.

I read this Java sample and tried to adapt it into C#: https://wiki.openoffice.org/wiki/Docume ... ce_Service

My difficulty is that Java uses this object to perform its casts:
Code: Select all   Expand viewCollapse view
XStorable store = ( XStorable)UnoRuntime.queryInterface(XStorable.class, xDs);

There is nothing equivalent in C#.

I converted the Java code this way:
Code: Select all   Expand viewCollapse view
  public static void CreateDataSource(string dataSourceProvidedFilePath, string dataSourceSavedFilePath)
    {
                XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
        XMultiServiceFactory _rMSF = (XMultiServiceFactory)oStrap.getServiceManager();

         // the XSingleServiceFactory of the database context creates new generic
      // com.sun.star.sdb.DataSources (!)
      // retrieve the database context at the global service manager and get its
      // XSingleServiceFactory interface
      XSingleServiceFactory xFac = (XSingleServiceFactory) _rMSF.createInstance("com.sun.star.sdb.DatabaseContext");
          //(XSingleServiceFactory)UnoRuntime.queryInterface(XSingleServiceFactory.class, _rMSF.createInstance("com.sun.star.sdb.DatabaseContext"));

      // instantiate an empty data source at the XSingleServiceFactory
      // interface of the DatabaseContext
      Object xDs = xFac.createInstance();

      // register it with the database context
      XNamingService xServ = (XNamingService)xFac;
          //(XNamingService)UnoRuntime.queryInterface(XNamingService.class, xFac);

      XStorable store = ( XStorable) xDs;
          //( XStorable)UnoRuntime.queryInterface(XStorable.class, xDs);

      XModel model =( XModel) xDs;
          //( XModel)UnoRuntime.queryInterface(XModel.class, xDs);

        //on détermine le fichier ou sera sauvegardée la data source
      string dataSourcePathURL = Path.Combine(Path.GetDirectoryName(dataSourceProvidedFilePath), dataSourceSavedFilePath + ".odb").ConvertToOpenOfficeURL();
      store.storeAsURL(/*"file:///c:/test.odb"*/dataSourcePathURL,model.getArgs());
      xServ.registerObject("NewDataSourceName", xDs);

      // setting the necessary data source properties
      XPropertySet xDsProps = (XPropertySet)xDs;
          //(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xDs);
      // Adabas D URL
      xDsProps.setPropertyValue("URL", new uno.Any("sdbc:adabas::MYDB1"));

      // force password dialog
      //xDsProps.setPropertyValue("IsPasswordRequired", new Boolean(true));

      // suggest dsadmin as user name
      xDsProps.setPropertyValue("User", new uno.Any("dsadmin"));
      store.store();
    }

Some casts worked fine:
Code: Select all   Expand viewCollapse view
XNamingService xServ = (XNamingService)xFac;
          //(XNamingService)UnoRuntime.queryInterface(XNamingService.class, xFac);

But some other casts throw an exception:

Code: Select all   Expand viewCollapse view
XStorable store = ( XStorable) xDs; //( XStorable)UnoRuntime.queryInterface(XStorable.class, xDs);

->

Code: Select all   Expand viewCollapse view
Unable to cast transparent proxy to type 'unoidl.com.sun.star.frame.XStorable'.

Is there a way to have this code correctly converted to C#?

Otherwise, do you know any other resource showing how to create an Open Office DataSource in Java?

Thx
Windows 7 / Open Office 4.1.1
Burzum
 
Posts: 1
Joined: Thu Dec 29, 2016 6:39 pm

Return to External Programs

Who is online

Users browsing this forum: No registered users and 5 guests