OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

How to call macro along with getting its return value (C++)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Mon Nov 21, 2005 1:53 pm    Post subject: How to call macro along with getting its return value (C++) Reply with quote

First off, I need to clarify that the following code does only work with OOo 2.0, because it uses the new Scripting Framework services to accomplish something that is impossible in older OpenOffice versions: Calling a Starbasic function and getting its return value.
Macros could only be called previously by using the dispatch framework, which is not nice at all (parameter passing was also quite a problem!). See http://www.oooforum.org/forum/viewtopic.phtml?t=13712 for more information.

Please don't use my show-case example as it is, because it does not use any error-checking, which is very important (getting NULL is not checked for example). It calls the GetProductName function that should exist in every OOo distribution, because it's a shared standard function:
Code:

#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
#include <com/sun/star/script/provider/XScriptProvider.hpp>
#include <com/sun/star/script/provider/XScript.hpp>

using namespace std;
using namespace rtl;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::script::provider;

/* This is a small code snippet just to show how to call macros and
receive their return values
 (which is not possible with the dispatch framework) using the new
Scripting Framework interfaces.
 It uses the StarBasic GetProductname function coming with every
OOo/SO version to print out the product name.
 ATTENTION: No error checking or handling is done, please check the
IDL Reference to see what exceptions can occur.
*/

int SAL_CALL main( int argc, char **argv )
{

   Sequence <Any> outparam;
   Sequence <sal_Int16> indexes;
   OUString pname;

   Reference< XComponentContext > xContext( ::cppu::bootstrap() );
   Reference< XMultiComponentFactory > xServiceManager(
       xContext->getServiceManager() );
   Reference < com::sun::star::frame::XComponentLoader > xComponentLoader(
       xServiceManager->createInstanceWithContext(
           OUString( RTL_CONSTASCII_USTRINGPARAM(
"com.sun.star.frame.Desktop" ) ),
           xContext ), UNO_QUERY );
   Reference< XComponent > xComponent( xComponentLoader->loadComponentFromURL(
           OUString::createFromAscii("private:factory/swriter"),
           OUString::createFromAscii("_blank") , 0,
           Sequence < ::com::sun::star::beans::PropertyValue >() ) );
   Reference < XScriptProviderSupplier > xScriptPS(xComponent, UNO_QUERY);
   Reference < XScriptProvider >
xScriptProvider(xScriptPS->getScriptProvider(), UNO_QUERY);
   Reference < XScript > xScript(xScriptProvider->getScript(
       OUString::createFromAscii(

"vnd.sun.star.script:Tools.Misc.GetProductname?language=Basic&location=application")),
               UNO_QUERY);
   // call the pre-installed GetProductname function of the shared
Tools library inside the Misc module and extract OUString value from
the returned any
   xScript->invoke(Sequence <Any>(), indexes, outparam) >>= pname;

   // insert the returned string into the opened text document..
   Reference < ::com::sun::star::text::XTextDocument >
xDoc(xComponent, UNO_QUERY);
   Reference < ::com::sun::star::text::XText > xText(xDoc->getText(),
UNO_QUERY);
   xText->setString(pname);

   return 0;
}


I posted this on the dev-api mailinglist some time ago and just thought that it is well-suited in this forum, too.

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Tue Nov 22, 2005 4:30 am    Post subject: Reply with quote

Thank you for this post.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group