Example FRC
Example FRC
Applies to:
Any integration with external applications that can use the RFC API component structures such as C++ programs or another platform, working as a data server to SAP R/3 developments. For more information, visit the ABAP homepage.
Summary
This article motivates you to understand how simple is connecting external applications such as libraries or executables to serve SAP R/3 applications where the SAP cant do their job without a help. This article will guide you as an educational way to tell about all mainly steps required to creating interesting integrations using C++ applications or another platform as you well wish. Here you can find out how you can generate simple codes in C++ to compile and build external RFC Server applications that will be reused by SAP R/3 internal developments module functions as example. Author: Marcos Werneck Diniz
Author Bio
Marcos Werneck Diniz is a SAP R/3 consultant since 2002, working as a Material Management and Warehouse Management consultant in a large number of projects in many different sectors (industry (pharma), retail, aerospace, etc.) Currently he is a consultant of IBM do Brasil, advising the Material Management and Warehouse Management projects.
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
Table of Contents
Introduction .........................................................................................................................................................3 An example to guide you.................................................................................................................................3 Creating the Sample Solution .............................................................................................................................4 Main Program (ABAP).....................................................................................................................................4 Creating the Internal Function ZFUNCTION_RFC .........................................................................................6 Creating a C++ RFC Server Application .........................................................................................................6 SM59 Adjusting Settings to SAP R/3 Recognize the C++ Application ......................................................10 Testing the whole solution.........................................................................................................................12 Disclaimer and Liability Notice..........................................................................................................................14
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
Introduction
Ill assume that you have some expertise about SAP R/3 programming concepts, for instance: module functions, internal and external RFCs, function parameters and etc. This introduction will show the way of external RFC Server Applications can be used to help you at some moments where SAP R/3 conventional development can really do the work as you want. For examples, external integrations with data collectors, external equipments and some other situations which remain a comprehensive technical hard work. In these situations, maybe the best way to give a simple and good solution is using external components created by another language (C++, Visual Basic, etc.).
Note: This integration will manage the following steps: #1 a SAP R/3 application calling a module function; #2 a module function handling an external RFC call; #3 SM59 appropriately settings to connect the C++ application (local machine) and #4 the C++ application in details.
After this little introduction, Ill let you understand a simple example of each part of the solution.
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
Here you can find a preview of all codes and settings that will be maintained after this article:
DATA: myresult
DATA: msg_text1(80) TYPE c, "Message text msg_text2(80) TYPE c. "Message text DATA: field1 TYPE STRING.
PARAMETERS: p_field1(10) type c. field1 = p_field1. CALL FUNCTION 'ZFUNCTION_RFC' DESTINATION 'MYSERVER' EXPORTING question = p_field1
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
IF sy-subrc NE 0. WRITE 'An internal error ocurred in RFC call...'. ELSE. WRITE 'The result of C++ application is... '. WRITE myresult. ENDIF.
The main structure of this program is called through the command CALL FUNCTION. In this example, ZFUNCTION_RFC is a reference function which will process the IMPORTING and EXPORTING parameters. Note that this function must receive the same nomenclature of internal C++ function but Ill explain it better ahead. DESTINANTION MYSERVER is used to link the external executable with SAP R/3 through SM59 settings its required to ABAP code understand where is the local C++ application. If you take a look in this program youll see a simple structure when a parameter p_field1 is sent do RFC call through question attribute and the function returned a value in myanswer parameter (IMPORTING). Here you can check the screen interface of this simple function
Note: This interface is simple, just to illustrate this example! And Im not an ABAP programmer
If we try to execute this function right now without any other implementation our result will be something like this:
Now lets forward to the next step the internal ABAP function.
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
Creating the Internal Function ZFUNCTION_RFC As I told you, this function will be used to take care of the internal parameters processed by the main ABAP program show in our last step. Basically we will create an empty function as this example below:
FUNCTION ZFUNCTION_RFC. *"-------------------------------------------------------------------*"*"Interface local: *" IMPORTING *" VALUE(I_VALUE1) TYPE STRING *" EXPORTING *" VALUE(E_RESULT) TYPE STRING *"--------------------------------------------------------------------
ENDFUNCTION.
As you could see, this function processes only the parameters of the C++ application (internal functions) working as local interface between our ABAP program and the external C++ function. Here is extremely important be aware of the nomenclature of these programs and functions (C++) they must have the same name! Creating a C++ RFC Server Application Now our simple solution is almost finishing since we have to create a simple (?!) C++ application which will support the RFC Server function every time the ABAP program call them. At this moment you must consider the utilization of the RFC SDK from SAP R/3. This package will give you as well other examples, the correct includes, libraries and the file librfc32.dll required to connect this application to SAP R/3. Here you can get the sample C++ application:
// RFCSERVER.CPP : Sample RFC Server C++ application // Author: Marcos Werneck, [email protected] // 12.12.2008 // // required includes - please check the RFC SDK in order to use the correct files // #include "stdafx.h" #include "stdio.h" #include "string.h" #include "time.h" #include "c:\rfcsdk\include\srfcserv.h" #include "c:\rfcsdk\include\saprfc.h" #include "c:\rfcsdk\include\sapitab.h"
int mainU (int argc, rfc_char_t **argv) { RFC_RC remote_teste (RFC_HANDLE handle); */ char * remote_teste_docu (void); RFC_RC install ( RFC_HANDLE handle ); RFC_HANDLE handle;
/* RFC handle for internal function /* n/a */ /* RFC installation handle */ /* RFC general handle */
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
if (rc != RFC_OK) /* Install processing routine */ { RfcAbort(handle,"Initialization error"); return(1); } do { rc = RfcDispatch(handle); */ } while (rc == RFC_OK); RfcClose(handle); return(0); } /***************************************** * remote_teste() * internal function used by C++ application *******************************************/ RFC_RC remote_teste (RFC_HANDLE handle) { RFC_RC rc; RFC_PARAMETER parameters[4]; RFC_TABLE tables[2]; /* Close RFC handle - end of the application */ /* Wait RFC calls from ABAP program
/* SET rc = RFC_RC */ /* Parameters of RFC FUNCTION */ /* Table structure of RFC FUNCTION - not used by this example */
memset(¶meters[0], 0, sizeofR(parameters)); // EXPORTING PARAMETER parameters[0].name = cU("QUESTION"); parameters[0].nlen = strlenU ((rfc_char_t*) parameters[0].name); parameters[0].addr = &questions; parameters[0].leng = 0; parameters[0].type = RFCTYPE_STRING; parameters[1].name = NULL; tables[0].name = NULL; rc = RfcGetData( handle, parameters, tables); /* Receive data from ABAP RFC CALL... */ if (rc != RFC_OK) return (rc); answer = RfcAllocString (12); strcpy ((char*) answer, (char*) questions);
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
// IMPORTING PARAMETER parameters[0].name = cU("MYANSWER"); parameters[0].nlen = strlenU ((rfc_char_t *)parameters[0].name); parameters[0].addr = &answer; parameters[0].leng = strlen ((char*) answer); parameters[0].type = RFCTYPE_STRING; parameters[1].name = NULL; tables[0].name = NULL; rc = RfcSendData(handle, parameters, tables); /* Send data to ABAP RFC CALL */ return(rc); /* end of remote_teste()
*/
/**************************************** * remote_uname_docu() * * this function supplies a documentation ********************************************/ char * remote_teste_docu(void) { static char docu[] =
"RFC_TESTE is a test program that executes a uname command \n" " on the called Unix system.\n" " \n" " A 250 Character buffer is passed to this function (and ignored) \n" " a 250 character buffer is returned from this function \n" " the returned data is a text string \n" " \n Garth Kennedy 1 Dec 1994 \n" ; return (docu); /* end of remote_uname_docu() */
/**************************************** * install() * Install functions -> SAP R/3 ****************************************/ RFC_RC install(RFC_HANDLE handle) { RFC_RC rc; rc = RfcInstallFunctionExt(handle, "ZFUNCTION_RFC", external (ABAP) function */ (RFC_ONCALL)remote_teste, function */ remote_teste_docu() ); DOCUMENTATION **NOT USED** */ if (rc != RFC_OK) return rc; /* internal and /* internal C++ /* MS-DOS
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
*/
As you can see, we have a lot of internal function that enables the RFC CALL processing with external programs in this case, a C++ application. Most of the C++ programmers can easily understand this code and modify it to use again in another solutions. Here the main point is show how you can use the common functions and procedures. Here you can find a simple context of this solution:
http://help.sap.com/saphelp_nw04/helpdata/en/22/04299d488911d189490000e829fbbd/frameset.htm
The main functions that you will always use are: RFCACCEPT RFCINSTALLFUNCTION RFCDISPATCH RFCCLOSE RFCGETDATA RFCSENDDATA
There is a lot of information available at http://help.sap.com/ and http://www.sdn.sap.com/. Some other specialized ABAP portals have useful information as well. But sometime the research is very hard to discover samples. Some of C++ samples are very complex and some times very hard to understand the whole function. I hope you use this small sample to understand the basic functions.
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
SM59 Adjusting Settings to SAP R/3 Recognize the C++ Application Our last step for this solution, is create a entry in SM59 just to link a internal identification to this C++ program. Here is the main screen of SM59 Display and maintain RFC destinations
Note: There are some specific settings that you can manage at this transaction, but for our sample, well use the TCP/IP connections.
When you create a new entry for TCP/IP connections the following screen must be filled:
After, the second stage is defining the activation type in this sample the C++ application will be executed from a local directory.
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
Note: After these settings, you can save the information and be able to test connection in the same transaction.
Its not required create the program entry with extensions such as .dll or .exe If have done all settings correctly, the test connection must appear as below:
Now you can test your RFC Server through SAP program
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
Testing the whole solution Now choose your ABAP program in our sample ZMAIN_ABAP and execute it as required in our specification pass one parameter and receive the same value as an IMPORTING parameter.
The code in C++ application that makes this result for us is listed below (highlighted):
// EXPORTING PARAMETER parameters[0].name = cU("QUESTION"); parameters[0].nlen = strlenU ((rfc_char_t*) parameters[0].name); parameters[0].addr = &questions; parameters[0].leng = 0; parameters[0].type = RFCTYPE_STRING; parameters[1].name = NULL; tables[0].name = NULL; rc = RfcGetData( handle, parameters, tables); /* Receive data from ABAP RFC CALL... */ if (rc != RFC_OK) return (rc); answer = RfcAllocString (12); strcpy ((char*) answer, (char*) questions); // IMPORTING PARAMETER parameters[0].name = cU("MYANSWER"); parameters[0].nlen = strlenU ((rfc_char_t *)parameters[0].name); parameters[0].addr = &answer; parameters[0].leng = strlen ((char*) answer); parameters[0].type = RFCTYPE_STRING; parameters[1].name = NULL; tables[0].name = NULL; rc = RfcSendData(handle, parameters, tables); /* Send data to ABAP RFC CALL */
I hope with this simple example, you can be able to research more options and improvements to implement solid solutions.
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration
Related Content RFC Series Part 1: Mining R/3 with the RFCSDK - What is RFC? Consuming RFC Function Module Using Guided Procedures The RFC API For more information, visit the ABAP homepage.
Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration