Hello community,

 

FreeBASIC is a powerful free and open source BASIC compiler for Windows and Linux. You find the official site here and the very good German site here. The actual version 0.90.1 can only create 32-bit applications, but the new beta release 0.91 can also create 64-bit applications – look here (in German). FreeBASIC is an ideal partner to SAP NetWeaver RFC SDK, because it is very easy to use SAP NetWeaver RFC SDK with FreeBASIC. This is the reason for this tutorial.

We start with an include file called sapnwrfc.inc. It collects constants, structures and declarations:

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

  '-Includes------------------------------------------------------------
    #Include Once "windows.bi"

  '-Constants-----------------------------------------------------------
    Enum
      RFC_OK = 0
      RFC_RETRY = 14
    End Enum

  '-Structures----------------------------------------------------------
  
    '-RFC_ERROR_INFO----------------------------------------------------
      Type RFC_ERROR_INFO
        code As Integer
        group As Integer
        key As WString * 128
        message As WString * 512
        abapMsgClass As WString * 21
        abapMsgType As WString * 2
        abapMsgNumber As WString * 4
        abapMsgV1 As WString * 51
        abapMsgV2 As WString * 51
        abapMsgV3 As WString * 51
        abapMsgV4 As WString * 51
      End Type

    '-RFC_CONNECTION_PARAMETER------------------------------------------
      Type RFC_CONNECTION_PARAMETER
        name As WString Ptr
        value As WString Ptr
      End Type

  '-Declarations--------------------------------------------------------
    Declare Function RfcCloseConnection StdCall Lib "sapnwrfc" _
      Alias "RfcCloseConnection" (ByVal rfcHandle As Integer, _
      errorInfo As RFC_ERROR_INFO) As Integer

    Declare Function RfcCreateFunction StdCall Lib "sapnwrfc" _
      Alias "RfcCreateFunction" (ByVal funcDescHandle As Integer, _
      errorInfo As RFC_ERROR_INFO) As Integer

    Declare Function RfcCreateFunctionDesc StdCall Lib "sapnwrfc" _
      Alias "RfcCreateFunctionDesc" (name As WString, _
      errorInfo As RFC_ERROR_INFO) As Integer

    Declare Function RfcGetChars StdCall Lib "sapnwrfc" _
      Alias "RfcGetChars" (ByVal dataHandle As Integer, _
      name As WString, ByVal charBuffer As WString Ptr, _
      bufferLength As Integer, errorInfo As RFC_ERROR_INFO) As Integer

    Declare Function RfcGetStructure StdCall Lib "sapnwrfc" _
      Alias "RfcGetStructure" (ByVal dataHandle As Integer, _
      name As WString, ByVal strucHandle As Integer, _
      errorInfo As RFC_ERROR_INFO) As Integer

    Declare Function RfcDestroyFunction StdCall Lib "sapnwrfc" _
      Alias "RfcDestroyFunction" (ByVal funcHandle As Integer, _
      errorInfo As RFC_ERROR_INFO) As Integer

    Declare Function RfcGetFunctionDesc StdCall Lib "sapnwrfc" _
      Alias "RfcGetFunctionDesc" (ByVal rfcHandle As Integer, _
      funcName As WString, errorInfo As RFC_ERROR_INFO) _
      As Integer

    Declare Function RfcGetVersion StdCall Lib "sapnwrfc" _
      Alias "RfcGetVersion" (ByVal MajorVersion As Long Ptr, _
      ByVal MinorVersion As Long Ptr, ByVal PatchLevel As Long Ptr) _
      As WString Ptr

    Declare Function RfcInstallServerFunction StdCall Lib "sapnwrfc" _
      Alias "RfcInstallServerFunction" (sysID As WString, _
      ByVal funcDescHandle As Integer, ByVal serverFunction As Integer, _
      errorInfo As RFC_ERROR_INFO) As Integer

    Declare Function RfcInvoke StdCall Lib "sapnwrfc" _
      Alias "RfcInvoke" (ByVal rfcHandle As Integer, _
      ByVal funcHandle As Integer, errorInfo As RFC_ERROR_INFO) _
      As Integer

    Declare Function RfcListenAndDispatch StdCall Lib "sapnwrfc" _
      Alias "RfcListenAndDispatch" (ByVal rfcHandle As Integer, _
      ByVal timeout As Integer, errorInfo As RFC_ERROR_INFO) _
      As Integer

    Declare Function RfcOpenConnection StdCall Lib "sapnwrfc" _
      Alias "RfcOpenConnection" (ByVal connectionParams As Integer, _
      ByVal paramCount As Integer, errorInfo As RFC_ERROR_INFO) _
      As Integer

    Declare Function RfcPing StdCall Lib "sapnwrfc" _
      Alias "RfcPing" (ByVal rfcHandle As Integer, _
      errorInfo As RFC_ERROR_INFO) As Integer

    Declare Function RfcRegisterServer StdCall Lib "sapnwrfc" _
      Alias "RfcRegisterServer" (ByVal connectionParams As Integer, _
      ByVal paramCount As Integer, errorInfo As RFC_ERROR_INFO) _
      As Integer

  '-Macros--------------------------------------------------------------
    #Macro RfcErrorHandler()
      If RfcErrorInfo.code <> RFC_OK Then
        MessageBox(null, RfcErrorInfo.message, "RFC Error Message", _
          MB_OK Or MB_ICONERROR)
      End If
    #EndMacro

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

Not all functions of the SAP NetWeaver RFC library are declared, only the functions we needed for this tutorial. Store this file, that are all preparations.

 

Now we take a look at our first FreeBASIC program:

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

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

  '-Variables-----------------------------------------------------------
    Dim RfcErrorInfo As RFC_ERROR_INFO
    Dim connParams(6) AS RFC_CONNECTION_PARAMETER
    Dim hRFC As Integer
    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

      MessageBox(null, "Check connection with TAC SMGW in the " & _
        "SAP system", "", MB_OK Or MB_ICONINFORMATION)

      RfcCloseConnection hRFC, RfcErrorInfo
      RfcErrorHandler()

    End If

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

 

It is very easy. In the first block we define the connection parameter and then we open a connection to our SAP system via the function RFCOpenConnection. The program stops with a message box and in our SAP system we find, with the TAC SMGW, our connected client application.

 

0101_SMGW.jpg

 

After closing the message box we close the connection via the function RFCCloseConnection.

As you can see, it is very easy to code a client application which connects an SAP system.

Cheers
Stefan

 

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

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