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 a OOoBasic sub in C++

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


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

PostPosted: Thu Oct 28, 2004 8:12 am    Post subject: How to call a OOoBasic sub in C++ Reply with quote

Our goal is to execute an OOoBasic sub. I start with a Danny's interesting post
http://www.oooforum.org/forum/viewtopic.php?p=20890#20890 :

In the macros of OOo and module “Essai1” I put this :
Code:

REM  *****  BASIC  *****
Sub SaySomething( x )
   MsgBox x
End Sub

The OOoBasic code would be :
Code:

REM  *****  BASIC  *****
Sub Main
   oDispatch = createUnoService( "com.sun.star.frame.DispatchHelper" )
   oDispatch.executeDispatch( StarDesktop, "macro:///Standard.Module1.SaySomething(Hi)",
      "", 0, Array() )
End Sub

Translating this code in C++ is easy : we give again the entire main
Code:

int main( ) {
//retrieve an instance of the remote service manager
    Reference< XMultiServiceFactory > rOfficeServiceManager;
    rOfficeServiceManager = ooConnect();
    if( rOfficeServiceManager.is() ){
        printf( "Connected sucessfully to the office\n" );
    }

//get the desktop service using createInstance returns an XInterface type
    Reference< XInterface  > Desktop = rOfficeServiceManager->createInstance(
    OUString::createFromAscii( "com.sun.star.frame.Desktop" ));

//query for the XComponentLoader interface
    Reference< XComponentLoader > rComponentLoader (Desktop, UNO_QUERY);
    if( rComponentLoader.is() ){
           printf( "XComponentloader successfully instanciated\n" );
       }

   Reference< XDesktop > rDesktop(Desktop,UNO_QUERY);

// Don't forget to add : #include <com/sun/star/frame/XDispatchHelper.hpp>
// Don't forget to add "com.sun.star.frame.XDispatchHelper \" in the makefile
// Query the XDispatcher Interface
   Reference< XDispatchHelper > rDispatchHelper = Reference< XDispatchHelper >
            ( rOfficeServiceManager->createInstance(
                                        OUString( RTL_CONSTASCII_USTRINGPARAM(
                                        "com.sun.star.frame.DispatchHelper" ))), UNO_QUERY );


// Don't forget to add : #include <com/sun/star/frame/XDispatchProvider.hpp>
// Don't forget to add "com.sun.star.frame.XDispatchProvider \" in the makefile
// Query the XDispatchProvider Interface
   Reference< XDispatchProvider > rDispatchProvider(rDesktop,UNO_QUERY);

   rDispatchHelper->executeDispatch(rDispatchProvider,
         OUString::createFromAscii("macro:///Standard.Essai1.SaySomething(Hi)"),
         OUString::createFromAscii(""),
         0,
         Sequence < ::com::sun::star::beans::PropertyValue > ());
    return 0;
}

which prints out a message box with "Hi".

