Purpose With the CallVB statement, you can call .NET functions from a GuiXT script or InputScript – regardless of whether these are implemented in C#, VB.NET, or another .NET language.

In the called function you can

  • use the function parameters (strings or objects)
  • return a string or an object
  • read and write GuiXT variables and long text variables
  • display your own Windows.Forms dialog with form.ShowDialog()
  • call methods of an ActiveX control embedded in the SAP GUI screen with the command
  • use SAP GUI scripting with the analogous CallVBAsync command

The .NET class library (dll) can be made available locally or centrally, e.g. in the SAP MIME Repository

Prerequisites:

  • Component "GuiXT Controls" is active
  • The folder  ".NET class libraries" (guixt.ini parameter VBDirectory) is specified in GuiXT profile. All options that are available for the script directories can be used, e.g.

    VBDirectory  SAPMR:SAP/BC/BSP/SAP/GUIXT/VB
  • The interface library guinet.dll (part of the GuiXT Setup) and your own class library are contained in the ".NET class libraries" folder
Example CallVB msg = utilities.email.send

The .NET class library "utilities.dll" is loaded from the  ".NET class libraries" folder specified in GuiXT profile, guixt.ini-Parameter VBDirectory. For each  SAP GUI mode a separate object of the given class "email" is created. The function  "send" of this object is called and the function result is stored in V[msg].

Format 1 CallVB progname.classname.funcname "par1" "par2 "par2" ...

The class library progname.dll is loaded and for each SAP GUI mode a spearate object of class classname is created. The function  f of this object is called, passing the given strings "par1", "par2",... as function parameters.

Instead of passing the parameters as position parameters, you can also specify the parameter names defined in VB. Example:

CallVB utilities.email.send emailaddr:="&V[email]" cc:="&V[cclist]" subject:="&V[subject]" body:="&V[mytext]"

Format 2 CallVB varname = progname.classname.funcname "par1" "par2 "par2"

The class library progname.dll is loaded and for each SAP GUI mode a spearate object of class classname is created. The function  f of this object is called, passing the given strings "par1", "par2",... as function parameters. The function result is transformed into a string and set into the given GuiXT variable V[varname].

Processing details The first CallVB command for a class library "progname.dll"  loads the library into the .NET runtime. If necessary, the library is first copied from the SAP MIME Repository into the local GuiXT cache folder.

If you use the SAP MIME Repository, please set a VersionNumber in the session script.

The interface libray guinet.dll is also copied, if necessary,

Next an object  of class classname is created, one for each SAP mode, and the given function funcname of this object is executed.

All parameters are passed by value and in string format. The returned value is transformed into string format and stored into the given GuiXT variable.

In the called VB function you may read and write GuiXT variables and longtext variables; for details see Class "guixt" in VB.  Passing parameters "by reference" is not supported but you may simulate this by passing the name of a variable and the use  guixt.GetVariable() and guixt.SetVariable().

Conversion of values If a .NET function declares its parameters as scalar types other than String (e.g., Integer, Decimal, Double, or Date), GuiXT automatically performs a conversion from the GuiXT string to the target type when the function is called. Technically, this is done within the .NET environment using the CTypeDynamic method.

GuiXT automatically converts scalar return types into a string.  In this process, "Boolean" is returned as "-1" for True and "0" for False. Regional settings of the user affect date values and decimal numbers.

Recommendations for typing:

  • Input parameters: For decimal numbers or date values, it is often safer to initially accept parameters as String in .NET. This allows you to control the conversion within the function itself and avoid potential errors caused by differing formats.
  • Return value: If a return value is not only displayed but also logically processed further in GuiXT, the .NET function should return it as a String in a fixed format (e.g., YYYYMMDD). This prevents errors caused by different settings on various client machines.

Example with automatic conversion:

Public Function sqrt(n As Double) As Double
  Return Math.Sqrt(n)
End Function

Example with custom conversion (recommended):

Public Function sqrt(n As String) As String
  Dim number As Double = Double.Parse(n, NumberStyles.Any,
        CultureInfo.InvariantCulture)

  Return Math.Sqrt(number).ToString(CultureInfo.InvariantCulture)

End Function

GuiXT
CallVB result = util.calculations.sqrt "2" 

