Skip to Content

Hello community,

Windows offers the possibility to define Windows Script Components (WSC), also known as Scriptlets.  This means you can compile a script file to a type library, register the WSC file and use it inside any other programming language as a COM object, e.g. like ABAP. You find detailed information about WSC on the Microsoft Technet here.

At first you must define a WSC file. A WSC file is a XML file. To define a WSC you can use the Microsofts Windows Script Component Wizard from here. In six steps you can create a WSC. You can use different Active Scripting Engines – a similar example see here.

Here an example with VBScript and JScript:

<?xml version="1.0"?>
<package>
  <comment>
    Compile this Windows Scripting Component (WSC) to a typelibrary with
    the command: rundll32 scrobj.dll,GenerateTypeLib ABAPTest.wsc
      -file:ABAPTest.tlb -doc:"This is an ABAP test" -name:ABAPTest
    and register this WSC with a right mouse click on it and choose the
    menu item Register.
  </comment>

  <component id="ABAPTest">
    <?component error="true" debug="true"?>

    <registration
      description="ABAPTest"
      progid="ABAPTest.WSC"
      version="1.00"
      classid="{cfda20a9-a891-486d-89b7-9e57d1c91e75}" />

    <public>
      <property name="MsgText">
        <get/>
        <put/>
      </property>
      <method name="ShowMsgBox">
        <parameter name="AddMsgText"/>
      </method>
      <property name="AlertText">
        <get/>
        <put/>
      </property>
      <method name="ShowAlertBox">
        <parameter name="AddAlertText"/>
      </method>
    </public>

    <script language="VBScript">
      <![CDATA[
'-Begin-----------------------------------------------------------------

  '-Variables-----------------------------------------------------------
    Dim MsgText

  '-Properties----------------------------------------------------------
    Function get_MsgText()
      get_MsgText = MsgText
    End Function

    Function put_MsgText(newValue)
      MsgText = newValue
    End Function

  '-Methods-------------------------------------------------------------
    Sub ShowMsgBox(AddMsgText)
      MsgBox MsgText & vbCrLf & AddMsgText
    End Sub

  '-Main----------------------------------------------------------------
    MsgText = "Hello World from WSC VBScript"

'-End-------------------------------------------------------------------
      ]]>
    </script>

    <script language="JScript">
      <![CDATA[
//-Begin----------------------------------------------------------------

  //-Variables----------------------------------------------------------
    var AlertText;

  //-Properties---------------------------------------------------------
    function get_AlertText() {
      return AlertText;
    }

    function put_AlertText(newValue) {
      AlertText = newValue;
    }

  //-Methods------------------------------------------------------------
    function ShowAlertBox(AddAlertText) {
      ShowMsgBox(AlertText + '\r' + AddAlertText)
    }

  //-Main---------------------------------------------------------------
    AlertText = 'Hello World from WSC JScript';

//-End------------------------------------------------------------------
      ]]>
    </script>

  </component>
</package>

 

Compile this WSC file with the command rundll32 scrobj.dll,GenerateTypeLib ABAPTest.wsc -file:ABAPTest.tlb -doc:”This is an ABAP test” -name:ABAPTest to a type libaray and register the WSC file with the context menu item “Register”. Be sure that you have admin rights to register the WSC.

 

That is all, now we can use this componenten inside any other language. Here the VBScript example:

'-Begin-----------------------------------------------------------------

  '-Variables-----------------------------------------------------------
    Dim ABAPTest

  '-Main----------------------------------------------------------------
    Set ABAPTest = CreateObject("ABAPTest.WSC")
    If IsObject(ABAPTest) Then

      '-VBScript--------------------------------------------------------
        ABAPTest.ShowMsgBox("This is a Test")

        ABAPTest.MsgText = "Hello World from ABAP via VBScript"
        ABAPTest.ShowMsgBox("This is another Test")

        MsgBox ABAPTest.MsgText

      '-JScript---------------------------------------------------------
        ABAPTest.MsgText = vbNullString
        ABAPTest.ShowAlertBox("This is a Test")

        ABAPTest.AlertText = "Hello World from ABAP via JScript"
        ABAPTest.ShowAlertBox("This is another Test")

        MsgBox ABAPTest.AlertText

      Set ABAPTest = Nothing
    End If

'-End-------------------------------------------------------------------

 

And here the same example in ABAP:

"-Begin-----------------------------------------------------------------
  Report zWSCTest.

    "-Type pools--------------------------------------------------------
      Type-Pools OLE2.

    "-Variables---------------------------------------------------------
      Data ABAPTest Type OLE2_OBJECT.
      Data MsgText Type String.
      Data AlertText Type String.

    "-Main--------------------------------------------------------------
      Create Object ABAPTest 'ABAPTest.WSC'.
      If sy-subrc <> 0 Or ABAPTest-HANDLE = 0 Or
        ABAPTest-TYPE <> 'OLE2'.
        Exit.
      EndIf.

      "-VBScript--------------------------------------------------------
        Call Method Of ABAPTest 'ShowMsgBox'
          Exporting #1 = 'This is a test'.

        Set Property Of ABAPTest 'MsgText' =
          'Hello World from ABAP via VBScript'.
        Call Method Of ABAPTest 'ShowMsgBox'
          Exporting #1 = 'This is another test'.

        Get Property Of ABAPTest 'MsgText' = MsgText.
        Write: / MsgText.

      "-JScript---------------------------------------------------------
        Set Property Of ABAPTest 'MsgText' = ''.
        Call Method Of ABAPTest 'ShowAlertBox'
          Exporting #1 = 'This is a test'.

        Set Property Of ABAPTest 'AlertText' =
          'Hello World from ABAP via JScript'.
        Call Method Of ABAPTest 'ShowAlertBox'
          Exporting #1 = 'This is another test'.

        Get Property Of ABAPTest 'AlertText' = AlertText.
        Write: / AlertText.

      Free Object ABAPTest.

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

 

If you want to use a WSC on a Windows 64bit system, you must add the CLSID registration from HKCR\CLSID also to HKCR\Wow6432Node\CLSID.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CFDA20A9-A891-486D-89B7-9E57D1C91E75}]
@="ABAPTest"

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CFDA20A9-A891-486D-89B7-9E57D1C91E75}\InprocServer32]
@="C:\\Windows\\system32\\scrobj.dll"
"ThreadingModel"="Apartment"

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CFDA20A9-A891-486D-89B7-9E57D1C91E75}\ProgID]
@="ABAPTest.WSC.1.00"

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CFDA20A9-A891-486D-89B7-9E57D1C91E75}\ScriptletURL]
@="file:///C:/Dummy/ABAPTest.wsc"

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CFDA20A9-A891-486D-89B7-9E57D1C91E75}\VersionIndependentProgID]
@="ABAPTest.WSC"

 

With this option it is very easy to make functions of different scripting engines available in ABAP.

Cheers
Stefan

To report this post you need to login first.

Be the first to leave a comment

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

Leave a Reply