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

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

I presented here and here the possibility to code FreeBASIC programs as SAP client applications via SAP RFC SDK. Now in the 4th part I will show, how easy it is to code Common Gateway Interface (CGI) programs with FreeBASIC and to embed an SAP client application inside this CGI program. This CGI program can be implemented on any web server – Attention, it is platform dependent – and on this way you can integrate SAP NW RFC library functions seamlessly.

We start with the FreeBASIC code. For this example I use cgi-util from New Breed Software. Hint: Also you can use FastCGI  from here, FreeBASIC offers for both an interface. For a little bit more information look here.


    #Include Once ""
    #Include Once ""

    Dim RfcErrorInfo As RFC_ERROR_INFO
    Dim As Integer hRFC, hFuncDesc, hFunc, hStruct, res
    Dim As ZString Ptr pASHost, pSysNr, pClient, pUser, pPassWd, pLang
    Dim As WString * 16 nASHost, nSysNr, nClient, nUser, nPassWd, nLang
    Dim As WString * 16 vASHost, vSysNr, vClient, vUser, vPassWd, vLang
    Dim As WString * 9 SAPHost, SAPSysID
    Dim As WString * 11 SAPDBSys
    Dim As WString * 33 SAPDBHost

    #Macro RfcErrorHandlerCGI()
      If RfcErrorInfo.code <> RFC_OK Then
        Print "<h1>RFC Error Message</h1><br>"
        Print RfcErrorInfo.message
      End If

    res = cgi_init()
    Print !"Content-type: text/html\n\n"

    If res <> CGIERR_NONE Then
      Print "Error " & Str(res) & " " & Str(cgi_strerror(res)) & "<p>"

      pASHost = cgi_getentrystr("ASHOST") : nASHost = "ASHOST" : vASHost = *pASHost
      pSysNr  = cgi_getentrystr("SYSNR")  : nSysNr  = "SYSNR"  : vSysNr  = *pSysNr
      pClient = cgi_getentrystr("CLIENT") : nClient = "CLIENT" : vClient = *pClient
      pUser   = cgi_getentrystr("USER")   : nUser   = "USER"   : vUser   = *pUser
      pPassWd = cgi_getentrystr("PASSWD") : nPassWd = "PASSWD" : vPassWd = *pPassWd
      pLang   = cgi_getentrystr("LANG")   : nLang   = "LANG"   : vLang   = *pLang

      connParams(0).name = @nASHost : connParams(0).value = @vASHost
      connParams(1).name = @nSysNr  : connParams(1).value = @vSysNr
      connParams(2).name = @nClient : connParams(2).value = @vClient
      connParams(3).name = @nUser   : connParams(3).value = @vUser
      connParams(4).name = @nPassWd : connParams(4).value = @vPassWd
      connParams(5).name = @nLang   : connParams(5).value = @vLang

      hRFC = RfcOpenConnection(@connParams(0), 8, RfcErrorInfo)
      If hRFC <> 0 And RfcErrorInfo.code = RFC_OK Then

        hFuncDesc = RfcGetFunctionDesc(hRFC, "RFC_SYSTEM_INFO", _
        If hFuncDesc <> 0 And RfcErrorInfo.code = RFC_OK Then

          hFunc = RfcCreateFunction(hFuncDesc, RfcErrorInfo)
          If hFunc <> 0 And RfcErrorInfo.code = RFC_OK Then

            RfcInvoke hRFC, hFunc, RfcErrorInfo
            If RfcErrorInfo.code = RFC_OK Then

              RfcGetStructure hFunc, "RFCSI_EXPORT", @hStruct, _
              If RfcErrorInfo.code = RFC_OK Then

                RfcGetChars hStruct, "RFCHOST", @SAPHost, 8, _
                RfcGetChars hStruct, "RFCSYSID", @SAPSysID, 8, _
                RfcGetChars hStruct, "RFCDBHOST", @SAPDBHost, 32, _

                RfcGetChars hStruct, "RFCDBSYS", @SAPDBSys, 10, _

                Print "<h1>FM RFC_SYSTEM_INFO</h1>"
                Print "Host: " & SAPHost
                Print "<br />"
                Print "SysID: " & SAPSysID
                Print "<br />"
                Print "DBHost: " & SAPDBHost
                Print "<br />"
                Print "DBSys: " & SAPDBSys
                Print "<p>"

              End If

            End If

            RfcDestroyFunction hFunc, RfcErrorInfo

          End If

        End If

        RfcCloseConnection hRFC, RfcErrorInfo

      End If

    End If


As you can see, is there no big difference between this code and that code. Only the CGI initialization, the input from the HTML page and the output via console print command.

Now we take a look at the HTML code


    <title>Test form for SAP RFC call via CGI</title>


    <form action="0104_SysInfo_CGI.exe" method="post">

      ASHost: <input type="text" name="ASHOST"><br />
      SysNr: <input type="text" name="SYSNR"><br />
      Client: <input type="text" name="CLIENT"><br />
      User: <input type="text" name="USER"><br />
      Password: <input type="password" name="PASSWD"><br />

      Language:<select name="LANG">
      <br />

      <input type="submit" name="sub" value="Ok">




Only one form, the different input fields and a button to submit the request.


If I press the button I get the following expected result.


As you can see is it with CGI and FreeBASIC very easy to implement SAP RFC library in a context of a web server.



P.S. You can find Part 1 here, Part 2 here and Part 3 here.

Assigned Tags

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