xref: /AOO41X/main/odk/examples/DevelopersGuide/ProfUNO/CppBinding/office_connect.cxx (revision 34dd1e2512dbacb6a9a7e4c7f17b9296daa8eff3)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <stdio.h>
25 
26 #include <sal/main.h>
27 
28 #include <cppuhelper/bootstrap.hxx>
29 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
32 
33 using namespace com::sun::star::uno;
34 using namespace com::sun::star::lang;
35 using namespace com::sun::star::bridge;
36 using namespace rtl;
37 using namespace cppu;
38 
SAL_IMPLEMENT_MAIN()39 SAL_IMPLEMENT_MAIN()
40 {
41     // create the initial component context
42     Reference< XComponentContext > rComponentContext =
43         defaultBootstrap_InitialComponentContext();
44 
45     // retrieve the servicemanager from the context
46     Reference< XMultiComponentFactory > rServiceManager =
47         rComponentContext->getServiceManager();
48 
49     // instantiate a sample service with the servicemanager.
50     Reference< XInterface > rInstance =
51         rServiceManager->createInstanceWithContext(
52             OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver" ),
53             rComponentContext );
54 
55     // Query for the XUnoUrlResolver interface
56     Reference< XUnoUrlResolver > rResolver( rInstance, UNO_QUERY );
57 
58     if( ! rResolver.is() )
59     {
60         printf( "Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n" );
61         return 1;
62     }
63     try
64     {
65         // resolve the uno-url
66         rInstance = rResolver->resolve( OUString::createFromAscii(
67             "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager" ) );
68 
69         if( ! rInstance.is() )
70         {
71             printf( "StarOffice.ServiceManager is not exported from remote counterpart\n" );
72             return 1;
73         }
74 
75         // query for the simpler XMultiServiceFactory interface, sufficient for scripting
76         Reference< XMultiServiceFactory > rOfficeServiceManager (rInstance, UNO_QUERY);
77 
78         if( ! rInstance.is() )
79         {
80             printf( "XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n" );
81             return 1;
82         }
83 
84         printf( "Connected sucessfully to the office\n" );
85     }
86     catch( Exception &e )
87     {
88         OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
89         printf( "Error: %s\n", o.pData->buffer );
90         return 1;
91     }
92     return 0;
93 }
94