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

How to use RFC-SDK with FreeBASIC – Part 3: Server Apps (SAP uses FreeBASIC)

In the 3rd part now an example of an SAP server application. That means that you can use FreeBASIC functions from SAP via ABAP Call Function command.

'-Begin-----------------------------------------------------------------
'
' To know the gateway service (gwserv) and host (gwhost) use TAC SMGW
' and the menu Goto > Parameters > Display and look at the attribute
' entries gateway hostname and gateway service.
'
' Insert in the file Windows\system32\drivers\etc\services the entry
' sapgw00 with 3300/tcp for sapgw00, or e.g. for sapgw99 3399/tcp.
'
' Customize with TAC SM59 the RFC destination PBTESTPROGRAM
'
'-----------------------------------------------------------------------

  '-Includes------------------------------------------------------------
    #Include Once "sapnwrfc.inc"

  '-Function ABAPCall---------------------------------------------------
    Function ABAPCall(ByVal rfcHandle As Integer, _
      ByVal funcHandle As Integer, errorInfo As RFC_ERROR_INFO) _
      As Integer

      MessageBox(null, "ABAP call", "", MB_OK Or MB_ICONINFORMATION)

      Return RFC_OK
    End Function

  '-Variables-----------------------------------------------------------
    Dim RfcErrorInfo As RFC_ERROR_INFO
    Dim connParams(3) AS RFC_CONNECTION_PARAMETER
    Dim As Integer hDesc, hConn, rc
    Dim As WString * 16 nProgramID, nGWHost, nGWServ
    Dim As WString * 16 vProgramID, vGWHost, vGWServ

  '-Main----------------------------------------------------------------
    nProgramID = "PROGRAM_ID" : vProgramID = "FBTESTPROGRAM"
    nGWHost    = "GWHOST"     : vGWHost    = "ABAP"
    nGWServ    = "GWSERV"     : vGWServ    = "sapgw00"

    connParams(0).name = @nProgramID : connParams(0).value = @vProgramID
    connParams(1).name = @nGWHost    : connParams(1).value = @vGWHost
    connParams(2).name = @nGWServ    : connParams(2).value = @vGWServ

    hDesc = RfcCreateFunctionDesc("ABAPCall", RfcErrorInfo)
    RfcErrorHandler()

    If hDesc <> 0 And RfcErrorInfo.code = RFC_OK Then

      RfcInstallServerFunction "", hDesc, @ABAPCall, RfcErrorInfo
      RfcErrorHandler()

      If RfcErrorInfo.code = RFC_OK Then

        hConn = RfcRegisterServer(@connParams(0), 3, RfcErrorInfo)
        RfcErrorHandler()

        If hConn <> 0 And RfcErrorInfo.code = RFC_OK Then

          rc = RFC_OK
          While rc = RFC_OK Or rc = RFC_RETRY
            rc = RfcListenAndDispatch(hConn, 4, RfcErrorInfo)
            Select Case rc
              Case RFC_OK
                OutputDebugString("RFC_OK")
              Case RFC_RETRY
                OutputDebugString("RFC_RETRY")
            End Select
          Wend

        End If

      End If

    End If

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

 

At first we define the function in FreeBASIC which was called from ABAP, here ABAPCall. Now we define the connections parameters for the server registration. Parallel you must configure with the TAC SM59 your server program in the SAP system.

 

0801_SM59.jpg

 

We create a function description via RfcCreateFunctionDesc and install our function via RfcInstallServerFunction. With RfcRegisterServer we register the server on the SAP system and with an endless loop we listen via RfcListenAndDispatch about our ABAP call to the FreeBASIC server application.

Here the ABAP program to call the FreeBASIC function:

"-Begin-----------------------------------------------------------------
  Report zFBServerTest.

  Call Function 'ABAPCall' Destination 'FBTESTPROGRAM'.

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

As you can see, it is also very easy to code a server application which uses FreeBASIC functions from an SAP system.

