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

How to Store and Use Any Code with ABAP

$
0
0

Hello community,

 

I presented here the possibility to use FBSL via ScriptX inside ABAP. 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.

 

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

  Function ZREADINCLASSTRING

    Importing

      Value(I_INCLNAME) Type SOBJ_NAME

    Exporting

      Value(E_STRINCL) Type String.

 

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

      Data resTADIR Type TADIR.

      Data tabIncl Type Table Of String.

      Data lineIncl Type String Value ''.

      Data strIncl Type String Value ''.

 

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

      Select Single * From TADIR Into resTADIR

        Where OBJ_NAME = I_InclName.

      If sy-subrc = 0.

 

        Read Report I_InclName Into tabIncl.

        If sy-subrc = 0.

          Loop At tabIncl Into lineIncl.

            Concatenate strIncl lineIncl cl_abap_char_utilities=>cr_lf

              Into strIncl.

            lineIncl = ''.

          EndLoop.

        EndIf.

 

      EndIf.

      E_strIncl = strIncl.

 

  EndFunction.

 

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

 

 

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

 

//-Begin----------------------------------------------------------------

 

  #include <windows.h>

 

  int main() {

 

    int rc = MessageBox(0, "Hello World from FBSL Dynamic C Layer",

      "FBSL Dynamic C Layer", 0);

 

  }

 

//-End------------------------------------------------------------------

 

 

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

 

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

  Program ZTESTDYNC2.

 

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

      Constants SW_SHOWNORMAL Type i Value 1.

 

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

      Data oFBSL Type OLE2_OBJECT.

      Data Buffer Type String Value ''.

      Data WorkDir Type String Value ''.

      Data WinInc Type String Value ''.

      Data FileName Type String Value ''.

      Data ProcID Type i Value 0.

      Data InclCode 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 oFBSL 'ScriptX'.

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

        Call Function 'ZSCRIPTXDLL'.

        Create Object oFBSL 'ScriptX'.

      EndIf.

 

      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\Windows.inc"'

          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.

        Flush.

 

        Call Method Of oFBSL 'Shell' = ProcID Exporting

          #1 = 'Fbsl.exe' #2 = 'TestDynC2.fbs'

          #3 = SW_SHOWNORMAL #4 = 1.

        Flush.

 

        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.

      EndIf.

 

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

 

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.

 

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>