Tutorial UNO Client

From Apache OpenOffice Wiki
Jump to: navigation, search
You use UNO ? Tutorial_Help

Now that we have our component built and ready, we are ready to invoke it from elsewhere, and we do exactly that from the same earlier location:

--- svx/source/dialog/charmap.cxx       2004-07-13 15:15:11.000000000 +0530
+++ svx/source/dialog/charmap.cxx       2005-07-20 13:57:39.557992653 +0530
@@ -117,6 +119,9 @@
 #include <comphelper/types.hxx>
+#include <comphelper/processfactory.hxx>
 #include "rtl/ustrbuf.hxx"

@@ -1097,6 +1102,15 @@ void SvxCharMapData::SetCharFont( const 
 IMPL_LINK( SvxCharMapData, OKHdl, OKButton *, EMPTYARG )
+    Reference< XExecutableDialog > xHelloWorld( ::comphelper::getProcessServiceFactory()->
+        createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.helloWorld") ) ), UNO_QUERY );
+    if( xHelloWorld.is() )
+        xHelloWorld->execute();
+    else
+        fprintf( stderr, "Unable to instantiate xHelloWorld.\n" );
     String aStr = aShowText.GetText();
     if ( !aStr.Len() )

By invoking the global service factory to create an instance of our new component for us, we pass it the name of the service that we want to use and ask it to create one instance of it for us.

Which of course fails :-) so what does happen is the printing of the error message that it is unable to instantiate xHelloWorld.

While the component is built and ready, what happens is that all components are registered into the central registry which specifies the complete list of components available and where their implementations lie, which is in $OOoInstall/program/services.rdb. Which is where the ServiceManager goes in to look for our service and turns up blank because it is definitely not aware of our component and therefore the query for our service fails.

Enter regview and regcomp :-) regview is the registry viewer that is built in registry/ and copied over to the solver, so the path to regview goes: solver/680/unxlngi4.pro/bin/regview. This is of course unnecessary if LinuxIntelEnvSet.sh is sourced, since that places the solver's bin/ directory in the $PATH.

So if we shift over to the $OOoInstall/program directory and issue:

regview services.rdb | less

we would see the entire list of services and their implementations and checking for 'helloWorld' would confirm that our library is not registered within.

What we therefore need to do is register our component implemented by our so into this registry, so we go:

regcomp -register -r services.rdb -c libhworld680li.so

[ This is assuming that we have either copied over our library from the build tree or even better, as the Hacker's Guide describes, linked in our library into the build tree so that it is accessible/hackable from the installation. ]

and do a regview to check for the 'helloWorld' entry and success! :-) The entry should look similar to:

Registry "file:///home/raul/opt/m110/program/services.rdb":

   / org.openoffice.world.hello
     / UNO
       / ACTIVATOR
         Value: Type = RG_VALUETYPE_STRING
                Size = 34
                Data = "com.sun.star.loader.SharedLibrary"
       / SERVICES
         / org.openoffice.helloWorld
       / LOCATION
         Value: Type = RG_VALUETYPE_STRING
                Size = 18
                Data = "libhworld680li.so"
   / org.openoffice.helloWorld
            Size = 35
            Len  = 1
            Data = 0 = "org.openoffice.world.hello"

This time we run the code in charmap, the registry does have the entry corresponding to our service and successfully returns the right service implementation instance to execute() it! w00t! :-)

Similarly, regcomp -revoke -r services.rdb -c libhworld680li.so would remove the registry entry and we'd get back our error message :-) Do note that services.rdb is usually installed as read-only and it would probably be necessary to change its permissions in order to write/register/revoke to the registry.

Which leaves us with one last thing to do, the automatic registration of the component at installation time in addition to copying over as well, and the existing infrastructure takes care of the entire magic for us with two changes in the earlier entry we had:

--- scp2/source/ooo/file_library_ooo.scp        2005-07-23 14:31:19.997821619 +0530
+++ scp2/source/ooo/file_library_ooo.scp        2005-07-23 14:31:54.521173943 +0530
@@ -469,7 +469,8 @@ End
 #ifdef UNX
 File gid_File_Lib_Hl_World
-    Styles = (PACKED);
+    RegistryID = gid_Starregistry_Services_Rdb;
     Dir = gid_Dir_Program;

And then, we're done, or are we now ? :-)

helloworld-uno-style.diff Next: Tutorial_UNO_IDL

See also

Personal tools