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

Tip: How to Check Whether Your Script Runs in SAP GUI Context

In this post I present the possibility to check whether your script runs in SAP GUI context. My example uses CCo, the COM Connector for the RFC library, but you can use also other ways to call a RFC function module (FM). I use the RFC FM RFC_IS_GUI_ON. As you can see I call the FM two times, one time without a SAP GUI connection and the other time with SAP GUI connection (USE_SAPGUI). The FM delivers Y if it runs in SAP GUI context. I check the return value and show a message box with the result.

Here the Visual Basic Script example:


    Option Explicit

    Const RFC_OK = 0

    Dim SAP, hRFC, rc, hFuncDesc, hFunc, Answer, cnt

    Set SAP = CreateObject("COMNWRFC")
    If IsObject(SAP) Then


        Select Case cnt
          Case 0
            '-Without SAP GUI-------------------------------------------
              hRFC = SAP.RfcOpenConnection("ASHOST=ABAP, SYSNR=00, " & _
                "CLIENT=001, USER=BCUSER")
          Case 1
            '-With SAP GUI----------------------------------------------
              hRFC = SAP.RfcOpenConnection("ASHOST=ABAP, SYSNR=00, " & _
                "CLIENT=001, USER=BCUSER, USE_SAPGUI=2")
        End Select

        If hRFC Then

          hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, "RFC_IS_GUI_ON")
          If hFuncDesc Then
            hFunc = SAP.RfcCreateFunction(hFuncDesc)
            If hFunc Then
              If SAP.RfcInvoke(hRFC, hFunc) = RFC_OK Then
                rc = SAP.RfcGetChars(hFunc, "ON", Answer, 1)
                If Answer = "Y" Then
                  MsgBox "GUI is on"
                  MsgBox "GUI is not on"
                End If
              End If
              rc = SAP.RfcDestroyFunction(hFunc)
            End If
          End If

          rc = SAP.RfcCloseConnection(hRFC)
        End If

        cnt = cnt + 1
      Loop Until cnt = 2

      Set SAP = Nothing
    End If


Here the AutoIt example, but only with one non-GUI call:


    AutoItSetOption("MustDeclareVars", 1)

    Const $RFC_OK = 0

    Dim $SAP, $hRFC, $hFuncDesc, $hFunc, $Answer

    $SAP = ObjCreate("COMNWRFC")
    If IsObj($SAP) Then

      $hRFC = $SAP.RfcOpenConnection("ASHOST=ABAP, SYSNR=00, " & _
        "CLIENT=001, USER=BCUSER")
      If $hRFC Then

        $hFuncDesc = $SAP.RfcGetFunctionDesc($hRFC, "RFC_IS_GUI_ON")
        If $hFuncDesc Then
          $hFunc = $SAP.RfcCreateFunction($hFuncDesc)
          If $hFunc Then
            If $SAP.RfcInvoke($hRFC, $hFunc) = $RFC_OK Then
              $SAP.RfcGetChars($hFunc, "ON", $Answer, 1)
              If $Answer = "Y" Then
                MsgBox(0, "", "GUI is on")
                MsgBox(0, "", "GUI is not on")

      $SAP = 0



Enjoy it.

Assigned Tags

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