To compile this example I will give soon more explanations in my document (Or see
Setting the page properties/margins directly from C++ http://www.oooforum.org/forum/viewtopic.php?t=12734
_________________
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
SergeM
Super User
Super User


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

PostPosted: Thu Oct 28, 2004 9:09 am    Post subject: Reply with quote

This code works but I just discover an "139 Error" in the console.
The last line (befor the return) is the OOoBasic call which works and then I don't see exacly why this gives an error.
I will see this problem to morrow.
_________________
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
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Oct 28, 2004 9:18 am    Post subject: Reply with quote

Very interesting example.
The more you do with this, the more it tempts me to consider C++ again.
I edited your post to make the url clickable.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
SergeM
Super User
Super User


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

PostPosted: Fri Oct 29, 2004 8:07 am    Post subject: Reply with quote

When writing the XRay thread I suddently understood what happen and why there is an error in my program. This morning I try to add some code befor the return and it prints out what I have asked (first the messageBox from OOoBasic and second the printf sentence) but return an 139 error. I deduce the problem is with an object destructor. But why ? Probably because OOoBasic is still running when the destructor is called. the two process are completly asynchronous. This will bring a complication to my XRay call.
I first test this idea and come back for the conclusion.
_________________
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
SergeM
Super User
Super User


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

PostPosted: Fri Oct 29, 2004 8:15 am    Post subject: Reply with quote

Yes waiting with a getchar() after launching the OOoBasic code remove the error.
_________________
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
Cybb20
Super User
Super User


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

PostPosted: Fri Oct 29, 2004 1:54 pm    Post subject: Reply with quote

I guess what is wrong about it is the following line in the C++ code:
Code:

OUString::createFromAscii("macro:///Standard.Essai1.SaySomething(Hi)"),

Hi is not a string here. It is passed as an identifier. To pass it as a string, do this:
Code:

OUString::createFromAscii("macro:///Standard.Essai1.SaySomething(\"Hi\")"),


That's only a guess.

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: Sat Oct 30, 2004 4:30 am    Post subject: Reply with quote

Cybb20,
Curiously my code is not wrong. The way you show us is a OOoBasic way. I have read in a thread somewere in this Forum that in Java you don't need the " around the string you pass to the sub. I try it in C++ and it works. I cannot explain why !
_________________
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
SergeM
Super User
Super User


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

PostPosted: Mon Nov 01, 2004 1:24 am    Post subject: Reply with quote

Sorry,
In fact as shown above with Danny'code OOoBasic code doesn't need \"Hi\" too, like the C++ code.
_________________
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
SergeM
Super User
Super User


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

PostPosted: Mon Nov 01, 2004 1:35 am    Post subject: Reply with quote

Some other interesting links :
calling a macro via uno (Java) :
http://www.oooforum.org/forum/viewtopic.php?p=15589#15589
apply macro from differents files http://www.oooforum.org/forum/viewtopic.php?p=20890#20890
Problems with uno:url for document internal (Java ) macroshttp://www.oooforum.org/forum/viewtopic.php?p=17377
_________________
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
sprezzatura
Newbie
Newbie


Joined: 17 Nov 2005
Posts: 4
Location: Nova Scotia

PostPosted: Thu Nov 17, 2005 9:58 am    Post subject: Reply with quote

Does the code sample at the top run in Windows?

(I understand that main() would make it a console app.)
Back to top
View user's profile Send private message
SergeM
Super User
Super User


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

PostPosted: Thu Nov 17, 2005 12:25 pm    Post subject: Reply with quote

Quote:

Does the code sample at the top run in Windows? (I understand that main() would make it a console app.)

The code at the top is UNO code that means :
- of course it can be a console app
- but it requires OOo running and waiting a connexion from the code and then executing some things in OpenOffice.org. It's similar as a COM code which automate MSWord for instance but does'nt use COM

If you want to make a console app which launch a OOobasic macro there is a easier way (I think). I give any threads showing how to call from shell, but all what is doing with a shell is doing with programming too :

How to convert Word -> PDF from the command line http://www.oooforum.org/forum/viewtopic.phtml?t=3772

Start macro from command line http://www.oooforum.org/forum/viewtopic.phtml?t=7975
simple command line question http://www.oooforum.org/forum/viewtopic.phtml?t=2619&highlight=
How can I install my macro from C++
http://www.oooforum.org/forum/viewtopic.phtml?t=4784
Run macro from document from shell :
[url]http ://www.oooforum.org/forum/viewtopic.phtml?t=6808[/url]
Invoke an OO macro from shell http://www.oooforum.org/forum/viewtopic.phtml?t=17143&highlight=
_________________
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
Cybb20
Super User
Super User


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

PostPosted: Mon Nov 21, 2005 1:56 pm    Post subject: Reply with quote

See http://www.oooforum.org/forum/viewtopic.phtml?t=27453&highlight= for a better way to call macros (only for OOo 2.0).

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
sprezzatura
Newbie
Newbie


Joined: 17 Nov 2005
Posts: 4
Location: Nova Scotia

PostPosted: Tue Nov 22, 2005 5:31 am    Post subject: Reply with quote

Does Open Office have to be running in order to respond to the calls?

Thanks,

Pierre
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


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

PostPosted: Tue Nov 22, 2005 6:28 am    Post subject: Reply with quote

Hi sprezzatura:
Yes it must be running, it acts as the server for incoming calls, and if you shutdown the server no connection exists anymore and your calls end in nirvana (or something like that Wink).

Note that the office does not need to run prior to launching some Java or C++ (soon for Python as well) code if you want to start the office on the same machine you have your code running, the means to it is called the simple bootstrap mechanism which uses a named pipe of random name and spawns the soffice process. My example on the referred page does use this mechanism.

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
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