Wednesday, June 5, 2013

Compiling the SAP NW RFC SDK C/C++ samples on windows XP with visual studio 2005


Hi Folks,
This is a heavy one. I was away for many months, so you deserve a special treat now.

Installing and recompiling the sap nw rfc sdk samples.

In on sentence: a nightmare. I was a c programmer in the past so i definitely have an understanding what it is about, also in the windows environment. And i interfaced several tool to my own c/c++ dll's in order to acces our custom hardware. And based on that experience i can say only one thing: this is the worst experience i had with such an interfacing tool/library.

First Step: understand what it is about. On sdn you find three articles and a nice pdf  "NW_RFC_SDK_Guide.pdf"
"Improve Communication Between Your C_C++ Applications and%20SAP Systems with SAP NetWeaver RFC SDK*"
Just google them and get an idea of the library and interface they expose.
Second step: get the NW RFC SDK. on service.sap.com there are several notes. After searching and searching and searching you get your way through to the file "NWRFC_15-20004566.SAR"

Note: i unzipped it with some difficulty because the version of sapcar i downloaded from service.sap.com did not work. I had to download an older one ( always from the sap service site) and voilà it worked. Now i put the pdf files and the rfc sdk in this file for your pleasure. Please check the sap site for updated versions.

Third step: i unzipped everything to d:\nwrfcsdk
It looks like this


Fourth step: check whether the binaries in the bin directory work.

In order to do so i went on to the sap system. I read the documentation about RFC sdk and about sap gateways. I soon realized that, not having access the the externa sap gateway configuration in our system landscape, i could have not done any serious test installing an RFC server on an application server. So i fell back on the frontend. I decided that from then on the aim of my life would have been to start a calculator ( calc.exe) on my pc running an abap report and without COM and OCX stuff in between.

Step 5: copy calc.exe from the windows folder to the sapgui folder: on my pc C:\Programs\SAP\FrontEnd\SAPgui
Step 6: copy rfcexec.exe from the bin directory to the sapgui directory. It is not clean copying the files around, but not knowing the environment the SAPGui sees when it runs the rfcexec program i prefered to do so.
Step 7: create an RFC in SM59 that point to the server program on my frontend.



Step 7: test that it works ( remember we not have yet recompiled anything !!)
Push the connection test button. The console application should start and appear in a nice black window. like this.

The test should return something like this


 Now that it works if i recompile rfcexec from its source and i just replace the program in the sapgui directory with my recompiled versione it should work.
Step 8: understand how to build that things. I read note 1056696. Following the indication of the readme.txt I actually would have expected some step by step instuctions not really scattered across the universe as they actually are. But at least companies pay B$ for that...
here is the saptechno link.
Being on windows 32bit here is what i have to do
  • Windows (x86, 32 bit)
                       cl -DBCDASM -nologo -Od -Ob1 -fp:strict -Gy -GF -EHs -Z7 -W3 -Wp64 -D_X86_ -DWIN32 -DSAPwithUNICODE -DUNICODE -D_UNICODE -MD -D_AFXDLL -FR -J -RTC1 -D_CRT_NON_CONFORMING_SWPRINTFS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -DSAPonNT -c /EHc- /TP .cpp
                       link -nologo /NXCOMPAT -STACK:0x800000  ole32.lib rpcrt4.lib oleaut32.lib oledb.lib uuid.lib kernel32.lib advapi32.lib user32.lib gdi32.lib winspool.lib ws2_32.lib Iphlpapi.lib netapi32.lib comdlg32.lib shell32.lib dbghelp.lib version.lib mpr.lib secur32.lib -OPT:REF -LARGEADDRESSAWARE -subsystem:console -out:.exe .obj sapnwrfc.lib libsapucum.lib [and possibly sapdecfICUlib.lib] 
  • Windows
                       Microsoft Visual Studio 2005 SP1
                       When using Microsoft Visual Studio 2008, please note the following limitation: the functions from libsapucum.dll (sapuc.h) cannot be used with VS 2008. Instead of these functions use the standard Microsoft w-functions (wchar.h). (On Windows the datatype "SAP_UC" is equivalent to "wchar_t".) For example, instead of printfU() use wprintf(), instead of getcU() use _getws().

So i think: great. I'll need the old compiler versions then.

