ABAP–C語言調用SAP的RFC的代碼範例

來源:互聯網
上載者:User

C語言代碼
#include <stdlib.h>
#include <stdio.h>
#include "sapnwrfc.h"

RFC_RC SAP_API stfc_connection_impl(RFC_CONNECTION_HANDLE rfcHandle, RFC_FUNCTION_HANDLE funcHandle, RFC_ERROR_INFO* errorInfo){
 RFC_RC rc = RFC_OK;
 SAP_UC requtext[256], buf[256];

 rc = RfcGetString(funcHandle, cU("REQUTEXT"), requtext, 256, NULL, errorInfo);
 printfU(cU("Got request for STFC_CONNECTION./nREQUTEXT = %s/n/n"), requtext);
 sprintfU(buf, cU("STFC_CONNECTION called with REQUTEXT = %s"), requtext);
 rc = RfcSetString(funcHandle, cU("ECHOTEXT"), buf, strlenU(buf), errorInfo);

 return rc;
}

int mainU(int argc, SAP_UC** argv){
 RFC_RC rc = RFC_OK;
 RFC_CONNECTION_PARAMETER loginParams[1];
 RFC_ERROR_INFO errorInfo;
 RFC_CONNECTION_HANDLE connection;
 RFC_FUNCTION_DESC_HANDLE z_perform_callback, stfc_connection;
 RFC_FUNCTION_HANDLE functionContainer;
 SAP_UC data[256];

 loginParams[0].name = cU("dest"); loginParams[0].value = cU("SPJ");

 connection = RfcOpenConnection(loginParams, 1, &errorInfo);
 if (connection == NULL){
  printfU(cU("Error during logon: %s/n"), errorInfo.message);
  printfU(cU("Please check that the sapnwrfc.ini file is in the current/nworking directory and the logon parameters are ok./n"));
  return 1;
 }

 /* Note: In the following all error handling is omitted for simplicity. */
 z_perform_callback = RfcGetFunctionDesc(connection, cU("Z_PERFORM_CALLBACK"), &errorInfo);
 stfc_connection = RfcGetFunctionDesc(connection, cU("STFC_CONNECTION"), &errorInfo);

 // The stfc_connection_impl function will be called from R/3 during the RfcInvoke step below!
 rc = RfcInstallServerFunction(NULL, stfc_connection, stfc_connection_impl, &errorInfo);

 functionContainer = RfcCreateFunction(z_perform_callback, &errorInfo);

 rc = RfcSetString(functionContainer, cU("INPUT_DATA"), cU("Original Request"), 16, &errorInfo);
 rc = RfcInvoke(connection, functionContainer, &errorInfo);
 rc = RfcGetString(functionContainer, cU("OUTPUT_DATA"), data, 256, NULL, &errorInfo);

 RfcCloseConnection(connection, NULL);

 printfU(cU("Response from Z_PERFORM_CALLBACK: OUTPUT_DATA = %s/n"), data);
 return 0;

ABAP代碼
FUNCTION z_perform_callback.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(INPUT_DATA) TYPE  CHAR255
*"  EXPORTING
*"     VALUE(OUTPUT_DATA) TYPE  CHAR255
*"----------------------------------------------------------------------

  DATA: error_message(120),
        request LIKE sy-lisel,
        echo LIKE sy-lisel.

  CONCATENATE 'Z_PERFORM_CALLBACK called with INPUT_DATA =' input_data
    INTO request SEPARATED BY space.

  CALL FUNCTION 'STFC_CONNECTION' DESTINATION 'BACK'
    EXPORTING
      requtext       = request
    IMPORTING
      echotext       = echo
*    RESPTEXT       =
    EXCEPTIONS
      system_failure = 1 MESSAGE error_message
      communication_failure = 2 MESSAGE error_message
            .

  IF sy-subrc NE 0.
    CONCATENATE 'Error during callback:' error_message INTO output_data
    SEPARATED BY space.
  ELSE.
    CONCATENATE 'Response from STFC_CONNECTION: ECHOTEXT =' echo INTO output_data
    SEPARATED BY space.
  ENDIF.

ENDFUNCTION.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.