Technical Articles
How to use RFC-SDK with FreeBASIC – Part 2: Client Apps (FreeBASIC uses SAP)
In the 2nd part now a few examples of SAP NetWeaver client applications.
We start with a simple ping, to demonstrate how to call an ABAP function module from FreeBASIC.
'-Begin-----------------------------------------------------------------
'-Includes------------------------------------------------------------
#Include Once "sapnwrfc.inc"
'-Variables-----------------------------------------------------------
Dim RfcErrorInfo As RFC_ERROR_INFO
Dim connParams(6) AS RFC_CONNECTION_PARAMETER
Dim As Integer hRFC, hFuncDesc, hFunc
Dim As WString * 16 nASHost, nSysNr, nClient, nUser, nPassWd, nLang
Dim As WString * 16 vASHost, vSysNr, vClient, vUser, vPassWd, vLang
'-Main----------------------------------------------------------------
nASHost = "ASHOST" : vASHost = "ABAP"
nSysNr = "SYSNR" : vSysNr = "00"
nClient = "CLIENT" : vClient = "001"
nUser = "USER" : vUser = "BCUSER"
nPassWd = "PASSWD" : vPassWd = "minisap"
nLang = "LANG" : vLang = "EN"
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), 6, RfcErrorInfo)
RfcErrorHandler()
If hRFC <> 0 And RfcErrorInfo.code = RFC_OK Then
'-Variant 1-------------------------------------------------------
'-
'- Do not use lowercase characters in FM name,
'- use only uppercase characters
'-
'-----------------------------------------------------------------
hFuncDesc = RfcGetFunctionDesc(hRFC, "RFC_PING", RfcErrorInfo)
RfcErrorHandler()
If hFuncDesc <> 0 And RfcErrorInfo.code = RFC_OK Then
hFunc = RfcCreateFunction(hFuncDesc, RfcErrorInfo)
RfcErrorHandler()
If hFunc <> 0 And RfcErrorInfo.code = RFC_OK Then
RfcInvoke hRFC, hFunc, RfcErrorInfo
RfcErrorHandler()
If RfcErrorInfo.code = RFC_OK Then
MessageBox(null, "Ping successful", "FM RFC_PING", _
MB_OK Or MB_ICONINFORMATION)
End If
RfcDestroyFunction hFunc, RfcErrorInfo
RfcErrorHandler()
End If
End If
'-Variant 2-------------------------------------------------------
RfcPing hRFC, RfcErrorInfo
RfcErrorHandler()
If RfcErrorInfo.code = RFC_OK Then
MessageBox(null, "Ping successful", "Function RfcPing", _
MB_OK Or MB_ICONINFORMATION)
End If
RfcCloseConnection hRFC, RfcErrorInfo
RfcErrorHandler()
End If
'-End-------------------------------------------------------------------
We open the connection and get a function description from the function module RFC_PING via RfcGetFunctionDesc. With this function description we create the function via RfcCreateFunction in the context of our program and execute it with RfcInvoke. If RfcInvoke is successful we get a message box. Last but not least we destroy the function with RfcDestroyFunction.
The 2nd variant is only an example how to use the function RfcPing from the SAP NetWeaver RFC library.
Our next example is the using of the function module RFC_SYSTEM_INFO. We use the same way as above, but after the invoke we read the result
of the function module. The result is in the structure RFCSI_EXPORT, we get it with RfcGetStructure and the entries with RfcGetChars.
'-Begin-----------------------------------------------------------------
'-Includes------------------------------------------------------------
#Include Once "sapnwrfc.inc"
'-Variables-----------------------------------------------------------
Dim RfcErrorInfo As RFC_ERROR_INFO
Dim connParams(8) AS RFC_CONNECTION_PARAMETER
Dim As Integer hRFC, hFuncDesc, hFunc, hStruct
Dim As WString * 16 nASHost, nSysNr, nClient, nUser, nPassWd, _
nLang, nUseSAPGUI, nTrace
Dim As WString * 16 vASHost, vSysNr, vClient, vUser, vPassWd, _
vLang, vUseSAPGUI, vTrace
Dim As WString * 9 SAPHost, SAPSysID
Dim As WString * 11 SAPDBSys
Dim As WString * 33 SAPDBHost
'-Main----------------------------------------------------------------
nASHost = "ASHOST" : vASHost = "ABAP"
nSysNr = "SYSNR" : vSysNr = "00"
nClient = "CLIENT" : vClient = "001"
nUser = "USER" : vUser = "BCUSER"
nPassWd = "PASSWD" : vPassWd = "minisap"
nLang = "LANG" : vLang = "EN"
nUseSAPGUI = "USE_SAPGUI" : vUseSAPGUI = "0"
nTrace = "TRACE" : vTrace = "0"
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
connParams(6).name = @nUseSAPGUI : connParams(6).value = @vUseSAPGUI
connParams(7).name = @nTrace : connParams(7).value = @vTrace
hRFC = RfcOpenConnection(@connParams(0), 8, RfcErrorInfo)
RfcErrorHandler()
If hRFC <> 0 And RfcErrorInfo.code = RFC_OK Then
hFuncDesc = RfcGetFunctionDesc(hRFC, "RFC_SYSTEM_INFO", _
RfcErrorInfo)
RfcErrorHandler()
If hFuncDesc <> 0 And RfcErrorInfo.code = RFC_OK Then
hFunc = RfcCreateFunction(hFuncDesc, RfcErrorInfo)
RfcErrorHandler()
If hFunc <> 0 And RfcErrorInfo.code = RFC_OK Then
RfcInvoke hRFC, hFunc, RfcErrorInfo
RfcErrorHandler()
If RfcErrorInfo.code = RFC_OK Then
RfcGetStructure hFunc, "RFCSI_EXPORT", @hStruct, _
RfcErrorInfo
RfcErrorHandler()
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
MessageBox(null, "Host: " & SAPHost & Chr(13,10) & _
"SysID: " & SAPSysID & Chr(13,10) & _
"DBHost: " & SAPDBHost & Chr(13,10) & _
"DBSys: " & SAPDBSys, "FM RFC_SYSTEM_INFO", _
MB_OK Or MB_ICONINFORMATION)
End If
End If
RfcDestroyFunction hFunc, RfcErrorInfo
RfcErrorHandler()
End If
End If
RfcCloseConnection hRFC, RfcErrorInfo
RfcErrorHandler()
End If
'-End-------------------------------------------------------------------
The result is displayed in a message box.
As you can see, it is very easy to code a client application which uses function modules from an SAP system.
Hi,
awesome series of blog.. during some time I was keen to find a way to have ABAP consuming native code, because I can't squeeze performance in ABAP.
Kudos to you.
D.
Hello Daniel,
thanks for your reply
🙂
Cheers
Stefan