Difference between revisions of "Documentation/DevGuide/ProUNO/Bridge/Return Values"
OOoWikiBot (talk | contribs) m (FINAL VERSION FOR L10N) |
m |
||
| Line 18: | Line 18: | ||
To receive a return value in C++ provide a <code>VARIANT</code> argument to <code>IDispatch::Invoke:</code> | To receive a return value in C++ provide a <code>VARIANT</code> argument to <code>IDispatch::Invoke:</code> | ||
| − | + | <source lang="idl"> | |
//UNO IDL | //UNO IDL | ||
long func(); | long func(); | ||
| − | + | </source> | |
| − | // | + | <source lang="cpp"> |
| + | //C++ | ||
DISPPARAMS dispparams= { NULL, 0, 0, 0}; | DISPPARAMS dispparams= { NULL, 0, 0, 0}; | ||
VARIANT result; | VARIANT result; | ||
| Line 28: | Line 29: | ||
hr= pdisp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, | hr= pdisp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, | ||
&dispparams, &result, NULL, 0); | &dispparams, &result, NULL, 0); | ||
| + | </source> | ||
| − | The following | + | The following examples show that using VB or JScript is simple: |
| − | + | <source lang="vb"> | |
| − | + | ' VB | |
Dim result As Long | Dim result As Long | ||
result= obj.func | result= obj.func | ||
| − | + | </source> | |
| + | |||
| + | <source lang="javascript"> | ||
//JScript | //JScript | ||
var result= obj.func | var result= obj.func | ||
| + | </source> | ||
When a function has inout parameters then provide arguments by reference in C++: | When a function has inout parameters then provide arguments by reference in C++: | ||
| − | + | <source lang="idl"> | |
//UNO IDL | //UNO IDL | ||
void func( [inout] long val); | void func( [inout] long val); | ||
| − | + | </source> | |
| + | <source lang="cpp"> | ||
//C++ | //C++ | ||
long longOut= 10; | long longOut= 10; | ||
| Line 55: | Line 61: | ||
//The value of longOut will be modified by UNO function. | //The value of longOut will be modified by UNO function. | ||
| + | </source> | ||
| − | The | + | The VB code below is simple because VB uses call by reference by default. After the call to <code>func()</code>, value contains the function output: |
| − | + | <source lang="vb"> | |
Dim value As Long | Dim value As Long | ||
value= 10 | value= 10 | ||
obj.func value | obj.func value | ||
| − | + | </source> | |
The type of argument corresponds to the UNO type according to the default mapping, cf . [[Documentation/DevGuide/ProUNO/Bridge/Type Mappings|Type Mappings]]. If in doubt, use <code>VARIANT</code>s. | The type of argument corresponds to the UNO type according to the default mapping, cf . [[Documentation/DevGuide/ProUNO/Bridge/Type Mappings|Type Mappings]]. If in doubt, use <code>VARIANT</code>s. | ||
| − | + | <source lang="vb"> | |
Dim value As Variant | Dim value As Variant | ||
value= 10; | value= 10; | ||
obj.func value | obj.func value | ||
| − | + | </source> | |
However, there is one exception. If a function takes a character (<code>char</code>) as an argument and is called from VB, use an <code>Integer</code>, because there is no character type in VB. For convenience, the COM bridge also accepts a String as <code>inout</code> and <code>out</code> parameter: | However, there is one exception. If a function takes a character (<code>char</code>) as an argument and is called from VB, use an <code>Integer</code>, because there is no character type in VB. For convenience, the COM bridge also accepts a String as <code>inout</code> and <code>out</code> parameter: | ||
| − | + | <source lang="vb"> | |
| − | + | ' VB | |
Dim value As String | Dim value As String | ||
// string must contain only one character | // string must contain only one character | ||
| Line 76: | Line 83: | ||
Dim ret As String | Dim ret As String | ||
obj.func value | obj.func value | ||
| + | </source> | ||
JScript does not have <code>inout</code> or <code>out</code> parameters. As a workaround, the bridge accepts JScript <code>Array</code> objects. Index 0 contains the value. | JScript does not have <code>inout</code> or <code>out</code> parameters. As a workaround, the bridge accepts JScript <code>Array</code> objects. Index 0 contains the value. | ||
| − | + | <source lang="javascript"> | |
// Jscript | // Jscript | ||
var inout= new Array(); | var inout= new Array(); | ||
| Line 84: | Line 92: | ||
obj.func( inout); | obj.func( inout); | ||
var value= inout[0]; | var value= inout[0]; | ||
| − | + | </source> | |
Out parameters are similar to <code>inout</code> parameters in that the argument does not need to be initialized. | Out parameters are similar to <code>inout</code> parameters in that the argument does not need to be initialized. | ||
| − | + | <source lang="cpp"> | |
//C++ | //C++ | ||
long longOut; | long longOut; | ||
| Line 97: | Line 105: | ||
hr= pdisp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, | hr= pdisp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, | ||
&dispparams, NULL, NULL, 0); | &dispparams, NULL, NULL, 0); | ||
| − | + | </source> | |
| − | + | ||
| + | <source lang="vb"> | ||
| + | ' VB | ||
Dim value As Long | Dim value As Long | ||
obj.func value | obj.func value | ||
| − | + | </source> | |
| + | |||
| + | <source lang="javascript"> | ||
//JScript | //JScript | ||
var out= new Array(); | var out= new Array(); | ||
obj.func(out); | obj.func(out); | ||
var value= out[0]; | var value= out[0]; | ||
| − | + | </source> | |
{{PDL1}} | {{PDL1}} | ||
[[Category:Documentation/Developer's Guide/Professional UNO]] | [[Category:Documentation/Developer's Guide/Professional UNO]] | ||
Revision as of 12:22, 22 October 2009
There are three possible ways to return values in UNO:
- function return values
inoutparametersoutparameters
Return values are commonplace in most languages, whereas inout and out parameters are not necessarily supported. For example, in JScript.
To receive a return value in C++ provide a VARIANT argument to IDispatch::Invoke:
//UNO IDL
long func();
//C++
DISPPARAMS dispparams= { NULL, 0, 0, 0};
VARIANT result;
VariantInit( &result);
hr= pdisp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dispparams, &result, NULL, 0);
The following examples show that using VB or JScript is simple:
' VB
Dim result As Long
result= obj.func
//JScript
var result= obj.func
When a function has inout parameters then provide arguments by reference in C++:
//UNO IDL
void func( [inout] long val);
//C++
long longOut= 10;
VARIANT var;
VariantInit(&var);
var.vt= VT_BYREF | VT_I4;
var.plVal= &longOut;
DISPPARAMS dispparams= { &var, 0, 1, 0};
hr= pdisp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dispparams, NULL, NULL, 0);
//The value of longOut will be modified by UNO function.
The VB code below is simple because VB uses call by reference by default. After the call to func(), value contains the function output:
Dim value As Long
value= 10
obj.func value
The type of argument corresponds to the UNO type according to the default mapping, cf . Type Mappings. If in doubt, use VARIANTs.
Dim value As Variant
value= 10;
obj.func value
However, there is one exception. If a function takes a character (char) as an argument and is called from VB, use an Integer, because there is no character type in VB. For convenience, the COM bridge also accepts a String as inout and out parameter:
' VB
Dim value As String
// string must contain only one character
value= "A"
Dim ret As String
obj.func value
JScript does not have inout or out parameters. As a workaround, the bridge accepts JScript Array objects. Index 0 contains the value.
// Jscript
var inout= new Array();
inout[0]=123;
obj.func( inout);
var value= inout[0];
Out parameters are similar to inout parameters in that the argument does not need to be initialized.
//C++
long longOut;
VARIANT var;
VariantInit(&var);
var.vt= VT_BYREF | VT_I4;
var.plVal= &longOut;
DISPPARAMS dispparams= { &var, 0, 1, 0};
hr= pdisp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dispparams, NULL, NULL, 0);
' VB
Dim value As Long
obj.func value
//JScript
var out= new Array();
obj.func(out);
var value= out[0];
| Content on this page is licensed under the Public Documentation License (PDL). |