Skip to Content

How to Use VB# (Visual Basic dotNET) Inside ABAP

I presented here the possibiltiy how to use Windows PowerShell inside ABAP.

Here now the possibility how to use VB#, Visual Basic for dotNET, inside ABAP, on the same way:

  Program zUseVBSharp.

      Type-Pools OLE2 .

      Constants OUTPUT_CONSOLE Type i Value 0.
      Constants OUTPUT_WINDOW Type i Value 1.
      Constants OUTPUT_BUFFER Type i Value 2.

      Data PS Type OLE2_OBJECT.
      Data Result Type i Value 0.
      Data strResult Type String Value ''.
      Data tabResult Type Table Of String.
      Data PSCode Type String Value ''.

      Define _.
        Concatenate PSCode &1 cl_abap_char_utilities=>cr_lf Into PSCode.

      Create Object PS ' SAPIEN.ActiveXPoSHV3'.
      If sy-subrc = 0 And PS-Handle <> 0 And PS-Type = 'OLE2'.

        Call Method Of PS 'Init' = Result Exporting #1 = 0.
        If Result = 0.

          Call Method Of PS 'IsPowerShellInstalled' = Result.
          If Result <> 0.

            Set Property Of PS 'OutputMode' = OUTPUT_BUFFER.

"-PowerShell Begin------------------------------------------------------

_ '$VBCode = @"'.

"-VB# Begin-------------------------------------------------------------

_ 'Option Strict On'.

_ 'Imports System'.
_ 'Imports Microsoft.VisualBasic'.

_ 'Namespace VBCode'.

_ '  Public Class VB'.

_ '    Public Shared Function Hello1() As String'.
_ '      Return "Hello World!"'.
_ '    End Function'.

_ '    Public Function Hello2(ByVal Name As String) As String'.
_ '      Return "Hello " & Name & "!"'.
_ '    End Function'.

_ '    Public Sub Hello3(ByVal Name As String)'.
_ '      MsgBox(Name, MsgBoxStyle.OkOnly, "Hello")'.
_ '    End Sub'.

_ '  End Class'.

_ 'End Namespace'.

"-VB# End---------------------------------------------------------------

_ '"@;'.

_ 'Add-Type -TypeDefinition $VBCode -Language VisualBasic'.
_ '$VB = new-Object VBCode.VB'.

_ '[VBCode.VB]::Hello1()'.
_ '$VB.Hello2("Stefan")'.
_ '$VB.Hello3("Stefan")'.

"-PowerShell End--------------------------------------------------------

            Call Method Of PS 'Execute' Exporting
              #1 = PSCode.

            Call Method Of PS 'OutputString' = strResult.

            Split strResult At cl_abap_char_utilities=>cr_lf
              Into Table tabResult.

            Loop At tabResult Into strResult.
              Write: / strResult.



        Free Object PS.



The PowerShell code encloses the VB# code. The VB# code is a namespace, a class and three methods. In the PowerShell code, below VB# code, these methods are called. Hello1 and Hello2 returns the result inside the output buffer of ActiveX-PowerShell and Hello3 opens a message box.

On the same way you can embed C# code inside ABAP.

I think this possibility opens the gate very wide to embed the power of dotNET framework and its languages.

You must be Logged on to comment or reply to a post.