0% found this document useful (0 votes)
28 views

Example FRC

This article discusses how to create external RFC components in C++ that can be used by SAP R/3 applications. It provides steps to build a sample solution that calls a C++ application from ABAP code using the RFC API. The sample demonstrates exporting data from ABAP, calling a C++ function, and importing the result back to ABAP.

Uploaded by

Olivier Huet
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Example FRC

This article discusses how to create external RFC components in C++ that can be used by SAP R/3 applications. It provides steps to build a sample solution that calls a C++ application from ABAP code using the RFC API. The sample demonstrates exporting data from ABAP, calling a C++ function, and importing the result back to ABAP.

Uploaded by

Olivier Huet
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration

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

Company: IBM do Brasil Created on: 08 December 2008

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.

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 1

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

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 2

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.).

An example to guide you


In this article Ill assume you are in touch of a few technical terms used in this document BAPI, module functions, z programs, BADI, tables, external and internal RFCs, etc. Imagine you are already working with these technologies and you are going to make a next step in direction of this new integration C++ applications serving ABAP programs. If you are familiar with these terms, please go ahead if not, let me point to some related documentation that you can easy understand this concept: Classical SAP Technologies (ABAP) help.sap.com Introduction to RFC Server Programs help.sap.com Introduction of the RFC API help.sap.com An extra documentation or articles can be found using simple search keywords at https://www.sdn.sap.com Come back to our article, Ill exemplify a simple scenario as you can see at the picture below:

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.

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 3

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:

Creating the Sample Solution


In order to keep your focus in whole solution I decided to compose the sample solution step-by-step starting by the ABAP program that will control the RFC Server application and its return. Main Program (ABAP) The program show here is extremely simple. Itll send a parameter and receive another one in this case de C++ application will receive this information and send back to the ABAP program. Please, make sure that you understand this simple solution, in order to create special processes into C++ application such as: database requests, legacy integrations, equipment integrations, etc. Lets understand the basic of this program:
*&---------------------------------------------------------------------* *& Report ZMAIN_ABAP * *& * *&---------------------------------------------------------------------* *& Simple program to call RFC external functions * *& * *&---------------------------------------------------------------------* REPORT zmain_abap. TYPE STRING. .

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

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 4

Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration

IMPORTING myanswer = myresult EXCEPTIONS communication_failure = 1 system_failure = 2

MESSAGE msg_text1 MESSAGE msg_text2.

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:

Note: A message error handled by the ABAP program.

Now lets forward to the next step the internal ABAP function.

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 5

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 */

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 6

Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration

RFC_RC rc; handle = RfcAccept(argv); rc = install(handle); functions */

/* SET rc = RFC_RC */ /* Parameters processing handle */ /* Call to install internal

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(&parameters[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);

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 7

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[] =

<-- NOT USED BY THIS SAMPLE...

"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

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 8

Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration

return RFC_OK; /* end of install()

*/

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:

Note: This picture can be found at

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.

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 9

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.

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 10

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

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 11

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.

After the program execution we have

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.

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 12

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.

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 13

Creating External RFC Components in C++ (RFC API) and the SAP R/3 Integration

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

SAP COMMUNITY NETWORK 2008 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 14

You might also like