Step 9: retrieve an obsolete compiler version. I asked a friend of mine to help me. He found the DVD somehow. Some might say you can look it  up as a torrent, mount the iso with deamon tools and install it. But it is not me ;-)
So i installed the Visual studio 2005.

Step 10: install SP1. This step is fundamental. I've have spent two days figuring out why my exe did not work. Then i thought of the missing SP. I installed it and voilà i could recompile it and it worked. You can get SP1 on the microsoft site.

Step 11: create a project in visual studio.One could have created a little makefile to recompile it. I was lazy and requested the Nice devstudio gui to do the work for me.
I create a new project Win 32 console "from existing code"

 The i ask for an "external build system"






The next screen i leave empty. I'll fill it later
Now from the project properties i enter the commands to compile. (This is really brute force, don't do that at home. Create a makefile instead. :) )
In the properties in the nmake command line
i enter the following
cl -DBCDASM -nologo -Od -Ob1 -fp:strict -Gy -GF -EHs -Z7 -W3 -Wp64 -D_X86_ -DWIN32 -DSAPwithUNICODE -DUNICODE -D_UNICODE -MD -D_AFXDLL -FR -J -RTC1 -D_CRT_NON_CONFORMING_SWPRINTFS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -DSAPonNT -c /EHc- /TP rfcexec.cpp
   link -nologo /NXCOMPAT -STACK:0x800000  ole32.lib rpcrt4.lib oleaut32.lib oledb.lib uuid.lib kernel32.lib advapi32.lib user32.lib gdi32.lib winspool.lib ws2_32.lib Iphlpapi.lib netapi32.lib comdlg32.lib shell32.lib dbghelp.lib version.lib mpr.lib secur32.lib -OPT:REF -LARGEADDRESSAWARE -subsystem:console -out:rfcexec.exe rfcexec.obj ..\lib\sapnwrfc.lib ..\lib\libsapucum.lib










 Note that i coded the lib paths relative to the curretn path. I did not want to mess around with setting the paths correctly. i will take time later to make it via vcvars32 or similar. ../lib is fine for now.

It now looks like this



Step 12: I save and i compile all. (F7 or menu build-build all)
Step 13: I realize i have no include paths. The quick and dirty solution is to hard code the path like in the next picture. Again don't...


Now it compiles.
Step 13: copy the exe file and the manifest file into the sapgui directory


 Without the manifest it does not load the CRT (C run time) dll's so it is definitely needed.

Step 14:I run the RFC test and i see that it is loaded by the sap gui as before happened with the rfcexec.exe provided as a compiled binary. I need no pic. you trust me don't you? :)

Step 15: comment the autorization check in order to run anything not only forn SAPL... program. The standard has a restriction the allows only programs to call rfcexec if it comes from SAPLEDI7
I don't want that. so i by pass it in the coding as shown below.



 Step 16: create a report to call the RFC. Yes our aim is to call an rfc in sap and get an rfc server on our frontend programmed in C to be called. Here is the report.



 Step 17: run the report. I set the parameters as shown.
 

 Step 18: press Run and get the result: Yes!!! Yes finally SAP can run a calcolator!!!!



Step 19: The trace file lands in the "Mydocuments/SAP directory" I had to do a search all drives to get it.

***** Rfcexec trace file opened at Wed Jun 05 15:59:32 2013 My real own compiled version!!! NW RFC SDK Version: 720 Patch 15 ***** Current working directory: D:\xxxxxxx\My Documents\SAP Using default mode. Allowing connections only from Report * and System: * ---------- RfcExecServer created successfully Received call for: RFC_REMOTE_EXEC User: xxxxxxxx SysID: xxx Client: 100 Program: calc.exe Calling Report: ZTESTZ Number of arguments: 0 Process started successfully Communication Failure: connection closed without message (CM_NO_DATA_RECEIVED) RfcExecServer is shutting down ***** Rfcexec trace file closed at Wed Jun 05 15:59:32 2013
Conclusions: it is a real mess. Specially figuring out how to create the project, compile it. Leave alone that there is no debugging support. I read in some note that debugging information must be left out of the exec in order for it to run. No information is given on the initial handshake between the RFC library and the server EXE. So if the exe does not start at all and does not write anything in the trace file your are ON YOUR OWN.
I don't understand: given the fact that the note states several platforms and development systems as "supported", why do they not provide a makefile for all platforms...and detailed instructions.