Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / ProfUNO / CppBinding / office_connect.cxx
blobfab8b8d9d1f4b4215797fa34602249b964e283b3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 #include <stdio.h>
38 #include <sal/main.h>
40 #include <cppuhelper/bootstrap.hxx>
41 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
45 using namespace com::sun::star::uno;
46 using namespace com::sun::star::lang;
47 using namespace com::sun::star::bridge;
48 using namespace cppu;
50 using ::rtl::OString;
51 using ::rtl::OUString;
52 using ::rtl::OUStringToOString;
54 SAL_IMPLEMENT_MAIN()
56 // create the initial component context
57 Reference< XComponentContext > rComponentContext =
58 defaultBootstrap_InitialComponentContext();
60 // retrieve the servicemanager from the context
61 Reference< XMultiComponentFactory > rServiceManager =
62 rComponentContext->getServiceManager();
64 // instantiate a sample service with the servicemanager.
65 Reference< XInterface > rInstance =
66 rServiceManager->createInstanceWithContext(
67 OUString("com.sun.star.bridge.UnoUrlResolver"),
68 rComponentContext );
70 // Query for the XUnoUrlResolver interface
71 Reference< XUnoUrlResolver > rResolver( rInstance, UNO_QUERY );
73 if( ! rResolver.is() )
75 printf( "Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n" );
76 return 1;
78 try
80 // resolve the uno-url
81 rInstance = rResolver->resolve( OUString(
82 "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager" ) );
84 if( ! rInstance.is() )
86 printf( "StarOffice.ServiceManager is not exported from remote counterpart\n" );
87 return 1;
90 // query for the simpler XMultiServiceFactory interface, sufficient for scripting
91 Reference< XMultiServiceFactory > rOfficeServiceManager (rInstance, UNO_QUERY);
93 if( ! rInstance.is() )
95 printf( "XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n" );
96 return 1;
99 printf( "Connected successfully to the office\n" );
101 catch( Exception &e )
103 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
104 printf( "Error: %s\n", o.pData->buffer );
105 return 1;
107 return 0;
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */