Show TOC

Procedure documentationCalling a Function in SAP Systems Locate this document in the navigation structure

 

In the following example program, an external RFC client connects to an SAP system and calls the function module BAPI_COMPANY_GETDETAIL:

Syntax Syntax

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "sapnwrfc.h"
  4. void errorHandling(RFC_RC rc, SAP_UC description[], RFC_ERROR_INFO*
  5. errorInfo, RFC_CONNECTION_HANDLE connection){
  6. 	printfU(cU("%s: %d\n"), description, rc);
  7. 	printfU(cU("%s: %s\n"), errorInfo->key, errorInfo->message);
  8. /* It's better to close the TCP/IP connection cleanly, than to 
  9. just let the backend get a "Connection reset by peer" error...*/
  10. 	if (connection != NULL) RfcCloseConnection(connection, errorInfo);
  11. 	exit(1);
  12. }
  13. int mainU(int argc, SAP_UC** argv){
  14. 	RFC_RC rc = RFC_OK;
  15. 	RFC_CONNECTION_PARAMETER loginParams[6];
  16. 	RFC_ERROR_INFO errorInfo;
  17. 	RFC_CONNECTION_HANDLE connection;
  18. 	RFC_FUNCTION_DESC_HANDLE bapiCompanyDesc;
  19. 	RFC_FUNCTION_HANDLE bapiCompany;
  20. 	RFC_STRUCTURE_HANDLE returnStructure;
  21. 	SAP_UC message[221];
  22. 	RFC_BYTE buffer[1105];
  23. 	unsigned utf8Len = 1105, resultLen;
  24. 	FILE* outFile;
  25. ----------------------------------------------- 
  26. /*OPEN CONNECTION*/
  27. ----------------------------------------------- 
  28. /*Create logon parameter list*/
  29. 	loginParams[0].name = cU("ashost");	loginParams[0].value =
  30. cU("hs0023");
  31. 	loginParams[1].name = cU("sysnr");	loginParams[1].value = cU("05");
  32. 	loginParams[2].name = cU("client");	loginParams[2].value =
  33. cU("800");
  34. 	loginParams[3].name = cU("user");	loginParams[3].value =
  35. cU("alice");
  36. 	loginParams[4].name = cU("lang");	loginParams[4].value =
  37. cU("EN");
  38. 	loginParams[5].name = cU("passwd");	loginParams[5].value =
  39. cU("secret");
  40. /*Open connection*/
  41. 	connection = RfcOpenConnection(loginParams, 6, );
  42. 	if (connection == NULL) errorHandling(rc, cU("Error during logon"),
  43. , NULL);
  44. ----------------------------------------------- 
  45. /* DYNAMIC METADATA retrieval (can also be executed using STATIC functions)*/
  46. ----------------------------------------------- 
  47. 	bapiCompanyDesc = RfcGetFunctionDesc(connection,
  48. cU("BAPI_COMPANY_GETDETAIL"), );
  49. 	if (bapiCompanyDesc == NULL) errorHandling(rc, cU("Error during
  50. metadata lookup"), , connection);
  51. -----------------------------------------------
  52. /*FUNCTION CALL/*
  53. -----------------------------------------------
  54. 	
  55. /*Create function instance*/
  56. 	
  57. bapiCompany = RfcCreateFunction(bapiCompanyDesc, );
  58. 	/*Parameter setting*/ 
  59. 	RfcSetChars(bapiCompany, cU("COMPANYID"), cU("000007"), 6,
  60. );
  61. 	/*Call*/
  62. 	rc = RfcInvoke(connection, bapiCompany, );
  63. 	if (rc != RFC_OK) errorHandling(rc, cU("Error calling
  64. BAPI_COMPANY_GETDETAIL"), , connection);
  65. 	/*Getting parameters*/
  66. 	RfcGetStructure(bapiCompany, cU("RETURN"), ,
  67. );
  68. 	RfcGetString(returnStructure, cU("MESSAGE"), message, 221,
  69. , );
  70. 	/*After having finished the procedure: release memory only if a ‘create’ function was invoked before*/
  71. 	RfcDestroyFunction(bapiCompany, );
  72. 	/*Final UTF8-UTF16 conversion*/
  73. 	windows.h utf8Len = WideCharToMultiByte(CP_UTF8, 0, message,
  74. strlenU(message), buffer, 1105, NULL, NULL);
  75. 	RfcSAPUCToUTF8(message,  strlenU(message), buffer, ,
  76. , );
  77. 	outFile = fopen("message.xml", "w");
  78. 	fputs("<?xml version=\"1.0\"?>\n<message>", outFile);
  79. 	fputs(buffer, outFile);
  80. 	fputs("</message>", outFile);
  81. 	fclose(outFile);
  82. 	return 0;
  83. }
  84.  
End of the code.