Quantcast
Channel: SCN : Blog List - ABAP Connectivity
Viewing all articles
Browse latest Browse all 68

ScriptX NEON - Library To Use Any Script Language Inside ABAP

$
0
0

Hello community,

 

one year ago I presented here a way how to use Freestyle BASIC Script Language (FBSL) inside ABAP. Also I presented here a way how to use native C with FBSL inside ABAP. Here is now the Next stEp Of evolutioN (NEON) of FBSLX, ScriptX. You can find ScriptX here and FBSL here.


ScriptX is a COM library to control scripting languages. Therefor it offers a set of different functions which supports the pre and post activities to use any scripting language, and also the communication to the running script. ScriptX contains FBSL, a powerful scripting language which extends the possibilities of ScriptX to a maximum.


zjuliarings2.jpg

Lord of the Julia Rings 2 inside ABAP in eclipse


ScriptX builds a bridge to integrate your presentation server environment on Windows platform in the SAP application server environment. You can communicate between these two worlds on the application server side with ABAP and on the presentation server side with FBSL. FBSL offers phantastic possibilities to be the base on the presentation server side.


ScriptX1.jpg

 

zufo.jpgUFO inside ABAP in eclipse

 

 

Python inside ABAP

 

With ScriptX you can use e.g. Python inside ABAP. Here an example how to use a simple Python 3.4.1 script inside ABAP

 

"-Begin-----------------------------------------------------------------

  Program ZSCRIPTX.

 

    "-Constants---------------------------------------------------------

      Constants SW_SHOWNORMAL Type i Value 1.

 

    "-Variables---------------------------------------------------------

      Data oScriptX Type OLE2_OBJECT.

      Data Buffer Type String Value ''.

      Data WorkDir Type String Value ''.

      Data PathName Type String Value ''.

      Data FileName Type String Value ''.

      Data rc Type i Value 0.

      Data hFileMap Type i Value 0.

      Data RetCode Type String Value ''.

 

    "-Macros------------------------------------------------------------

      Define _.

        Concatenate Buffer &1 cl_abap_char_utilities=>cr_lf Into Buffer.

      End-Of-Definition.

 

      Define Flush.

        Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.

      End-Of-Definition.

 

    "-Main--------------------------------------------------------------

      Create Object oScriptX 'ScriptX'.

      If sy-subrc <> 0 Or  oScriptX-Handle = 0 Or oScriptX-Type <> 'OLE2'.

        Call Function 'ZSCRIPTXDLL'.

        Create Object oScriptX 'ScriptX'.

      EndIf.

 

      If sy-subrc = 0 And oScriptX-Handle > 0 And oScriptX-Type = 'OLE2'.

 

        "-Show messages in system debugger, e.g. DebugView--------------

          Set Property Of oScriptX 'DebugOutput' = 1.

 

        "-Get SAP GUIs work directory-----------------------------------

          Call Method cl_gui_frontend_services=>get_sapgui_workdir

            Changing SAPWORKDIR = WorkDir Exceptions Others = 1.

          Set Property Of oScriptX 'CurrentDirectory' = WorkDir.

 

        Call Method Of oScriptX 'About'.

        Flush.

 

        "-Create archive file-------------------------------------------

          Call Function 'ZPYTHONRAR'.

 

        "-Unpack archive------------------------------------------------

          Call Method Of oScriptX 'Unrar' Exporting

            #1 = 'Python.rar' #2 = ''.

          Flush.

 

        "-Delete archive file-------------------------------------------

          Call Method Of oScriptX 'DeleteFileA' Exporting

            #1 = 'Python.rar'.

          Flush.

 

        Call Method Of oScriptX 'FileMapCreate' = hFileMap

          Exporting #1 = 'SAP001' #2 = 64.

        Flush.

 

        If hFileMap <> 0.

 

"-Python Script begin---------------------------------------------------

 

  "-Coding--------------------------------------------------------------

_ '# -*- coding: iso-8859-15 -*-'.

 

  "-Packages------------------------------------------------------------

_ 'import ctypes'.

_ 'import sys'.

 

  "-Constants-----------------------------------------------------------

_ 'FILE_MAP_ALL_ACCESS = 30'.

 

  "-Main----------------------------------------------------------------

_ 'print("Python version ", sys.version)'.

_ 'print("Hello World from Python")'.

_ 'var_inp = input("Enter something: ")'.

_ 'print("You entered: ", var_inp)'.

 

  "-Transfer the input to the memory map file---------------------------

_ 'hMMF = ctypes.windll.kernel32.OpenFileMappingW(FILE_MAP_ALL_ACCESS, \'.

_ '  0, "SAP001")'.

 

_ 'if hMMF != 0:'.

_ '  buffer = ctypes.windll.kernel32.MapViewOfFile(hMMF, \'.

_ '    FILE_MAP_ALL_ACCESS, 0, 0, 0)'.

 

_ '  if buffer != 0:'.

_ '    ctypes.cdll.msvcrt.strcpy(buffer, var_inp.encode("ascii"))'.

_ '    rc = ctypes.windll.kernel32.UnmapViewOfFile(buffer)'.

_ '  rc = ctypes.windll.kernel32.CloseHandle(hMMF)'.

 

_ 'input("Press any key...")'.

 

"-Python Script end-----------------------------------------------------

 

          "-Create Python script file-----------------------------------

            Concatenate WorkDir '\Test.py' Into FileName.

            Call Method Of oScriptX 'WriteFile' Exporting #1 = FileName

              #2 = Buffer.

            Flush.

 

          "-Add Python path---------------------------------------------

            Concatenate WorkDir '\Python' Into PathName.

            Call Method Of oScriptX 'AddPath' Exporting #1 = PathName.

            Flush.

 

          "-Execute Python script---------------------------------------

            Call Method Of oScriptX 'Shell' = rc

              Exporting #1 = 'python.exe' #2 = 'Test.py'

              #3 = SW_SHOWNORMAL #4 = 1.

            Flush.

 

          "-Read the input from the memory map file---------------------

            Call Method Of oScriptX 'FileMapRead' = RetCode

              Exporting #1 = 'SAP001' #2 = 64.

            Flush.

 

          "-Destroy memory map file-------------------------------------

            Call Method Of oScriptX 'FileMapClose' = rc

              Exporting #1 = hFileMap.

            Flush.

 

          "-Delete Python script file-----------------------------------

            Call Method Of oScriptX 'DeleteFileA'

              Exporting #1 = FileName.

 

          "-Delete Python environment-----------------------------------

            Call Method Of oScriptX 'DeleteDirectory'

              Exporting #1 = 'Python'.

            Flush.

 

          "-Write the content of the memory map file--------------------

            Write: / RetCode.

 

        EndIf.

 

        Free Object oScriptX.

      EndIf.

 

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

 

001.jpg

 

002.JPG

 

Comments are welcome.

 

Enjoy it.

 

Cheers
Stefan


Viewing all articles
Browse latest Browse all 68

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>