// SAP RFC C++ Class library Example. // Copyright 1996 SAP America, Inc. // All rights reserved.
#include <iostream.h> #include "CRfclass.h" #include "Client.h" ////////////////////////////////////////////////////////// // Example RFC client application // This program demonstrates using RFC classes to // make an RFC call to the R/3 system. // // The called RFC function is declared as follows: // RFC_CUSTOMER_GET // Import KUNNR, NAME1 // Table CUSTOMER_T ////////////////////////////////////////////////////////// int main() { CRfcConnection Connection ; //Connection object char* pException ; //RFC call exceptions CRfcCustomer MyFunc(&Connection); //RFC_CUSTOMER_GET function object RFC_RC rc; //RFC return code try { Logon (Connection) ; } catch (RFC_ERROR_INFO err) { DumpErrorInfo (err) ; return 0; } //Assign KUNNR parameter value MyFunc.SetCustomerNumber("*") ; try { //pException is used for the exception raised by the //RFC function. In this case, no exception is thrown //by the "CallReceive" function rc = MyFunc.CallReceive (pException) ; } catch (RFC_ERROR_INFO err) { cout<<"RFC CallReceive Error:\r\n" ; DumpErrorInfo (err) ; } if (rc != RFC_OK) { cout<<"RFC Exception:"<<pException<<"\n\r" ; } //Dump customer information in the table //Because there is only one table parameter for the //function object, it is easy just to use index. //It's also possible to use: //DumpTable (MyFunc.GetTableParam (CUSTOMER) ; MyFunc.DumpTable () ; return 0; } /////////////////////////////////////////// //Define Helper functions //Set logon information and logon to the system ///////////////////////////////////////////
void Logon (CRfcConnection& Connection) { RFC_USER_INFO UserInfo; //All user login info RFC_CONNECT_INFO ConnectInfo; //R/3 system connection info //Define user logon info UserInfo.rstrClient = "000"; UserInfo.rstrUserName = "zeng"; UserInfo.rstrPassword = "zzheng"; UserInfo.rstrLanguage = "E"; //Define system connection info ConnectInfo.rstrDestination = "DEMO" ; ConnectInfo.rfcMode = RFC_MODE_R3ONLY ; ConnectInfo.rstrHostName = "apd2105"; ConnectInfo.rstrGatewayHost = "apd2105"; ConnectInfo.rstrGatewayService = "sapgw22"; ConnectInfo.nSystemNo = 22; //Fill user info and connection info into the //connection object so that they will be used //when starting logon Connection.SetUserInfo (UserInfo); Connection.SetConnectInfo (ConnectInfo); //Actually opens a connection and verifies the //user information. If no user validation is desired //at this point, use Connection.Open instead. Connection.SafeOpen () ; } //Dump RFC table contents //It only works on tables that contain character strings void CRfcCustomer::DumpTable () { assert (m_pTabCUSTOMER_T != NULL) ; if (m_pTabCUSTOMER_T == NULL) return ; int nFieldCount = m_pTabCUSTOMER_T->GetFieldCount () ; //Print the column headers for (int nColumn = 0; nColumn < nFieldCount; nColumn++) { const RFC_FIELD_INFO *pRfcField ; //Get individual field description pRfcField = m_pTabCUSTOMER_T->GetFieldInfo (nColumn) ; cout<<CSTR(pRfcField->strName)<<"\t" ; } //Change line after finishing column headers cout<<"\r\n" ; int nRowCount = m_pTabCUSTOMER_T->GetRowCount() ; for (int nRow = 0; nRow < nRowCount; nRow++) { for (nColumn = 0; nColumn < nFieldCount; nColumn++) { cout<<(CSTR)(m_pTabCUSTOMER_T->Cell (nRow, nColumn))<<"\t"; } cout<<"\r\n" ; } } CRfcCustomer::CRfcCustomer(CRfcConnection* pConnection) :CRfcClientFunc(pConnection, "RFC_CUSTOMER_GET") {
m_pTabCUSTOMER_T = NULL; m_pSimKUNNR = NULL; m_pSimNAME1 = NULL; try { m_pSimKUNNR = new CRfcSimpleParam (NAME, TYPC, NAME_LEN) ; m_pSimNAME1 = new CRfcSimpleParam (NUMBER, TYPC, NUMBER_LEN) ; m_pTabCUSTOMER_T = new CRfcTableParam (CUSTOMER, CUSTOEMER_LEN) ; //Provide field information for table object //referenced by pTabCUSTOMER_T m_pTabCUSTOMER_T->AddFieldInfo (FieldsCUSTOMER_T, sizeof(FieldsCUSTOMER_T) / sizeof(RFC_FIELD_INFO)) ; //Add parameter objects to function object //When function object is destroyed, it does not //delete the parameter objects. It is the user's //reponsibility to delete the parameter object. AddImportParam (*m_pSimKUNNR) ; AddImportParam (*m_pSimNAME1) ; AddTableParam (*m_pTabCUSTOMER_T) ; m_pTabCUSTOMER_T->Create(); } catch (char*) // catch memory { ClearParams(); throw; } } void CRfcCustomer::SetCustomerNumber(CSTR Number) { m_pSimKUNNR->Value() = Number ; } void CRfcCustomer::SetCustomerName(CSTR Name) { m_pSimNAME1->Value() = Name ; } //Clear all RFC function parameters void CRfcCustomer::ClearParams() { if (m_pSimKUNNR != NULL) delete m_pSimKUNNR ; if (m_pSimNAME1 != NULL) delete m_pSimNAME1 ; if (m_pTabCUSTOMER_T != NULL) delete m_pTabCUSTOMER_T ; } CRfcCustomer::~CRfcCustomer() { ClearParams() ; } //Print out RFC error information void DumpErrorInfo (RFC_ERROR_INFO& err) { cout<<"RFC Error:\r\n" ; cout<<"Key:"<<err.key<<"\r\n" ; cout<<"Status:"<<err.status<<"\r\n"; cout<<"Message:"<<err.message<<"r\n"; } |