Show TOC Start of Content Area

Procedure documentation Calling a Function in SAP Systems  Locate the document in its SAP Library structure


In the following example program an external RFC client connects to an SAP system and calls the function module BAPI_COMPANY_GETDETAIL:


#include <stdlib.h>

#include <stdio.h>

#include "sapnwrfc.h"


void errorHandling(RFC_RC rc, SAP_UC description[], RFC_ERROR_INFO*

errorInfo, RFC_CONNECTION_HANDLE connection){

      printfU(cU("%s: %d\n"), description, rc);

      printfU(cU("%s: %s\n"), errorInfo->key, errorInfo->message);


/* It's better to close the TCP/IP connection cleanly, than to

just let the backend get a "Connection reset by peer" error...*/


      if (connection != NULL) RfcCloseConnection(connection, errorInfo);





int mainU(int argc, SAP_UC** argv){

      RFC_RC rc = RFC_OK;

      RFC_CONNECTION_PARAMETER loginParams[6];

      RFC_ERROR_INFO errorInfo;

      RFC_CONNECTION_HANDLE connection;

      RFC_FUNCTION_DESC_HANDLE bapiCompanyDesc;

      RFC_FUNCTION_HANDLE bapiCompany;

      RFC_STRUCTURE_HANDLE returnStructure;

      SAP_UC message[221];

      RFC_BYTE buffer[1105];

      unsigned utf8Len = 1105, resultLen;

      FILE* outFile;






/*Create logon parameter list*/


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


      loginParams[1].name = cU("sysnr");    loginParams[1].value = cU("05");

      loginParams[2].name = cU("client");    loginParams[2].value =


      loginParams[3].name = cU("user");      loginParams[3].value =


      loginParams[4].name = cU("lang");     loginParams[4].value =


      loginParams[5].name = cU("passwd"); loginParams[5].value =



/*Open connection*/


      connection = RfcOpenConnection(loginParams, 6, &errorInfo);

      if (connection == NULL) errorHandling(rc, cU("Error during logon"),

&errorInfo, NULL);



/* DYNAMIC METADATA retrieval (can also be executed using STATIC functions)*/



      bapiCompanyDesc = RfcGetFunctionDesc(connection,


      if (bapiCompanyDesc == NULL) errorHandling(rc, cU("Error during

metadata lookup"), &errorInfo, connection);






/*Create function instance*/


bapiCompany = RfcCreateFunction(bapiCompanyDesc, &errorInfo);


      /*Parameter setting*/


      RfcSetChars(bapiCompany, cU("COMPANYID"), cU("000007"), 6,





      rc = RfcInvoke(connection, bapiCompany, &errorInfo);

      if (rc != RFC_OK) errorHandling(rc, cU("Error calling

BAPI_COMPANY_GETDETAIL"), &errorInfo, connection);


      /*Getting parameters*/


      RfcGetStructure(bapiCompany, cU("RETURN"), &returnStructure,


      RfcGetString(returnStructure, cU("MESSAGE"), message, 221,

&resultLen, &errorInfo);


      /*After having finished the procedure: release memory only if a ‘create’ function was invoked before*/


      RfcDestroyFunction(bapiCompany, &errorInfo);


      /*Final UTF8-UTF16 conversion*/


      windows.h utf8Len = WideCharToMultiByte(CP_UTF8, 0, message,

strlenU(message), buffer, 1105, NULL, NULL);

      RfcSAPUCToUTF8(message,  strlenU(message), buffer, &utf8Len,

&resultLen, &errorInfo);


      outFile = fopen("message.xml", "w");

      fputs("<?xml version=\"1.0\"?>\n<message>", outFile);

      fputs(buffer, outFile);

      fputs("</message>", outFile);


      return 0;



 Further Information


      Starting a SAP GUI


End of Content Area