我在我的 CMake 中正确链接了库:
set(SAPNWRFC_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/nwrfcsdk/include")
set(SAPNWRFC_LIB_DIR "${PROJECT_SOURCE_DIR}/nwrfcsdk/lib")
set(SAPNWRFC_LIB_FILES sapnwrfc.lib libsapucum.lib)
target_link_directories(appsap_rtf PRIVATE ${SAPNWRFC_LIB_DIR})
target_link_libraries(appsap_rtf
PRIVATE Qt6::Quick
PRIVATE ${SAPNWRFC_LIB_FILES})
qt_add_executable(appsap_rtf companyClient.cpp)
然后我尝试了 SAP NetWeaver RFC SDK中的一个非常基本的示例:
companyClient.cpp:
#include <stdlib.h>
#include <stdio.h>
#include "nwrfcsdk/include/sapnwrfc.h"
void errorHandling(RFC_RC rc, SAP_UC* description, RFC_ERROR_INFO* errorInfo, RFC_CONNECTION_HANDLE connection){
printfU(cU("%s: %d\n"), description, rc);
printfU(cU("%s: %s\n"), errorInfo->key, errorInfo->message);
// It's better to close the TCP/IP connection cleanly, than to just let the
// backend get a "Connection reset by peer" error...
if (connection != NULL) RfcCloseConnection(connection, errorInfo);
exit(1);
}
//usage: companyClient <hostname> <system Number> <client> <user> <language> <password>
int mainU(int argc, SAP_UC** argv){
RFC_RC rc = RFC_OK;
RFC_CONNECTION_PARAMETER loginParams[6];
RFC_ERROR_INFO errorInfo;
RFC_CONNECTION_HANDLE connection;
RFC_FUNCTION_DESC_HANDLE bapiCompanyDesc;
RFC_FUNCTION_HANDLE bapiCompany;
RFC_STRUCTURE_HANDLE returnStructure;
SAP_UC message[221] = iU("");
RFC_BYTE buffer[1105];
unsigned utf8Len = 1105, resultLen;
FILE* outFile;
loginParams[0].name = cU("ashost"); loginParams[0].value = cU("****"); // hostname
loginParams[1].name = cU("sysnr"); loginParams[1].value = cU("**"); // system number
loginParams[2].name = cU("client"); loginParams[2].value = cU("***"); // client number
loginParams[3].name = cU("user"); loginParams[3].value = cU("*****"); // username
loginParams[4].name = cU("passwd"); loginParams[4].value = cU("*****"); // password
loginParams[5].name = cU("lang"); loginParams[5].value = cU("EN");
connection = RfcOpenConnection(loginParams, 6, &errorInfo);
if (connection == NULL) errorHandling(rc, cU("Error during logon"), &errorInfo, NULL);
bapiCompanyDesc = RfcGetFunctionDesc(connection, cU("BAPI_COMPANY_GETDETAIL"), &errorInfo);
if (bapiCompanyDesc == NULL) errorHandling(rc, cU("Error during metadata lookup"), &errorInfo, connection);
bapiCompany = RfcCreateFunction(bapiCompanyDesc, &errorInfo);
// Use a company ID that does not exit. 000007 should not exist in most systems.
RfcSetChars(bapiCompany, cU("COMPANYID"), cU("000007"), 6, &errorInfo);
rc = RfcInvoke(connection, bapiCompany, &errorInfo);
if (rc != RFC_OK) errorHandling(rc, cU("Error calling BAPI_COMPANY_GETDETAIL"), &errorInfo, connection);
RfcGetStructure(bapiCompany, cU("RETURN"), &returnStructure, &errorInfo);
RfcGetString(returnStructure, cU("MESSAGE"), message, 221, &resultLen, &errorInfo);
RfcDestroyFunction(bapiCompany, &errorInfo);
RfcCloseConnection(connection, NULL);
// On Windows you can use the following function from windows.h toconvert to UTF-8:
// utf8Len = WideCharToMultiByte(CP_UTF8, 0, message, strlenU(message), buffer, 1105, NULL, NULL);
// It will have a slightly better performance than RfcSAPUCToUTF8().
// On 'system i' with Japanese double byte characters using the ASCII LIBSAPNRFC the
// message string does not contain UNICODE but JIS encoded data. The call to the
// following function does not make any sense in this case. Instead you can add an
// encoding to the header lilke this: "<?xml version=\"1.0\" encoding=\"Shift-JIS\"?>\n<message>",
// and fputs the message directly some lines below:
// fwrite(buffer, 1, utf8Len, outFile); ==> fputs((char*)message, outFile);
/*CCQ_SECURE_LIB_OK*/
RfcSAPUCToUTF8(message, strlenU(message), buffer, &utf8Len, &resultLen, &errorInfo);
outFile = fopenU(cU("message.xml"), cU("w"));
if (!outFile) return 1;
/*SAPUNICODEOK_STRINGCONST*/ /*SAPUNICODEOK_LIBFCT*/
fputs("<?xml version=\"1.0\"?>\n<message>", outFile);
/*SAPUNICODEOK_LIBFCT*/
fwrite(buffer, 1, utf8Len, outFile); // See comment above for system i
/*SAPUNICODEOK_STRINGCONST*/ /*SAPUNICODEOK_LIBFCT*/
fputs("</message>", outFile);
fclose(outFile);
// Now view the message.xml file in a standard browser that supports UTF-8, and you should
// be able to view the Japanese characters.
return 0;
}
并且应用程序始终仅进行调试:
Error during logon: 0
R:
我是 100% 的登录数据,因为我从同一设备的其他应用程序使用它们(即直接将 Excel VBA 连接到 SAP RFC,而不使用此 SDK 连接器)。
唯一的区别是其他应用程序也需要
loginParameter
system
(在 SAP SDK 文档中它是 sysid
或 r3name
或 dest
;我尝试了所有这些,但结果相同)。
如何找出我的连接被拒绝且没有错误描述的原因?
如果这对任何人有帮助,则没有回复,也没有结果,因为我没有编译为 SAPwithUNICODE。 在我完成了here的问题后,我要么收到程序不喜欢的错误,要么收到积极的结果