Page 1 of 1

[Solved] How does XScript::invoke work

Posted: Mon Feb 11, 2013 11:06 am
by mangesh
I have added the following application level macro to add two numbers using OOBasic IDE in My macros and dialogs section. This macro is added to the CalcLib/Module1.

Code: Select all

Function UGADD( int1 as Integer , int2 as Integer) as Integer

Dim total as Integer
total =  int1 + int2
UGADD = total

End Function
I call the macro from an external C++ application using the following code

Code: Select all

           Any aCtx;
            aCtx <<= OUString(); // This indicates OOoBasic script.
            Reference <XInterface> iFactory = rOfficeServiceManager->createInstance(
                                                    OUString( RTL_CONSTASCII_USTRINGPARAM(
                                                    "com.sun.star.script.provider.MasterScriptProviderFactory" )));

            
            if ( iFactory.is() )
            {
                Reference< XScriptProviderFactory > xFactory(iFactory, UNO_QUERY );
                Reference<XScriptProvider> xScriptProvider = xFactory->createScriptProvider(aCtx);
                        
                if ( !xScriptProvider.is() )
                {
                    printf("ExecuteOOoBasicCommand() Failed to create ScriptProvider");
                    return 1;
                }

                Reference<XScript> xScript = xScriptProvider->getScript(OUString( RTL_CONSTASCII_USTRINGPARAM                         ("vnd.sun.star.script:CalcLib.Module1.UGADD?language=Basic&location=application")));
            
                
                if ( !xScript.is() )
                {
                    printf("ExecuteOOoBasicCommand() Failed to obtain XScript");
                    return 1;
                }
                
                Sequence< Any  > inArgs(2);
                                
                Any aInt1 , aInt2;
                sal_Int32 i = 24, j = 26;
                aInt1 <<= i;  
                aInt2 <<= j;               

                inArgs.getArray()[0] = aInt1; // Input1 is 24
                inArgs.getArray()[1] = aInt2; // input2 is 26 
                

                Sequence< Any > outArgs;                
                Sequence< sal_Int16 > outIndex;

                xScript->invoke(inArgs,outIndex,outArgs);      

                
                sal_Int32 out1 = 0, out2 = 0;
                sal_Int32 in1= 0, in2 = 0;
                Any result;

                sal_Int32 len = outArgs.getLength();
                sal_Int32 num = outIndex.getLength();
                sal_Int32 num1 = inArgs.getLength();

                sal_Int16 val1 = 0, val2 = 0;

                inArgs[0] >>= in1;  // This value is 24 as above
                inArgs[1] >>= in2;  // This value is 26 as above

                outArgs[0] >>= out1; // This value is 24 
                outArgs[1] >>= out2; // This value is 24
                                
                outIndex[0] >>= val1; // This value is 0
                outIndex[1] >>= val2; // This value is 0
                
                sal_Int32 retVal;
                
                if( outArgs.getArray()[0] >>= retVal ) // This is incorrectly returned as the first input value.
                {
                    printf( "int successfully extracted %d!\n"  , retVal );
                }

            }
I need help in getting the output value from this call. I checked out the information at the following link,
http://www.openoffice.org/api/docs/comm ... cript.html

But I cant get the above working. Please help me in getting this sorted out.

thanks
Mangesh

Re: How does XScript::invoke work

Posted: Mon Feb 11, 2013 1:11 pm
by Villeroy
http://www.openoffice.org/api/docs/comm ... tml#invoke
I'd suspect that it works exactly as documented. The method takes exactly one URL string and 3 arrays as arguments. The second array has short integer indices. Do not expect that anybody will debug your code. This is entirely your job.

Re: How does XScript::invoke work

Posted: Mon Feb 11, 2013 1:42 pm
by hanya
Simply keep the return value of the invoke method.

Re: How does XScript::invoke work

Posted: Mon Feb 11, 2013 2:22 pm
by mangesh
Hi Hanya,
I think I missed the return from the invoke. I was looking for the result in the outParams.
thanks for pointing this out.

regards
Mangesh