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

How to Store and Use Any Code with ABAP

I presented here the possibility to use FBSL inside ABAP. Unfortunately is FBSL not longer available. FBSL offers JIT compiler layers for Intel-style assembly and ANSI C. Therewith you can use Assembler and C inside your ABAP program. Here now a tiny trick to store and use your Assembler and C code in the ABAP environment.

You can store your code as include and read the include at the execution time of your ABAP program. To do that you can use the following function module. It reads the content of the include and delivers it as string.

      Value(I_INCLNAME) Type SOBJ_NAME
      Value(E_STRINCL) Type String.

        resTADIR Type TADIR,
        tabIncl Type Table Of String,
        lineIncl Type String Value '',
        strIncl Type String Value ''

      Select Single * From TADIR Into resTADIR
        Where OBJ_NAME = I_InclName.
      Check sy-subrc = 0.
      Read Report I_InclName Into tabIncl.
      Check sy-subrc = 0.
      Loop At tabIncl Into lineIncl.
        Concatenate strIncl lineIncl cl_abap_char_utilities=>cr_lf
          Into strIncl.
        lineIncl = ''.
      E_strIncl = strIncl.



Now you can create an include with the following ANSI C content:


  #include <windows.h>

  int main() {
    int rc = MessageBox(0, "Hello World from FBSL Dynamic C Layer",
      "FBSL Dynamic C Layer", 0);


And you can use the C code with the following report:

  Program ZTESTDYNC2.

      Constants SW_SHOWNORMAL Type i Value 1.

        oFBSL Type OLE2_OBJECT,
        Buffer Type String,
        WorkDir Type String,
        WinInc Type String,
        FileName Type String,
        ProcID Type i,
        InclCode Type String

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

      Define Flush.
        Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.

      Create Object oFBSL 'ScriptX'.
      If sy-subrc <> 0 Or  oFBSL-Handle = 0 Or oFBSL-Type <> 'OLE2'.
        Call Function 'ZSCRIPTXDLL'.
        Create Object oFBSL 'ScriptX'.

      If sy-subrc = 0 And oFBSL-Handle > 0 And oFBSL-Type = 'OLE2'.
        "-Show messages in system debugger, e.g. DebugView--------------
          Set Property Of oFBSL '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 oFBSL 'CurrentDirectory' = WorkDir.
        Concatenate '#Include "' WorkDir '\Include\"'
          Into WinInc.
        Call Method Of oFBSL 'ExtractFbslExe'.
        Call Method Of oFBSL 'ExtractFbslWinInc'.
        Call Method Of oFBSL 'ExtractFbslCInc'.

"-FBSL script begin-----------------------------------------------------
_ '#AppType GUI'.
_ '#Option Strict'.
_ WinInc.

  "-Dynamic C routine begin---------------------------------------------
    _ 'DynC Test()'.

    "-Read C code from include file-------------------------------------
      Call Function 'ZREADINCLASSTRING'
        Exporting I_InclName = 'ZTESTDYNCINC'
        Importing E_strIncl = InclCode.
      _ InclCode.
    _ 'End DynC'.

  "-Dynamic C routine end-----------------------------------------------
_ 'Sub Main()'.
_ '  Test()'.
_ 'End Sub'.

"-FBSL script end-------------------------------------------------------

        Concatenate WorkDir '\TestDynC2.fbs' Into FileName.
        Call Method Of oFBSL 'WriteFile' Exporting #1 = FileName
          #2 = Buffer.
        Call Method Of oFBSL 'Shell' = ProcID Exporting
          #1 = 'Fbsl.exe' #2 = 'TestDynC2.fbs'
          #3 = SW_SHOWNORMAL #4 = 1.
        Call Method Of oFBSL 'DeleteFileA'
          Exporting #1 = 'TestDynC2.fbs'.
        Call Method Of oFBSL 'DeleteFileA' Exporting #1 = 'Fbsl.exe'.
        Call Method Of oFBSL 'DeleteDirectory'
          Exporting #1 = 'CInclude'.
        Call Method Of oFBSL 'DeleteDirectory' Exporting #1 = 'Include'.
        Free Object oFBSL.



With ABAP in eclipse you have an excellent environment to develop polyglot, look here, here, here and here.

Now you can use also Assembler and C in your ABAP code and with ABAP in eclipse you have the possibility to do it in one IDE.

Comments are welcome.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.