Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

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:

"-Begin-----------------------------------------------------------------
  Program zUseVBSharp.

    "-TypePools---------------------------------------------------------
      Type-Pools OLE2 .

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

    "-Variables---------------------------------------------------------
      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 ''.

    "-Macros------------------------------------------------------------
      Define _.
        Concatenate PSCode &1 cl_abap_char_utilities=>cr_lf Into PSCode.
      End-Of-Definition.

    "-Main--------------------------------------------------------------
      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.
            EndLoop.

          EndIf.

        EndIf.

        Free Object PS.
      EndIf.

"-End-------------------------------------------------------------------

 

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.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Gene Lang
      Gene Lang

      Is there anyway to call a vb .net (not visual basic) dll function directly from abap without the use of powershell?

      If so; do you have an example?

      Thank you for you time,

      Gene
       

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Gene,

      thanks for your reply.

      There are other ways, but as far as I know it is not possible to call DLL functions directly from ABAP. You can find here another solution or here a solution via PowerShell.

      Cheers
      Stefan

      Author's profile photo Gene Lang
      Gene Lang

      Thank you!

      I'll review your suggestions.

      Gene