| View previous topic :: View next topic |
| Author |
Message |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
Posted: Thu Oct 28, 2004 8:12 am Post subject: How to call a OOoBasic sub in C++ |
|
|
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 |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
Posted: Fri Oct 29, 2004 8:07 am Post subject: |
|
|
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 |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
Cybb20 Super User


Joined: 02 Mar 2004 Posts: 1569 Location: Frankfurt, Germany
|
Posted: Fri Oct 29, 2004 1:54 pm Post subject: |
|
|
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 |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
sprezzatura Newbie

Joined: 17 Nov 2005 Posts: 4 Location: Nova Scotia
|
Posted: Thu Nov 17, 2005 9:58 am Post subject: |
|
|
Does the code sample at the top run in Windows?
(I understand that main() would make it a console app.) |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
Cybb20 Super User


Joined: 02 Mar 2004 Posts: 1569 Location: Frankfurt, Germany
|
|
| Back to top |
|
 |
sprezzatura Newbie

Joined: 17 Nov 2005 Posts: 4 Location: Nova Scotia
|
Posted: Tue Nov 22, 2005 5:31 am Post subject: |
|
|
Does Open Office have to be running in order to respond to the calls?
Thanks,
Pierre |
|
| Back to top |
|
 |
Cybb20 Super User


Joined: 02 Mar 2004 Posts: 1569 Location: Frankfurt, Germany
|
Posted: Tue Nov 22, 2005 6:28 am Post subject: |
|
|
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 ).
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 |
|
 |
|