Unable to Connect to OpenOffice with C++ in Windows 7

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
ACF79
Posts: 2
Joined: Wed Jun 29, 2016 1:27 pm

Unable to Connect to OpenOffice with C++ in Windows 7

Post by ACF79 »

Hello

I am required to append code to a large scale C++ Windows application that mail merges the contents of an OpenOffice (4.1) Document or an MS Word Document.

I started off by downloading and installing the SDK using the following guide.

http://wiki.services.openoffice.org/wik ... stallation

Next I started to write a test application just to create a new document

The problem I'm having is that I can't "connect" to OpenOffice. It seems that a lot of code example's out there are tied to connecting to Open Office in Unix/Linux

Below is my connection code (copied from the example).

Code: Select all

	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
	{
		fprintf(stderr, "WSAStartup failed.\n");
		exit(1);
	}

	OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("c:\Program Files (x86)\OpenOffice 4\program\ soffice -accept=socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"));
	OUString sUnoIni(RTL_CONSTASCII_USTRINGPARAM("file:///C:/Program Files (x86)/OpenOffice 4/program/uno.ini"));
	//OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("C:\\Program Files (x86)\\OpenOffice 4\\program\\soffice.exe -accept=socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"));
	Reference<XComponentContext> xComponentContext(::cppu::defaultBootstrap_InitialComponentContext(sUnoIni));
	
    /* Gets the service manager instance to be used (or null). This method has
       been added for convenience, because the service manager is a often used
       object.
    */
	Reference<XMultiComponentFactory> xMultiComponentFactoryClient(xComponentContext->getServiceManager());

    /* Creates an instance of a component which supports the services specified
       by the factory.
    */
	Reference<XInterface> xInterface = xMultiComponentFactoryClient->createInstanceWithContext(OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver" ),  xComponentContext);
    Reference<XUnoUrlResolver> resolver(xInterface, UNO_QUERY);

    // Resolves the component context from the office, on the uno URL given by argv[1].
    try
    {    
        xInterface = Reference<XInterface>(resolver->resolve(sConnectionString));		
    }
    catch (Exception& e)
    {
		printf("Error: cannot establish a connection using '%s':\n       %s\n",
               OUStringToOString(sConnectionString, RTL_TEXTENCODING_ASCII_US).getStr(),
               OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
		exit(1);        
    }

    // gets the server component context as property of the office component factory
    Reference<XPropertySet> xPropSet(xInterface, UNO_QUERY);
    xPropSet->getPropertyValue(OUString::createFromAscii("DefaultContext")) >>= xComponentContext;

    // gets the service manager from the office
    Reference<XMultiComponentFactory> xMultiComponentFactoryServer(xComponentContext->getServiceManager());

    /* Creates an instance of a component which supports the services specified
       by the factory. Important: using the office component context.
    */
    Reference<XComponentLoader> xComponentLoader(xMultiComponentFactoryServer->createInstanceWithContext(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" )),
            xComponentContext ), UNO_QUERY );
	
    /* Loads a component specified by an URL into the specified new or existing
       frame.
    */    
    //Reference<XComponent> xComponent = xComponentLoader->loadComponentFromURL(sAbsoluteDocUrl, OUString( RTL_CONSTASCII_USTRINGPARAM("_blank") ), 0, Sequence<::com::sun::star::beans::PropertyValue>());
	Reference<XComponent> xComponent = xComponentLoader->loadComponentFromURL(OUString(RTL_CONSTASCII_USTRINGPARAM("private:factory/swriter")), OUString( RTL_CONSTASCII_USTRINGPARAM("_blank") ), 0, Sequence<::com::sun::star::beans::PropertyValue>());

	// dispose the local service manager
    Reference<XComponent>::query(xMultiComponentFactoryClient)->dispose();

	// TODO
	WSACleanup();

	return TRUE;
There was a link on a previous topic that indicated a solution but the URL for the solution is no longer available

Is anyone able to provide assistance in how you actually code the service to create/open a document in Windows, let alone Mail Merge. I have no experience in using Open Office & the requirement to get open office to work is an Extremely Very High Priority

Regards
Adam
Simplicity is the Ultimate Sophistication

OpenOffice 4.1.2 on Windows 7
lalithr312
Posts: 56
Joined: Mon Jun 22, 2015 12:07 am

Re: Unable to Connect to OpenOffice with C++ in Windows 7

Post by lalithr312 »

I'm assuming you're getting an error when you execute this line:

Code: Select all

 xInterface = Reference<XInterface>(resolver->resolve(sConnectionString));
Since this is in a try/catch block, what sort of exception are you getting?
OpenOffice 4.1.1 on Windows 10
ACF79
Posts: 2
Joined: Wed Jun 29, 2016 1:27 pm

Re: Unable to Connect to OpenOffice with C++ in Windows 7

Post by ACF79 »

There are 2 particular error's that I receive, the first one without the uno based connection string is
UNO URL does not start with "uno:"
Then if I change the connection string to a uno based one such as "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager", I then get the following error
Connector : couldn't connect to socket (WSAECONNREFUSED, Connection refused)
Do I need to modfiy the code so that I am querying the service manager without addrssing UNO?
Simplicity is the Ultimate Sophistication

OpenOffice 4.1.2 on Windows 7
Post Reply