creating external rfc components in c%2b%2b (rfc api) and the sap r_3 integration

14
8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 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 can’t 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 SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2008 SAP AG 1

Upload: ceduardo68594

Post on 03-Jun-2018

227 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 1/14

 

Creating External RFCComponents in C++ (RFC API) andthe SAP R/3 Integration

 Appl ies 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 orexecutables to serve SAP R/3 applications where the SAP can’t do their job without a help.

This article will guide you as an educational way to tell about all mainly steps required to creating interestingintegrations 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 Serverapplications 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 manydifferent sectors (industry (pharma), retail, aerospace, etc.)

Currently he is a consultant of IBM do Brasil, advising the Material Management andWarehouse Management projects.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 1

Page 2: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 2/14

  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 SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 2

Page 3: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 3/14

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

Introduction

I’ll assume that you have some expertise about SAP R/3 programming concepts, for instance: modulefunctions, 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 somemoments 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 acomprehensive 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 I’ll assume you are in touch of a few technical terms used in this document – BAPI, modulefunctions, 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 directionof 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 thisconcept:

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, I’ll 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 modulefunction handling an external RFC call; #3 SM59 appropriately settings to connect the C++ application (localmachine) and #4 the C++ application in details.

 After this little introduction, I’ll let you understand a simple example of each part of the solution.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 3

Page 4: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 4/14

  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 startingby the ABAP program that will control the RFC Server application and its return.

Main Program (ABAP)

The program show here is extremely simple. It’ll send a parameter and receive another one – in this case deC++ 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.

Let’s understand the basic of this program:

*&---------------------------------------------------------------------*

*& Report ZMAIN_ABAP *

*& **&---------------------------------------------------------------------*

*& Simple program to call RFC external functions *

*& *

*&---------------------------------------------------------------------*

REPORT zmain_abap. .

DATA: myresult TYPE STRING.

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 SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 4

Page 5: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 5/14

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

IMPORTING

myanswer = myresult

EXCEPTIONS

communication_failure = 1 MESSAGE msg_text1

system_failure = 2 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 I’ll explain it betterahead.

DESTINANTION ‘MYSERVER’ is used to link the external executable with SAP R/3 through SM59 settings –it’s required to ABAP code understand where is the local C++ application.

If you take a look in this program you’ll see a simple structure when a parameter ‘p_field1’ is sent do RFCcall 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 I’m not an ABAP programmer…

If we try to execute this function right now – without any other implementation – our result will be somethinglike this:

Note: A message error handled by the ABAP program.

Now let’s forward to the next step… the internal ABAP function.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 5

Page 6: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 6/14

  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 ABAPprogram 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++) – theymust 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 willsupport 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 youas well other examples, the correct includes, libraries and the file librfc32.dll – required to connect thisapplication 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); /* RFC handle for internal function

*/

char * remote_teste_docu (void); /* n/a */

RFC_RC install ( RFC_HANDLE handle ); /* RFC installation handle */

RFC_HANDLE handle; /* RFC general handle */

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 6

Page 7: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 7/14

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

RFC_RC rc; /* SET rc = RFC_RC */

handle = RfcAccept(argv); /* Parameters processing handle */

rc = install(handle); /* Call to install internal

functions */

if (rc != RFC_OK) /* Install processing routine */

{

RfcAbort(handle,"Initialization error");

return(1);

}

do 

{

rc = RfcDispatch(handle); /* Wait RFC calls from ABAP program

*/

} while (rc == RFC_OK);

RfcClose(handle); /* Close RFC handle - end of the

application */

return(0);}

/*****************************************

* remote_teste()

* internal function used by C++ application

*******************************************/

RFC_RC remote_teste (RFC_HANDLE handle)

{

RFC_RC rc; /* SET rc = RFC_RC */

RFC_PARAMETER parameters[4]; /* Parameters of RFC FUNCTION */

RFC_TABLE tables[2]; /* 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 SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 7

Page 8: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 8/14

  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 <-- NOT USED BY THIS SAMPLE...

********************************************/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", /* internal and

external (ABAP) function */

(RFC_ONCALL)remote_teste, /* internal C++

function */

remote_teste_docu() ); /* MS-DOS

DOCUMENTATION **NOT USED** */

if (rc != RFC_OK)

return rc;

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 8

Page 9: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 9/14

  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 externalprograms – in this case, a C++ application.

Most of the C++ programmers can easily understand this code and modify it to use again in anothersolutions. 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 athttp://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 otherspecialized ABAP portals have useful information as well. But sometime the research is very hard to discoversamples.

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 SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 9

Page 10: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 10/14

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

SM59 – Adjusting Settings to SAP R/3 Recognize the C++ Appl ication

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, we’ll use the TCP/IPconnections.

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 executedfrom a local directory.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 10

Page 11: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 11/14

  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.

It’s 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 SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 11

Page 12: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 12/14

  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 ourspecification – 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 implementsolid solutions.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 12

Page 13: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 13/14

  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 SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

© 2008 SAP AG 13

Page 14: Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

8/12/2019 Creating External RFC Components in C%2b%2b (RFC API) and the SAP R_3 Integration

http://slidepdf.com/reader/full/creating-external-rfc-components-in-c2b2b-rfc-api-and-the-sap-r3-integration 14/14

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

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com

Disclaimer and Liabili ty Notice

This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is notsupported 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 orcode sample, including any liability resulting from incompatibility between the content within this document and the materials andservices offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of thisdocument.