You find Part 1 here, and Part 2 here.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello community,

      here a small addition to the code:

      '-Begin-----------------------------------------------------------------
      '
      ' To know the gateway service (gwserv) and host (gwhost) use TAC SMGW
      ' and the menu Goto > Parameters > Display and look at the attribute
      ' entries gateway hostname and gateway service.
      '
      ' Insert in the file Windows\system32\drivers\etc\services the entry
      ' sapgw00 with 3300/tcp for sapgw00, or e.g. for sapgw99 3399/tcp.
      '
      ' Customize with TAC SM59 the RFC destination PBTESTPROGRAM
      '
      '-----------------------------------------------------------------------
      
        '-Includes------------------------------------------------------------
          #Include Once "sapnwrfc.inc"
      
        '-Global variables----------------------------------------------------
          Common Shared quitFlag As Byte
      
        '-Sub Quit------------------------------------------------------------
          Sub Quit()
            MessageBox(null, "Exit Server", "SAP Server", MB_OK)
            quitFlag = TRUE
          End Sub
      
        '-Function ABAPCall---------------------------------------------------
          Function ABAPCall(ByVal rfcHandle As Integer, _
            ByVal funcHandle As Integer, errorInfo As RFC_ERROR_INFO) _
            As Integer
      
            MessageBox(null, "ABAP call", "", MB_OK Or MB_ICONINFORMATION)
      
            Return RFC_OK
          End Function
      
        '-Variables-----------------------------------------------------------
          Dim RfcErrorInfo As RFC_ERROR_INFO
          Dim connParams(3) AS RFC_CONNECTION_PARAMETER
          Dim As Integer hDesc, hConn, rc
          Dim As WString * 16 nProgramID, nGWHost, nGWServ
          Dim As WString * 16 vProgramID, vGWHost, vGWServ
      
        '-Main----------------------------------------------------------------
          nProgramID = "PROGRAM_ID" : vProgramID = "FBTESTPROGRAM"
          nGWHost    = "GWHOST"     : vGWHost    = "ABAP"
          nGWServ    = "GWSERV"     : vGWServ    = "sapgw00"
      
          connParams(0).name = @nProgramID : connParams(0).value = @vProgramID
          connParams(1).name = @nGWHost    : connParams(1).value = @vGWHost
          connParams(2).name = @nGWServ    : connParams(2).value = @vGWServ
      
          hDesc = RfcCreateFunctionDesc("ABAPCall", RfcErrorInfo)
          RfcErrorHandler()
          If hDesc <> 0 And RfcErrorInfo.code = RFC_OK Then
      
            RfcInstallServerFunction "", hDesc, @ABAPCall, RfcErrorInfo
            RfcErrorHandler()
            If RfcErrorInfo.code = RFC_OK Then
      
              hConn = RfcRegisterServer(@connParams(0), 3, RfcErrorInfo)
              RfcErrorHandler()
              If hConn <> 0 And RfcErrorInfo.code = RFC_OK Then
      
                quitFlag = FALSE
                ThreadCreate @Quit, 0
      
                rc = RFC_OK
                While rc = RFC_OK Or rc = RFC_RETRY
      
                  rc = RfcListenAndDispatch(hConn, 4, RfcErrorInfo)
                  Select Case rc
                    Case RFC_OK
                      OutputDebugString("RFC_OK")
                    Case RFC_RETRY
                      OutputDebugString("RFC_RETRY")
                  End Select
      
                  If quitFlag = TRUE Then
                    Exit While
                  End If
      
                Wend
      
              End If
      
            End If
      
          End If
      
      
      '-End-------------------------------------------------------------------

      I added a smal asynchronous function to exit the program without bother the Task Manager.

      Also you find here an example integration scenario to use FreeBASIC with ABAP in Eclipse.

      Cheers

      Stefan

      Author's profile photo Jack Graus
      Jack Graus

      Hello Stefan

      Next to using a 3th party tool like this SAP offers similar tools like the SAP .NET connector. For setting up a connection to SAP what advantages you see on using FreeBASIC compared to using the SAP .NET connector ?

      Regards Jack

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Jack,

      thank you for your reply.

      I think there are two good reasons to use this kind of development environment in comparison with a dotNET language:

      1. FreeBASIC is available on Windows and Linux. The next version of FreeBASIC is available for 64 and 32 bit. You can use the same source code, without any changes. (Sorry, but I don't know enough about the Mono Project.)
      2. FreeBASIC needs no framework. The single executable and the SAP NetWeaver libraries are enough, you need no more. Absolut easy xcopy Installation.

      If your focus is not on developing for different platforms and your opinion is, that the dotNET Framework anyway already exists, FreeBASIC offers not really advantages.

      But I think if you want platform independent development with minimum requirements, FreeBASIC is a good choice.

      Cheers

      Stefan