Programming Examples

The following sections contain examples in short form only. For more details, see the RFC client programs srfctest.c or sapinfo.c in the RFC SDK.

RFC Client Program Transferring Internal Tables

RFC_TABLE tables[1]; /* Work with one internal table */

RfcEnvironment (...); /* Install error handling function */

rfc_handle = RfcOpen (...); /* Open RFC connection */

if (rfc_handle == RFC_HANDLE_NULL) /* Check return code */

{

rfc_error_handling("RfcOpen"); /* Handle error and get error */

return 1; /* specification via RfcLastError */

}

tables[0].name = "ITAB1000"; /* Must fit with definition */
/* in SAP-FM */
tables[0].nlen = 8; /* Length of name */
tables[0].type = TYPEC; /* Character only */
tables[0].leng = 1000; /* Lenth of a table line */
tables[0].itmode = RFC_ITMODE_BYREFERENCE; /* Recommended */
tables[0].ithandle =
ItCreate (...); /* Allocate storage */
/* for internal table */

if (rfc_rc != RFC_OK) /* Check return code */

{

rfc_error_handling("ItCreated"); /* Get specific error via */
/* RfcLastError */
return 1; /* and handle error */

}

fill_table (table[0].ithandle, 10); /* Fill internal table with */
*/ 10 lines of text */

rfc_rc = RfcCallReceive (...); /* Call up function module */

if (rfc_rc != RFC_OK) /* Check return code */

{

rfc_error_handling("RfcCallReceive"); /* Get specific error via */
/* RfcLastError */
return 1; /* and handle error */

}

ItDelete (...); /* Free storage of internal table */

RfcClose (...); /* Close RFC connection */

/* Fill internal table with text as requested */

void fill_table (ITAB_H itab_h, int table_leng)

{
int linenr; /* Actual line number of a table */
int lineleng; /* Length of a table line */
char *ptr; /* Pointer to a table line */
char table_data[]="This is a test"; /* Text for test */

if (table_leng == 0) return 0; /* Table with no entry */

lineleng = ItLeng (itab_h); /* Determine length of a table */
/* line */

for (linenr = 1; linenr <= table_leng; linenr++)
/* Fill table as requested */

{

ptr = (char *) ItAppLine (itab_h);
/* Get address of next line */

if (ptr == NULL) /* Check return code */

{ /* Output error message and */

printf("\nMemory insufficient\n"); /* output error message */

exit(1); /* and terminate program */
}

memcpy(ptr, table_data, lineleng); /* Transfer data to internal */ /* table */

}
return; /* Back to main program */

}

RFC Client Program Receiving Internal Tables

RFC_TABLE tables[1]; /* Work with one internal table */

RfcEnvironment (...); /* Install error_handling function */

rfc_handle = RfcOpen (...); /* Open RFC connection */

if (rfc_handle == RFC_HANDLE_NULL) /* Check return code */

{

rfc_error_handling("RfcOpen"); /* Handle error and get error */

return 1; /* specification via RfcLastError */

}

tables[0].name = "ITAB1000"; /* Must fit with definition */
/* in SAP-FM */
tables[0].nlen = 8; /* Length of name */
tables[0].type = TYPEC; /* Character only */
tables[0].leng = 1000; /* Lenth of a table line */
tables[0].itmode = RFC_ITMODE_BYREFERENCE; /* Recommended */
tables[0].ithandle =
ItCreate (...); /* Allocate storage */
/* for internal table */

if (rfc_rc != RFC_OK) /* Check return code */

{

rfc_error_handling("ItCreated"); /* Get specific error via */
/* RfcLastError */
return 1; /* and handle error */

}

rfc_rc = RfcCallReceive (...); /* Call up function module */

if (rfc_rc != RFC_OK) /* Check return code */

{

rfc_error_handling("RfcCallReceive"); /* Get specific error via */
/* RfcLastError */
return 1; /* and handle error */

}

display_table (table[0].ithandle); /* output received internal */
/* table on screen */

ItDelete (...); /* Free storage of internal table */

RfcClose (...); /* Close RFC connection */

/* Output received internal table on screen */

void display_table (ITAB_H itab_h)

{
int linenr; /* Actual line number of a table */
int lineleng; /* Length of a table line */
char ptr; /* Pointer to a table line */
char table_data[8193]; /* Max. length of an internal */
/* table (8192 B) */

lineleng = ItLeng (itab_h); /* Get length of a table line */

for (linenr = 1; ; linenr++) /* Loop at internal table */

{

ptr = (char *) ItGetLine (itab_h);
/* Get address of next line */

if (ptr == NULL) break; /* NULL: End of table reached */

memcpy(table_data, ptr, lineleng);
/* Read a table line into buffer */

table_data[lineleng] = '\0'; /* Set string end in buffer for */
/* output */

printf("'%s'\n", table_data); /* Output on screen */

}

return; /* Back to main program */

}