Difference between revisions of "Documentation/DevGuide/ProUNO/Bridge/Return Values"
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> | ||
− | < | + | <syntaxhighlight lang="idl"> |
//UNO IDL | //UNO IDL | ||
long func(); | long func(); | ||
− | </ | + | </syntaxhighlight> |
− | < | + | <syntaxhighlight lang="cpp"> |
//C++ | //C++ | ||
DISPPARAMS dispparams= { NULL, 0, 0, 0}; | DISPPARAMS dispparams= { NULL, 0, 0, 0}; | ||
Line 29: | 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); | ||
− | </ | + | </syntaxhighlight> |
The following examples show that using VB or JScript is simple: | The following examples show that using VB or JScript is simple: | ||
− | < | + | <syntaxhighlight lang="vb"> |
' VB | ' VB | ||
Dim result As Long | Dim result As Long | ||
result= obj.func | result= obj.func | ||
− | </ | + | </syntaxhighlight> |
− | < | + | <syntaxhighlight lang="javascript"> |
//JScript | //JScript | ||
var result= obj.func | var result= obj.func | ||
− | </ | + | </syntaxhighlight> |
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++: | ||
− | < | + | <syntaxhighlight lang="idl"> |
//UNO IDL | //UNO IDL | ||
void func( [inout] long val); | void func( [inout] long val); | ||
− | </ | + | </syntaxhighlight> |
− | < | + | <syntaxhighlight lang="cpp"> |
//C++ | //C++ | ||
long longOut= 10; | long longOut= 10; | ||
Line 61: | Line 61: | ||
//The value of longOut will be modified by UNO function. | //The value of longOut will be modified by UNO function. | ||
− | </ | + | </syntaxhighlight> |
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: | 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: | ||
− | < | + | <syntaxhighlight lang="vb"> |
Dim value As Long | Dim value As Long | ||
value= 10 | value= 10 | ||
obj.func value | obj.func value | ||
− | </ | + | </syntaxhighlight> |
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. | ||
− | < | + | <syntaxhighlight lang="vb"> |
Dim value As Variant | Dim value As Variant | ||
value= 10; | value= 10; | ||
obj.func value | obj.func value | ||
− | </ | + | </syntaxhighlight> |
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: | ||
− | < | + | <syntaxhighlight lang="vb"> |
' VB | ' VB | ||
Dim value As String | Dim value As String | ||
Line 83: | Line 83: | ||
Dim ret As String | Dim ret As String | ||
obj.func value | obj.func value | ||
− | </ | + | </syntaxhighlight> |
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. | ||
− | < | + | <syntaxhighlight lang="javascript"> |
// Jscript | // Jscript | ||
var inout= new Array(); | var inout= new Array(); | ||
Line 92: | Line 92: | ||
obj.func( inout); | obj.func( inout); | ||
var value= inout[0]; | var value= inout[0]; | ||
− | </ | + | </syntaxhighlight> |
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. | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//C++ | //C++ | ||
long longOut; | long longOut; | ||
Line 105: | 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); | ||
− | </ | + | </syntaxhighlight> |
− | < | + | <syntaxhighlight lang="vb"> |
' VB | ' VB | ||
Dim value As Long | Dim value As Long | ||
obj.func value | obj.func value | ||
− | </ | + | </syntaxhighlight> |
− | < | + | <syntaxhighlight 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]; | ||
− | </ | + | </syntaxhighlight> |
{{PDL1}} | {{PDL1}} | ||
[[Category:Documentation/Developer's Guide/Professional UNO]] | [[Category:Documentation/Developer's Guide/Professional UNO]] |
Latest revision as of 14:04, 23 December 2020
There are three possible ways to return values in UNO:
- function return values
-
inout
parameters -
out
parameters
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 VARIANT
s.
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). |