Calling a Function in SAP Systems 
In the following example program, an external RFC client connects to an SAP system and calls the function module BAPI_COMPANY_GETDETAIL:
Syntax
#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);
exit(1);
}
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;
-----------------------------------------------
/*OPEN CONNECTION*/
-----------------------------------------------
/*Create logon parameter list*/
loginParams[0].name = cU("ashost"); loginParams[0].value =cU("hs0023"); loginParams[1].name = cU("sysnr"); loginParams[1].value = cU("05"); loginParams[2].name = cU("client"); loginParams[2].value =cU("800"); loginParams[3].name = cU("user"); loginParams[3].value =cU("alice"); loginParams[4].name = cU("lang"); loginParams[4].value =cU("EN"); loginParams[5].name = cU("passwd"); loginParams[5].value =cU("secret");/*Open connection*/
connection = RfcOpenConnection(loginParams, 6, );
if (connection == NULL) errorHandling(rc, cU("Error during logon"),, NULL);
-----------------------------------------------
/* DYNAMIC METADATA retrieval (can also be executed using STATIC functions)*/
-----------------------------------------------
bapiCompanyDesc = RfcGetFunctionDesc(connection,
cU("BAPI_COMPANY_GETDETAIL"), ); if (bapiCompanyDesc == NULL) errorHandling(rc, cU("Error duringmetadata lookup"), , connection);
-----------------------------------------------
/*FUNCTION CALL/*
-----------------------------------------------
/*Create function instance*/
bapiCompany = RfcCreateFunction(bapiCompanyDesc, );
/*Parameter setting*/
RfcSetChars(bapiCompany, cU("COMPANYID"), cU("000007"), 6,);
/*Call*/
rc = RfcInvoke(connection, bapiCompany, );
if (rc != RFC_OK) errorHandling(rc, cU("Error callingBAPI_COMPANY_GETDETAIL"), , connection);
/*Getting parameters*/
RfcGetStructure(bapiCompany, cU("RETURN"), ,);
RfcGetString(returnStructure, cU("MESSAGE"), message, 221,, );
/*After having finished the procedure: release memory only if a ‘create’ function was invoked before*/
RfcDestroyFunction(bapiCompany, );
/*Final UTF8-UTF16 conversion*/
windows.h utf8Len = WideCharToMultiByte(CP_UTF8, 0, message,
strlenU(message), buffer, 1105, NULL, NULL);
RfcSAPUCToUTF8(message, strlenU(message), buffer, ,
, );
outFile = fopen("message.xml", "w"); fputs("<?xml version=\"1.0\"?>\n<message>", outFile);fputs(buffer, outFile);
fputs("</message>", outFile);fclose(outFile);
return 0;
}