Добавляем в свойства компилятора -DSAPwithUNICODE а в свойства линковщика lib\sapnwrfc.lib lib\libsapucum.lib
sapnwrfc.ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
DEST=DEV TPNAME=TB_CALL #CODEPAGE=8400 ASHOST=saptx2 SYSNR=00 #SYSID=TX2 R3NAME=TX2 USER=COBOL_USER PASSWD=Pa$$w0rd CLIENT=362 LANG=RU TYPE=R # _DEBUG: Can be used for R/3 systems with release < 6.20, where "external breakpoints" are not yet available # _DEBUG=1 #USE_SAPGUI=2 GWSERV=sapgw00 GWHOST=saptx2 #RFC_TRACE=0 SAPROUTER=/H/10.26.169.35/S/3297 #TRACE=2 MSHOST=tx2ci NO_COMPRESSION=1 GROUP=SPACE |
functions.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdlib.h> #include <stdio.h> #include "sapnwrfc.h" class CFunctions { public: static int listening; static int userSaysYes(void); static void readValue(SAP_UC* buffer, int max); static RFC_RC SAP_API genericRequestHandler(RFC_CONNECTION_HANDLE rfcHandle, RFC_FUNCTION_HANDLE funcHandle, RFC_ERROR_INFO* errorInfo); void listen(RFC_CONNECTION_HANDLE* pserverHandle, RFC_CONNECTION_PARAMETER* params); static void errorThrowing(RFC_FUNCTION_DESC_HANDLE metadata, RFC_ERROR_INFO* errorInfo); static RFC_RC SAP_API repositoryLookup(SAP_UC const* functionName, RFC_ATTRIBUTES rfcAttributes, RFC_FUNCTION_DESC_HANDLE* funcDescHandle); static void fillSyMsg(RFC_ERROR_INFO* perrorInfo); void checkForReset(RFC_CONNECTION_HANDLE conn); static void printParameter(RFC_PARAMETER_DESC paramDesc, RFC_FUNCTION_HANDLE container) {} static void fillParameter(int* index, RFC_PARAMETER_DESC desc, RFC_FUNCTION_HANDLE container) {} void printExports(RFC_FUNCTION_DESC_HANDLE description, RFC_FUNCTION_HANDLE container); static void printImports(RFC_FUNCTION_DESC_HANDLE description, RFC_FUNCTION_HANDLE container); void fillImports(RFC_FUNCTION_DESC_HANDLE description, RFC_FUNCTION_HANDLE container); static void fillExports(RFC_FUNCTION_DESC_HANDLE description, RFC_FUNCTION_HANDLE container); static RFC_RC checkAuthorization(RFC_ABAP_NAME funcName, RFC_ATTRIBUTES attributes); void fillStructure(unsigned indent, RFC_TYPE_DESC_HANDLE typeDesc, RFC_STRUCTURE_HANDLE container); void printStructure(unsigned indent, RFC_TYPE_DESC_HANDLE typeDesc, RFC_STRUCTURE_HANDLE container); void fillTable(unsigned indent, RFC_TYPE_DESC_HANDLE typeDesc, RFC_TABLE_HANDLE container); void printTable(unsigned indent, RFC_TYPE_DESC_HANDLE typeDesc, RFC_TABLE_HANDLE container); }; |
functions.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
#include <stdlib.h> #include <stdio.h> #include "functions.h" #define BUF_SIZE 1024 static SAP_UC buffer[BUF_SIZE]; // This is not thread-safe int CFunctions::listening = 1; RFC_RC SAP_API CFunctions::genericRequestHandler(RFC_CONNECTION_HANDLE rfcHandle, RFC_FUNCTION_HANDLE funcHandle, RFC_ERROR_INFO* errorInfo) { RFC_RC rc; RFC_ATTRIBUTES attributes; RFC_FUNCTION_DESC_HANDLE metadata; RFC_ABAP_NAME funcName; printfU(cU("\n---------------------------------------\n")); RfcGetConnectionAttributes(rfcHandle, &attributes, NULL); metadata = RfcDescribeFunction(funcHandle, NULL); RfcGetFunctionName(metadata, funcName, NULL); rc = checkAuthorization(funcName, attributes); if (rc != RFC_OK) { errorInfo->code = RFC_EXTERNAL_FAILURE; memcpy(errorInfo->message, cU("Access denied!"), 25); rc = RFC_EXTERNAL_FAILURE; goto end; } printfU(cU("\n")); printImports(metadata, funcHandle); printfU(cU("\n")); errorThrowing(metadata, errorInfo); if (errorInfo->code != RFC_OK) { rc = errorInfo->code; goto end; } printfU(cU("\n")); fillExports(metadata, funcHandle); end: printfU(cU("\nStop listening after this request? [y/n] ")); if (userSaysYes()) listening = 0; return rc; } void CFunctions::listen(RFC_CONNECTION_HANDLE* pserverHandle, RFC_CONNECTION_PARAMETER* params) { RFC_RC rc; RFC_ERROR_INFO errorInfo; int refresh = 0; rc = RfcListenAndDispatch(*pserverHandle, 2, &errorInfo); switch (rc) { case RFC_OK: case RFC_RETRY: break; case RFC_ABAP_EXCEPTION: printfU(cU("COBOL_EXCEPTION in implementing function: %s\n"), errorInfo.key); break; case RFC_NOT_FOUND: printfU(cU("Unknown function module: %s\n"), errorInfo.message); refresh = 1; break; case RFC_EXTERNAL_FAILURE: printfU(cU("SYSTEM_FAILURE has been sent to backend: %s\n"), errorInfo.message); refresh = 1; break; case RFC_ABAP_MESSAGE: printfU(cU("Cobol message has been sent to backend: %s %s %s\n"), errorInfo.abapMsgType, errorInfo.abapMsgClass, errorInfo.abapMsgNumber); printfU(cU("Variables: V1=%s V2=%s V3=%s V4=%s\n"), errorInfo.abapMsgV1, errorInfo.abapMsgV2, errorInfo.abapMsgV3, errorInfo.abapMsgV4); refresh = 1; break; case RFC_COMMUNICATION_FAILURE: case RFC_CLOSED: printfU(cU("Connection broke down during transmission of return values: %s\n"), errorInfo.message); refresh = 1; break; } if (refresh) { printfU(cU("Trying to reconnect...\n")); *pserverHandle = RfcRegisterServer(params, 1, &errorInfo); if (*pserverHandle == NULL) { printfU(cU("Unable to reconnect to %s: %s: %s\n"), params->value, RfcGetRcAsString(errorInfo.code), errorInfo.message); printfU(cU("Stopping to listen at %s"), params->value); } } if (rc != RFC_RETRY && *pserverHandle != NULL && listening) printfU(cU("\nWaiting for request...\n")); } void CFunctions::errorThrowing(RFC_FUNCTION_DESC_HANDLE metadata, RFC_ERROR_INFO* errorInfo) { unsigned i, index = 2, paramCount = 0, choice = 0; RFC_EXCEPTION_DESC excepDesc; SAP_UC value[203]; errorInfo->code = RFC_OK; printf(("For this function we can throw the following errors:\n")); printfU(cU("1. SYSTEM_FAILURE\n")); printfU(cU("2. Cobol message\n")); RfcGetExceptionCount(metadata, ¶mCount, NULL); for (i = 0; i < paramCount; i++) { RfcGetExceptionDescByIndex(metadata, i, &excepDesc, NULL); printfU(cU("%d. %s: %s\n"), ++index, excepDesc.key, excepDesc.message); } printfU(cU("Please enter a value between 1 and %d or \"n\" for not throwing an exception: "), index); readValue(value, 203); if (strncmpU(value, cU("n"), 1) == 0) return; choice = atoiU(value); if (choice < 1 || choice > index) return; switch (choice) { case 1: printfU(cU("Please enter a value for the message (200): ")); readValue(value, 203); // In the worst case we need one extra for each of \r\n\0 i = strlenU(value); memcpy(errorInfo->message, value, i < 200 ? i : 200); errorInfo->code = RFC_EXTERNAL_FAILURE; break; case 2: fillSyMsg(errorInfo); errorInfo->code = RFC_ABAP_MESSAGE; break; default: errorInfo->code = RFC_ABAP_EXCEPTION; index = choice - 3; RfcGetExceptionDescByIndex(metadata, index, &excepDesc, NULL); memcpy(errorInfo->key, excepDesc.key, strlenU(excepDesc.key)); printfU(cU("Do you want to send some SY-MSG variables together with the Cobol exception? [y/n] ")); if (userSaysYes()) fillSyMsg(errorInfo); } } RFC_RC SAP_API CFunctions::repositoryLookup(SAP_UC const* functionName, RFC_ATTRIBUTES rfcAttributes, RFC_FUNCTION_DESC_HANDLE* funcDescHandle) { RFC_CONNECTION_PARAMETER loginParams[1]; RFC_CONNECTION_HANDLE repoCon; RFC_ERROR_INFO errorInfo; printfU(cU("Repository: %s not yet cached. Checking backend's DDIC.\n"), functionName); loginParams->name = cU("dest"); loginParams->value = rfcAttributes.sysId; repoCon = RfcOpenConnection(loginParams, 1, &errorInfo); if (repoCon == NULL) { printfU(cU("Repository: unable to connect to %s: %s: %s\n"), rfcAttributes.sysId, RfcGetRcAsString(errorInfo.code), errorInfo.message); return RFC_NOT_FOUND; } *funcDescHandle = RfcGetFunctionDesc(repoCon, functionName, &errorInfo); RfcCloseConnection(repoCon, NULL); if (*funcDescHandle == NULL) { printfU(cU("Repository: unable to find metadata for %s: %s: %s\n\n"), functionName, RfcGetRcAsString(errorInfo.code), errorInfo.key); return RFC_NOT_FOUND; } printfU(cU("Repository: successfully retrieved metadata information for %s\n\n"), functionName); return RFC_OK; } void CFunctions::fillSyMsg(RFC_ERROR_INFO* perrorInfo) { SAP_UC value[53]; printfU(cU("Please enter a value for SY-MSGTY [E/A/X]: ")); readValue(value, 53); value[1] = 0; memcpy(perrorInfo->abapMsgType, value, 2); printfU(cU("Please enter a value for SY-MSGID (CHAR20): ")); readValue(value, 53); value[20] = 0; memcpy(perrorInfo->abapMsgClass, value, strlenU(value)); printfU(cU("Please enter a value for SY-MSGNO [000-999]: ")); readValue(value, 53); value[3] = 0; memcpy(perrorInfo->abapMsgNumber, value, strlenU(value)); printfU(cU("Please enter a value for SY-MSGV1 (CHAR50): ")); readValue(value, 53); value[50] = 0; memcpy(perrorInfo->abapMsgV1, value, strlenU(value)); printfU(cU("Please enter a value for SY-MSGV2 (CHAR50): ")); readValue(value, 53); value[50] = 0; memcpy(perrorInfo->abapMsgV2, value, strlenU(value)); printfU(cU("Please enter a value for SY-MSGV3 (CHAR50): ")); readValue(value, 53); value[50] = 0; memcpy(perrorInfo->abapMsgV3, value, strlenU(value)); printfU(cU("Please enter a value for SY-MSGV4 (CHAR50): ")); readValue(value, 53); value[50] = 0; memcpy(perrorInfo->abapMsgV4, value, strlenU(value)); } void CFunctions::fillStructure(unsigned indent, RFC_TYPE_DESC_HANDLE typeDesc, RFC_STRUCTURE_HANDLE container) { unsigned i, j, fieldCount; RFC_RC rc; RFC_ERROR_INFO errorInfo; RFC_FIELD_DESC fieldDesc; RFC_STRUCTURE_HANDLE structure; RFC_TABLE_HANDLE table; RfcGetFieldCount(typeDesc, &fieldCount, NULL); for (i = 0; i < fieldCount; i++) { RfcGetFieldDescByIndex(typeDesc, i, &fieldDesc, NULL); for (j = 0; j < indent; j++) printfU(cU("\t")); switch (fieldDesc.type) { case RFCTYPE_STRUCTURE: printfU(cU("%s is a structure. Do you want to fill it? [y/n] "), fieldDesc.name); if (userSaysYes()) { rc = RfcGetStructure(container, fieldDesc.name, &structure, &errorInfo); if (rc != RFC_OK) { //Probably out of memory?! Bad luck... printfU(cU("Could not create container for %s: %s %s.\n"), fieldDesc.name, RfcGetRcAsString(errorInfo.code), errorInfo.message); printfU(cU("We'll need to do without it...\n")); continue; } fillStructure(indent + 1, fieldDesc.typeDescHandle, structure); } break; case RFCTYPE_TABLE: printfU(cU("%s is a table. Do you want to fill it? [y/n] "), fieldDesc.name); if (userSaysYes()) { rc = RfcGetTable(container, fieldDesc.name, &table, &errorInfo); if (rc != RFC_OK) { //Probably out of memory?! Bad luck... printfU(cU("Could not create container for %s: %s %s.\n"), fieldDesc.name, RfcGetRcAsString(errorInfo.code), errorInfo.message); printfU(cU("We'll need to do without it...\n")); continue; } fillTable(indent + 1, fieldDesc.typeDescHandle, table); } break; default: printfU(cU("%s, %s, Length %d: "), fieldDesc.name, RfcGetTypeAsString(fieldDesc.type), fieldDesc.nucLength); readValue(buffer, BUF_SIZE); rc = RfcSetString(container, fieldDesc.name, buffer, strlenU(buffer), &errorInfo); if (rc != RFC_OK) { //Probably the user entered some nonsense. Give him a second chance... printfU(cU("Could not set the value: %s %s.\n"), RfcGetRcAsString(errorInfo.code), errorInfo.message); printfU(cU("Try again? [y/n] ")); if (userSaysYes()) i--; } } } } void CFunctions::printStructure(unsigned indent, RFC_TYPE_DESC_HANDLE typeDesc, RFC_STRUCTURE_HANDLE container) { unsigned i, j, fieldCount, resultLen; RFC_RC rc; RFC_ERROR_INFO errorInfo; RFC_FIELD_DESC fieldDesc; RFC_STRUCTURE_HANDLE structure; RFC_TABLE_HANDLE table; RfcGetFieldCount(typeDesc, &fieldCount, NULL); for (i = 0; i < fieldCount; i++) { RfcGetFieldDescByIndex(typeDesc, i, &fieldDesc, NULL); for (j = 0; j < indent; j++) printfU(cU("\t")); switch (fieldDesc.type) { case RFCTYPE_STRUCTURE: printfU(cU("%s is a structure. Do you want to see its values? [y/n] "), fieldDesc.name); if (userSaysYes()) { rc = RfcGetStructure(container, fieldDesc.name, &structure, &errorInfo); if (rc != RFC_OK) { //Probably out of memory?! Bad luck... printfU(cU("Could not obtain container for %s: %s %s.\n"), fieldDesc.name, RfcGetRcAsString(errorInfo.code), errorInfo.message); printfU(cU("We'll need to do without it...\n")); continue; } printStructure(indent + 1, fieldDesc.typeDescHandle, structure); } break; case RFCTYPE_TABLE: rc = RfcGetTable(container, fieldDesc.name, &table, &errorInfo); if (rc != RFC_OK) { //Probably out of memory?! Bad luck... printfU(cU("Could not obtain container for %s: %s %s.\n"), fieldDesc.name, RfcGetRcAsString(errorInfo.code), errorInfo.message); printfU(cU("We'll need to do without it...\n")); continue; } RfcGetRowCount(table, &resultLen, NULL); printfU(cU("%s is a table with %d lines. Do you want to see its values? [y/n] "), fieldDesc.name, resultLen); if (userSaysYes()) { printTable(indent + 1, fieldDesc.typeDescHandle, table); } break; default: printfU(cU("%s, %s, Length %d: "), fieldDesc.name, RfcGetTypeAsString(fieldDesc.type), fieldDesc.nucLength); rc = RfcGetString(container, fieldDesc.name, buffer, BUF_SIZE, &resultLen, &errorInfo); if (rc != RFC_OK) { //Probably the buffer is too short?! printfU(cU("Could not read the value: %s %s.\n"), RfcGetRcAsString(errorInfo.code), errorInfo.message); continue; } printfU(cU("%s\n"), buffer); } } } void CFunctions::fillTable(unsigned indent, RFC_TYPE_DESC_HANDLE typeDesc, RFC_TABLE_HANDLE container) { unsigned lines, i, j; RFC_STRUCTURE_HANDLE lineHandle; RFC_ERROR_INFO errorInfo; for (j = 1; j < indent; j++) printfU(cU("\t")); printfU(cU("Please enter the number of lines: ")); readValue(buffer, BUF_SIZE); lines = atoiU(buffer); for (i = 0; i < lines; i++) { lineHandle = RfcAppendNewRow(container, &errorInfo); if (lineHandle == NULL) { // Probably out of memory?! printfU(cU("Unable to create a new line: %s %s\n"), RfcGetRcAsString(errorInfo.code), errorInfo.message); printfU(cU("Skipping the rest.\n")); break; } for (j = 1; j < indent; j++) printfU(cU("\t")); printfU(cU("Please enter the values for line %d:\n"), i); fillStructure(indent, typeDesc, lineHandle); } } void CFunctions::printTable(unsigned indent, RFC_TYPE_DESC_HANDLE typeDesc, RFC_TABLE_HANDLE container) { unsigned lines, i, j; RFC_ERROR_INFO errorInfo; RfcGetRowCount(container, &lines, &errorInfo); if (lines > 20) { for (j = 1; j < indent; j++) printfU(cU("\t")); printfU(cU("That's too many. Only printing the first 20...\n")); lines = 20; } for (i = 0; i < lines; i++) { RfcMoveTo(container, i, &errorInfo); for (j = 1; j < indent; j++) printfU(cU("\t")); printfU(cU("Contents of line %d:\n"), i); printStructure(indent, typeDesc, container); } } void CFunctions::checkForReset(RFC_CONNECTION_HANDLE conn) { printfU(cU("Do you want to reset the Cobol session? [y/n] ")); if (userSaysYes()) RfcResetServerContext(conn, NULL); } RFC_RC CFunctions::checkAuthorization(RFC_ABAP_NAME funcName, RFC_ATTRIBUTES attributes) { printfU(cU("AuthorizationCheck: User %s from system %s, client %s, host %s is trying to invoke %s.\n"), attributes.user, attributes.sysId, attributes.client, attributes.partnerHost, funcName); printfU(cU("Allow access? [y/n] ")); if (userSaysYes()) return RFC_OK; return RFC_EXTERNAL_FAILURE; } void CFunctions::readValue(SAP_UC* buf, int max) { size_t length; fgetsU(buf, max, stdin); // Unfortunately gets_sU doesn't exist... length = strlenU(buf); if (buf[length - 2] == cU('\r') || buf[length - 2] == cU('\n')) buf[length - 2] = 0; else if (buf[length - 1] == cU('\n') || buf[length - 1] == cU('\r')) buf[length - 1] = 0; } int CFunctions::userSaysYes(void) { SAP_UC buf[3]; readValue(buf, 3); if (strncmpU(buf, cU("y"), 3) == 0) return 1; return 0; } void CFunctions::printImports(RFC_FUNCTION_DESC_HANDLE description, RFC_FUNCTION_HANDLE container) { unsigned i, parameterCount; RFC_PARAMETER_DESC paramDesc; RfcGetParameterCount(description, ¶meterCount, NULL); printfU(cU("Here are the values of the IMPORTING parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_IMPORT) { printParameter(paramDesc, container); } } printfU(cU("\nHere are the values of the CHANGING parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_CHANGING) { printParameter(paramDesc, container); } } printfU(cU("\nHere are the values of the TABLES parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_TABLES) { printParameter(paramDesc, container); } } } void CFunctions::fillExports(RFC_FUNCTION_DESC_HANDLE description, RFC_FUNCTION_HANDLE container) { int i; unsigned parameterCount; RFC_PARAMETER_DESC paramDesc; RfcGetParameterCount(description, ¶meterCount, NULL); printfU(cU("Please enter the EXPORTING parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_EXPORT) { fillParameter(&i, paramDesc, container); } } printfU(cU("\nPlease enter the CHANGING parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_CHANGING) { fillParameter(&i, paramDesc, container); } } printfU(cU("\nPlease enter the TABLES parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_TABLES) { fillParameter(&i, paramDesc, container); } } } void CFunctions::fillImports(RFC_FUNCTION_DESC_HANDLE description, RFC_FUNCTION_HANDLE container) { int i; unsigned parameterCount; RFC_PARAMETER_DESC paramDesc; RfcGetParameterCount(description, ¶meterCount, NULL); printfU(cU("Please enter the IMPORTING parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_IMPORT) { fillParameter(&i, paramDesc, container); } } printfU(cU("\nPlease enter the CHANGING parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_CHANGING) { fillParameter(&i, paramDesc, container); } } printfU(cU("\nPlease enter the TABLES parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_TABLES) { fillParameter(&i, paramDesc, container); } } } void CFunctions::printExports(RFC_FUNCTION_DESC_HANDLE description, RFC_FUNCTION_HANDLE container) { unsigned i, parameterCount; RFC_PARAMETER_DESC paramDesc; RfcGetParameterCount(description, ¶meterCount, NULL); printfU(cU("Here are the results of the EXPORTING parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_EXPORT) { printParameter(paramDesc, container); } } printfU(cU("\nHere are the results of the CHANGING parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_CHANGING) { printParameter(paramDesc, container); } } printfU(cU("\nHere are the results of the TABLES parameters:\n")); for (i = 0; i < parameterCount; i++) { RfcGetParameterDescByIndex(description, i, ¶mDesc, NULL); if (paramDesc.direction == RFC_TABLES) { printParameter(paramDesc, container); } } } |
rfc_exchange.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
#include <iostream> #include <string> #include <stdlib.h> #include <stdio.h> #include "functions.h" #define _CRT_SECURE_NO_WARNINGS CFunctions func; static RFC_FUNCTION_HANDLE bapiUserGetDetail; RFC_STRUCTURE_HANDLE structHandle = NULL; RFC_FUNCTION_HANDLE c2014Header = NULL; RFC_TABLE_HANDLE ct2014Header = NULL; RFC_TYPE_DESC_HANDLE c2014Desc = NULL; RFC_FIELD_DESC fieldDesc; DATA_CONTAINER_HANDLE hcont; RFC_FUNCTION_HANDLE c2013Header = NULL; RFC_TABLE_HANDLE ct2013Header = NULL; RFC_TYPE_DESC_HANDLE c2013Desc = NULL; RFC_RC SAP_API c2013Cost_XIOut(RFC_CONNECTION_HANDLE rfcHandle, RFC_FUNCTION_HANDLE funcHandle, RFC_ERROR_INFO* errorInfo) { RFC_RC rc = RFC_OK; unsigned length; unsigned int tablen, fieldCount; SAP_UC requtext[256], resptext[256], echotext[256]; RFC_ATTRIBUTES attributes; RFC_ERROR_INFO error; RFC_STRUCTURE_HANDLE address, company, zscobol2013_cost_in; rc = RfcGetStructure(funcHandle, cU("IS_INPUT"), &structHandle, &error); // zscobol_c2013_cost rc = RfcGetTable(structHandle, cU("COST"), &ct2013Header, &error); // zicobol2013_tb_mat_data RfcGetRowCount(ct2013Header, &tablen, &error); zscobol2013_cost_in = RfcGetCurrentRow(ct2014Header, &error); // zscobol2013_cost_in c2013Desc = RfcDescribeType(zscobol2013_cost_in, errorInfo); rc = RfcGetFieldCount(c2013Desc, &fieldCount, errorInfo); for (int i = 0; i < fieldCount; i++) { rc = RfcGetFieldDescByIndex(c2013Desc, i, &fieldDesc, errorInfo); const int x = fieldDesc.nucLength; SAP_UC veri_data1[18 + 1]; rc = RfcGetChars(zscobol2013_cost_in, cU("COSTCENTERDATA"), veri_data1, sizeof(veri_data1), &error); printfU(cU("COSTCENTERDATA : %s\n"), veri_data1); } RfcGetString(funcHandle, cU("REQUTEXT"), requtext, 256, &length, NULL); printfU(cU("\nReceived request for ZCOBOL_C2013COSTCENTER_RFC_OUT\n")); printfU(cU("REQUTEXT = %s\n"), requtext); printfU(cU("Do you want to stop listening after this request? [y/n] ")); return RFC_OK; } RFC_RC SAP_API c2014MatTB_PIOut(RFC_CONNECTION_HANDLE rfcHandle, RFC_FUNCTION_HANDLE funcHandle, RFC_ERROR_INFO* errorInfo) { RFC_RC rc = RFC_OK; unsigned length; unsigned int tablen, fieldCount; SAP_UC requtext[256], resptext[256], echotext[256]; RFC_ATTRIBUTES attributes; RFC_ERROR_INFO error; RFC_STRUCTURE_HANDLE address, company, zscobol2014_tb_mat_data2; rc = RfcGetStructure(funcHandle, cU("IS_INPUT"), &structHandle, &error); // ZSCOBOL2014_TB_MAT rc = RfcGetTable(structHandle, cU("MATERIALDATA"), &ct2014Header, &error); // zscobol2014_tb_mat_data RfcGetRowCount(it2014Header, &tablen, &error); zscobol2014_tb_mat_data2 = RfcGetCurrentRow(ct2014Header, &error); // zscobol2014_tb_mat_data2 c2014Desc = RfcDescribeType(zscobol2014_tb_mat_data2, errorInfo); rc = RfcGetFieldCount(c2014Desc, &fieldCount, errorInfo); for (int i = 0; i < fieldCount; i++) { rc = RfcGetFieldDescByIndex(c2014Desc, i, &fieldDesc, errorInfo); const int x = fieldDesc.nucLength; SAP_UC veri_data1[18 + 1]; rc = RfcGetChars(zscobol2014_tb_mat_data2, cU("MATERIALNUMBER"), veri_data1, sizeof(veri_data1), &error); printfU(cU("MATNR : %s\n"), veri_data1); } RfcGetString(funcHandle, cU("REQUTEXT"), requtext, 256, &length, NULL); printfU(cU("\nReceived request for ZCOBOL_C2013COSTCENTER_RFC_OUT\n")); printfU(cU("REQUTEXT = %s\n"), requtext); RfcGetConnectionAttributes(rfcHandle, &attributes, NULL); printfU(cU("User is %s. Let's see what we can find out about him/her.\n"), attributes.user); RfcSetString(bapiUserGetDetail, cU("USERNAME"), attributes.user, strlenU(attributes.user), NULL); RfcGetStructure(bapiUserGetDetail, cU("ADDRESS"), &address, NULL); RfcSetString(address, cU("FULLNAME"), cU(" "), 1, NULL); RfcGetStructure(bapiUserGetDetail, cU("COMPANY"), &company, NULL); RfcSetString(company, cU("COMPANY"), cU(" "), 1, NULL); rc = RfcInvoke(rfcHandle, bapiUserGetDetail, errorInfo); if (rc != RFC_OK) { memcpy(echotext, cU("Unfortunately calling BAPI_USER_GET_DETAIL failed:"), 50); memcpy(resptext, errorInfo->message, strlenU(errorInfo->message)); errorInfo->message[0] = 0; errorInfo->code = RFC_OK; } else { memcpy(echotext, cU("Hi, you are "), 12); RfcGetStructure(bapiUserGetDetail, cU("ADDRESS"), &address, NULL); RfcGetString(address, cU("FULLNAME"), echotext + 12, 244, &length, NULL); memcpy(resptext, cU("Your company is "), 16); RfcGetStructure(bapiUserGetDetail, cU("COMPANY"), &company, NULL); RfcGetString(company, cU("COMPANY"), resptext + 16, 241, &length, NULL); } RfcSetString(funcHandle, cU("RESPTEXT"), resptext, strlenU(resptext), NULL); RfcSetString(funcHandle, cU("ECHOTEXT"), echotext, strlenU(echotext), NULL); printfU(cU("Do you want to stop listening after this request? [y/n] ")); rsize_t RSIZE_MAX = 1024; // TODO Нарушается стек, исправить! fgetsU(requtext, RSIZE_MAX, stdin); // getsU(requtext); if (*requtext == cU('y')) func.listening = 0; return RFC_OK; } int main(int argc, SAP_UC** argv) { func.listening = 1; RFC_RC rc = RFC_OK; RFC_ERROR_INFO errorInfo; RFC_CONNECTION_PARAMETER gatewayParams[1]; RFC_CONNECTION_HANDLE serverHandle1 = NULL, serverHandle2 = NULL; RFC_CONNECTION_HANDLE connection; RFC_FUNCTION_DESC_HANDLE desc1, desc2, desc3; //RFC_CONNECTION_PARAMETER loginParams[1]; RFC_ATTRIBUTES attributes; //loginParams->name = cU("dest"); //loginParams->value = cU("BIN2"); //SAP_UC* system1 = NULL; // = ("DEV"); //SAP_UC* system2 = cU("MBS_REG"); gatewayParams[0].name = cU("dest"); gatewayParams[0].value = cU("DEV"); // system1; // Получаем мета от функции connection = RfcOpenConnection(gatewayParams, 1, &errorInfo); if (connection == NULL) { printfU(cU("Error during logon: %s\n"), errorInfo.message); printfU(cU("Please check that the sapnwrfc.ini file is in the current\nworking directory and the logon parameters are ok.\n")); return 1; } rc = RfcGetConnectionAttributes(connection, &attributes, NULL); if (rc == RFC_OK) printfU(cU("Successfully logged on to destination (System-ID %s)\n"), attributes.sysId); desc1 = RfcGetFunctionDesc(connection, cU("ZCOBOL_C2014_MAT_RFC_OUT"), &errorInfo); desc2 = RfcGetFunctionDesc(connection, cU("BAPI_USER_GET_DETAIL"), &errorInfo); desc3 = RfcGetFunctionDesc(connection, cU("ZCOBOL_C2013COSTCENTER_RFC_OUT"), &errorInfo); // c2013Cost_XIOut RfcCloseConnection(connection, NULL); if (desc1 == NULL || desc2 == NULL || desc3 == NULL) { printfU(cU("Error getting metadata: %s\n"), errorInfo.message); return 1; } // мета rc = RfcInstallServerFunction(NULL, desc1, c2014MatTB_PIOut, &errorInfo); if (rc != RFC_OK) { printfU(cU("Unable to install RequestHandler: %s: %s\n"), RfcGetRcAsString(errorInfo.code), errorInfo.message); return 1; } bapiUserGetDetail = RfcCreateFunction(desc2, &errorInfo); if (bapiUserGetDetail == NULL) { printfU(cU("Error creating data container: %s\n"), errorInfo.message); return 1; } rc = RfcInstallServerFunction(NULL, desc3, c2013Cost_XIOut, &errorInfo); if (rc != RFC_OK) { printfU(cU("Unable to install RequestHandler: %s: %s\n"), RfcGetRcAsString(errorInfo.code), errorInfo.message); return 1; } rc = RfcInstallGenericServerFunction(&func.genericRequestHandler, &func.repositoryLookup, &errorInfo); if (rc != RFC_OK) { printfU(cU("Unable to install RequestHandler: %s: %s\n"), RfcGetRcAsString(errorInfo.code), errorInfo.message); return 1; } serverHandle1 = RfcRegisterServer(gatewayParams, 1, &errorInfo); if (serverHandle1 == NULL) printfU(cU("Unable to register at %s: %s: %s\n"), gatewayParams[0].value, RfcGetRcAsString(errorInfo.code), errorInfo.message); else printfU(cU("Successfully registered at destination %s\n"), gatewayParams[0].value); //gatewayParams[0].value = system2; //serverHandle2 = RfcRegisterServer(gatewayParams, 1, &errorInfo); //if (serverHandle2 == NULL){ // printfU(cU("Unable to register at %s: %s: %s\n"), system2, RfcGetRcAsString(errorInfo.code), errorInfo.message); // if (serverHandle1 == NULL) return 1; //} //else printfU(cU("Successfully registered at %s\n"), system2); printfU(cU("\nWaiting for request...\n")); while (serverHandle1 != NULL || serverHandle2 != NULL) { gatewayParams->value = gatewayParams[0].value; if (serverHandle1 != NULL) func.listen(&serverHandle1, gatewayParams); //gatewayParams->value = system2; //if (serverHandle2 != NULL) listen(&serverHandle2, gatewayParams); if (!func.listening) break; } if (serverHandle1 != NULL) RfcCloseConnection(serverHandle1, NULL); // if (serverHandle2 != NULL) RfcCloseConnection(serverHandle2, NULL); return 0; } |