Return Values

From Apache OpenOffice Wiki
Jump to: navigation, search



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 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).
Personal tools
In other languages