Knowledge Base Nr: 00110 saprfc2.cpp - http://www.swe-kaiser.de

Downloads:

RFC: SAP Funktion über RFC aufrufen mit Rückgabewerten

  
//SAPGUI Client muss installiert sein

#include "saprfc.h"
#include "sapitab.h"

/* some limit for parameter to allow static allocation */
#define MAX_PARA 4
#define MAX_SIZE 100

typedef struct
{
char strPLNBEZ[MAX_SIZE];
char strGLTRS[MAX_SIZE];
char strGSTRS[MAX_SIZE];
char strGAMNG[MAX_SIZE];
char strMAKTX[MAX_SIZE];
char strFERTH[MAX_SIZE];
char strMATNR[MAX_SIZE];
char strWKZNR[MAX_SIZE];
} MYDATA;

MIRSAP_API int GetSAPData(char *strFANR,
char *strPLNBEZ,
char *strGLTRS,
char *strGSTRS,
char *strGAMNG,
char *strMAKTX,
char *strFERTH,
char *strMATNR,
char *strWKZNR,
char *strDummy1,
char *strDummy2)
{
RFC_HANDLE handle;
RFC_ERROR_INFO_EX error_info_ex;
rfc_char_t connect_string[] = "TYPE=3 ASHOST=153.95.24.7 SYSNR=06 CLIENT=002 USER=*** PASSWD=***";

// RFC Funktion
RFC_RC rfc_rc;
rfc_char_t exception[512];
rfc_char_t *except_ptr = exception;
rfc_char_t *ptr;
static RFC_PARAMETER importing[MAX_PARA],
exporting[MAX_PARA],
changing[MAX_PARA];
static RFC_TABLE tables[MAX_PARA];
RFC_STRING input;
ITAB_H itab_h;
RFC_BCD test;

int i;
int nLineLength;

MYDATA myData;

// RFC Verbindung öffnen und anmelden
handle = RfcOpenEx (connect_string, &error_info_ex);

if (handle == RFC_HANDLE_NULL)
{
return (-1);
}

// RFC Funktion "Z_RFC_FA_DATEN" aufrufen

importing[0].name = NULL;

input = RfcAllocString (200);

strcpy ((char *) input, strFANR);


exporting[0].name = "FANUMMER";
exporting[0].nlen = strlen ((char *) exporting[0].name);
exporting[0].type = RFCTYPE_STRING;
exporting[0].addr = &input;
exporting[0].leng = strlen((char *)input);

exporting[1].name = NULL;

// Tabelle initialisieren
itab_h = ItCreate ("Z_SG_FADATEN", 10000, 0 , 0);


tables[0].name = "Z_SG_FADATEN";
tables[0].nlen = strlen ((char *) tables[0].name );
tables[0].ithandle = itab_h;
tables[0].leng = 10000;
tables[0].type = RFCTYPE_CHAR;

rfc_rc = RfcCallReceive (handle,
"Z_RFC_FA_DATEN",
exporting,
importing,
tables,
&except_ptr);

if (rfc_rc != RFC_OK)
{
RfcClose(handle);
/* sprintf(szResult, "ERROR: RfcCallReceive(): key:%s message:%s\r\nexcept_ptr: %s"
, error_info_ex.key
, error_info_ex.message
, except_ptr);
*/
return (-2);
}

i = ItFill (itab_h);

if (i>0)
{
nLineLength = ItLeng (itab_h);
ptr = (char *) ItGetLine (itab_h, 1);


// Daten übernehmen

memset (&myData, 0, sizeof(MYDATA));

// PLNBEZ CHAR 18
strncpy (myData.strPLNBEZ, ptr, 18);
myData.strPLNBEZ[18]=0;
ptr += 18;

// GLTRS DATS 8
strncpy (myData.strGLTRS, ptr, 8);
myData.strGLTRS[8]=0;
ptr += 8;
// GSTRS DATS 8
strncpy (myData.strGSTRS, ptr, 8);
myData.strGSTRS[8]=0;
ptr += 8;
// GAMNG CHAR 13
RfcConvertBcdToChar ( (RFC_BCD *) ptr, /* @parm Input data to convert */
7, /* @parm Length of input data */
3, /* @parm No. of digits after , */
myData.strGAMNG, /* @parm Output data */
20 /* @parm Length of output data */
);
// strncpy (myData.strGAMNG, ptr, 7);
// myData.strGAMNG[7]=0;
ptr += 7;
// MAKTX CHAR 40
strncpy (myData.strMAKTX, ptr, 40);
myData.strMAKTX[40]=0;
ptr += 40;
// FERTH CHAR 18
strncpy (myData.strFERTH, ptr, 18);
myData.strFERTH[18]=0;
ptr += 18;
// MATNR CHAR 18
strncpy (myData.strMATNR, ptr, 18);
myData.strMATNR[18]=0;
ptr += 18;
// WKZNR CHAR 18
strncpy (myData.strWKZNR, ptr, 18);
myData.strWKZNR[18]=0;
ptr += 18;

strcpy (strPLNBEZ, myData.strPLNBEZ);
strcpy (strGLTRS, myData.strGLTRS);
strcpy (strGSTRS, myData.strGSTRS);
strcpy (strGAMNG, myData.strGAMNG);
strcpy (strMAKTX, myData.strMAKTX);
strcpy (strFERTH, myData.strFERTH);
strcpy (strMATNR, myData.strMATNR);
strcpy (strWKZNR, myData.strWKZNR);
}
else
{
RfcClose (handle);
return -2;
}

// RFC Verbindung schließen
RfcClose (handle);

return 0;
}