Skip to Content

Hello community,

today I want to 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:

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

  '-Directives----------------------------------------------------------
    Option Explicit

  '-Constants-----------------------------------------------------------
    Const RFC_OK = 0

  '-Variables-----------------------------------------------------------
    Dim SAP, hRFC, rc, hFuncDesc, hFunc, Answer, cnt

  '-Main----------------------------------------------------------------
    Set SAP = CreateObject("COMNWRFC")
    If IsObject(SAP) Then

      Do

        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"
                Else
                  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

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

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

;-Begin-----------------------------------------------------------------

  ;-Directives----------------------------------------------------------
    AutoItSetOption("MustDeclareVars", 1)

  ;-Constants-----------------------------------------------------------
    Const $RFC_OK = 0

  ;-Variables-----------------------------------------------------------
    Dim $SAP, $hRFC, $hFuncDesc, $hFunc, $Answer

  ;-Main----------------------------------------------------------------
    $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")
              Else
                MsgBox(0, "", "GUI is not on")
              EndIf
            EndIf
            $SAP.RfcDestroyFunction($hFunc)
          EndIf
        EndIf

        $SAP.RfcCloseConnection($hRFC)
      EndIf
      $SAP = 0
    EndIf

;-End-------------------------------------------------------------------

 

Enjoy it.

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