Hello community,
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.
'-Begin----------------------------------------------------------------- '-Includes------------------------------------------------------------ #Include Once "cgi-util.bi" #Include Once "sapnwrfc.inc" '-Variables----------------------------------------------------------- Dim RfcErrorInfo As RFC_ERROR_INFO Dim connParams(6) AS RFC_CONNECTION_PARAMETER 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 '-Macros-------------------------------------------------------------- #Macro RfcErrorHandlerCGI() If RfcErrorInfo.code <> RFC_OK Then Print "<h1>RFC Error Message</h1><br>" Print RfcErrorInfo.message End If #EndMacro '-Main---------------------------------------------------------------- res = cgi_init() Print !"Content-type: text/html\n\n" If res <> CGIERR_NONE Then Print "Error " & Str(res) & " " & Str(cgi_strerror(res)) & "<p>" Else 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) RfcErrorHandlerCGI() If hRFC <> 0 And RfcErrorInfo.code = RFC_OK Then hFuncDesc = RfcGetFunctionDesc(hRFC, "RFC_SYSTEM_INFO", _ RfcErrorInfo) RfcErrorHandlerCGI() If hFuncDesc <> 0 And RfcErrorInfo.code = RFC_OK Then hFunc = RfcCreateFunction(hFuncDesc, RfcErrorInfo) RfcErrorHandlerCGI() If hFunc <> 0 And RfcErrorInfo.code = RFC_OK Then RfcInvoke hRFC, hFunc, RfcErrorInfo RfcErrorHandlerCGI() If RfcErrorInfo.code = RFC_OK Then RfcGetStructure hFunc, "RFCSI_EXPORT", @hStruct, _ RfcErrorInfo RfcErrorHandlerCGI() If RfcErrorInfo.code = RFC_OK Then RfcGetChars hStruct, "RFCHOST", @SAPHost, 8, _ RfcErrorInfo RfcGetChars hStruct, "RFCSYSID", @SAPSysID, 8, _ RfcErrorInfo RfcGetChars hStruct, "RFCDBHOST", @SAPDBHost, 32, _ RfcErrorInfo RfcGetChars hStruct, "RFCDBSYS", @SAPDBSys, 10, _ RfcErrorInfo 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 RfcErrorHandlerCGI() End If End If RfcCloseConnection hRFC, RfcErrorInfo RfcErrorHandlerCGI() End If cgi_quit() End If '-End-------------------------------------------------------------------
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
<html> <head> <title>Test form for SAP RFC call via CGI</title> </head> <body> <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"> <option>EN</option> <option>DE</option> </select> <br /> <input type="submit" name="sub" value="Ok"> </form> </body></html>
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.
Cheers
Stefan