The result returned is "1.4142135623731".

Object references Object references can be used as function parameters and can be returned by the VB function. GuiXT uses a string representation of the object references and automatically converts these string parameters to VB object references and converts a returned object reference to a string. Technically "IDispatch" interface pointers are used here. You can pass an object reference to a second VB function and then use the object's methods and attributes.

Example:

In the VB.NET-Funktion "newcustomer" we create a new object of class "customer" and return the object reference to GuiXT:

Public Function newcustomer() As customer

  Dim c As New customer

  c.name = "abc GmbH"

  Return c

End Function

Public Function customername(ByVal cust As customer) As String

  Return cust.name

End Function

In GuiXT we call the first function and store the result into a variable. Later on we can call up the second function and pass the "customer" object:

GuiXT
CallVB c1 = mylib.class1.newcustomer

...

CallVB name = mylib.class1.customername "&V[c1]"

Now the variable V[name] contains the string "abc GmbH".

Handling of .NET Exceptions When calling a .NET method, an exception (error causing termination of the CallVB) can occur at various points:
  • The method is not found
  • The parameter transfer is not possible (wrong number of parameters, non-convertible values, ...)
  • The called method terminates with an exception

In all cases, GuiXT sets the return value to "" and generates an error message in the "Script" window, the InputScript trace, as well as in the GuiXT Script Editor Pro, but continues processing the script.

After CallVB, you can check with if not Q[ok] whether an exception has occurred; the corresponding error text is then available in the system variable V[_exception]. Here are some examples:


GuiXT
CallVB result = util.calculations.sqroot "2" 

if not Q[ok] 
  Message "E: &V[_exception]" title="Exception in CallVB"
  Return
endif

Message: "Method 'sqroot' not found in class 'calculations' of dll C:\GuiXT\VB\util.dll"
GuiXT
CallVB result = util.calculations.sqroot "abc" 

if not Q[ok] 
  Message "E: &V[_exception]" title="Exception in CallVB"
  Return
endif

Message for parameter type "Double":
"VB.NET Exception for parameter 'n': The input string has an incorrect format. Conversion from string "abc" to type 'Double' is not valid."

Message for parameter type "String" with custom conversion:
"VB.NET Exception: The input string has an incorrect format."


You can also perform your own validations in the .NET method and then trigger an exception with a custom-defined message using "Throw":
Public Function sqrt(n As String) As String
  Dim number As Double
  If Not Double.TryParse(n, NumberStyles.Any, 
    CultureInfo.InvariantCulture, number) Then
      Throw New Exception
        ("Invalid value passed to method util.sqrt: " + n)
  End If
  Return Math.Sqrt(number)
           .ToString(CultureInfo.InvariantCulture)
 End Function

GuiXT
CallVB result = util.calculations.sqroot "abc" 

if not Q[ok] 
  Message "E: &V[_exception]" title="Exception in CallVB"
  Return
endif

Message:
"VB.NET Exception: Invalid value passed to method util.sqrt: abc"

Debugging You may use the "Stop" command in your VB.NET function to start the Visual Studio debugger.

Alternatively, you can use the "Debug->Attach to process" function for the "saplogon.exe" process in order to activate debugging dynamically for a running SAP session.
Example 2 We start MS Excel with some demo values:
Function StartExcel(ByVal rows As String) As Excel.Application

  Dim oXL As Excel.Application
  Dim oWB As Excel.Workbook
  Dim oSheet As Excel.Worksheet

  oXL = CreateObject("Excel.Application")
  oXL.Visible = True

  oWB = oXL.Workbooks.Add()
  oSheet = oWB.ActiveSheet
  oSheet.Columns(1).ColumnWidth = 30
  oSheet.Columns(2).ColumnWidth = 20

  For k = 1 To CInt(rows)
    oSheet.Cells(k, 1).Value = 
             guixt.GetVariable("region." & k)
    oSheet.Cells(k, 2).Value = 
             guixt.GetVariable("sales." & k)
     Next
   
    Return oXL

End Function
 .NET and Javascript In the tutorial  Advanced: JS, .NET, WASM, you will learn how to integrate .NET with other technologies (JavaScript and WASM) in GuiXT applications.
Tips & Tricks
Components GuiXT + Controls