Wednesday, 11 April 2007

BAPI / RFC with Delphi 6

A sad story with a happy end.

I wanted to write a small application that would update some Oracle tables based on data read from SAP using Delphi 6. Very soon I realized that it all came down to installing the various ActiveX components required for Delphi to connect to SAP, be able to execute functions and read the returned results.

And this is where the sad part of the story begins. Looking around at various sources I could find some instructions explaining how to perform the required installation but none of them seemed to be accurate enough or straightforward enough in order to produce the desired results. In my opinion the problem occurs because our German colleagues who provided the "how-to's" tried to translate the menus from their German Delphi to English and that caused the misunderstanding. Additionally, while following the directions provided I also run into compilations errors increasing my frustration. The following article provides my how to install SAP ActiveX controls on Delphi 6 and I hope to make it as clear as possible.

I have to admit that I owe everything to the guy who wrote and maintained the BAPI/RFC Programming with Delphi Page and to my good friend Marilena who found the page for me. The page has enough examples required in order for the average Delphi programmer to understand how to program the necessary communication. An other page that gives information regarding the pre-installation steps is this one found at SDN.

So here is the list of the steps I followed in order to install SAP ActiveX controls on my version of Delphi 6 If you are using Delphi 2006 then it you might beter have a look here.

  1. Create an additional package page to host the new components.
    1. Right click on the Delphi palette → Properties → Click the Add button.
    2. Type a name for the new page SAP will do just fine
    3. Use the Move Up and Move Down buttons to position the page at the correct position. Caution : Since the page will initially be empty it will not appear on the Delphi palette.
  2. Create an additional directory to hold the units for the new controls. In my case I created : C:\Program Files\Borland\Delphi6\Imports\SAPControls\.
  3. Go to main menu Component → Import ActiveX Control
    1. Select the SAP Logon Control Version 1.1 (wdtlog.OCX). The displayed class name should be TSAPLogonControl
    2. Select SAP as the Palette Page
    3. Change the directory of the unit file name to the new path created in the previous step.
    4. Click install. The first time select to create a new package, name it SAPControls and put it the path we are already using. The controls we will install next will be added to the package as well.
    5. Perform the same steps this time installing the TSAPBapiControl class from SAP BABPI Control Version 1.2
  4. Go to main menu Project → Import Type Library.
    1. Select SAP Remote Function Call Controll (Version 5.0). This will provide you with the TSAPFunctions, TFunction, TParameter, TExports, TImports, TStructure clases
    2. Click install and choose the SAPCOntrols package created previously.
  5. Press compile to compile the project. At this point I got many errors, but I will show you how to resolve them.
Many errors appear in functions with code like the following :
procedure TStructure.Set_Value(index: OleVariant; Param2: OleVariant);
   DefaultInterface.Set_Value(index) := Param2;
This should be corrected like this :
procedure TStructure.Set_Value(index: OleVariant; Param2: OleVariant);
   // DefaultInterface.Set_Value(index) := Param2;
   DefaultInterface.Value[index] := Param2;
The general error pattern is that there are unknown identifiers for functions of the form Set_XXX(i). These should be changed to indexed properties like XXX[i] Additionally I have had many functions not returning a value. The worst part is that the Delphi complier did not issue any warnings for some of them, so I had to look at each one separately. The worst case was the NewConnection method of the TSAPLogonControl class. The generated code was
function  TSAPLogonControl.NewConnection: IDispatch;
and it had to be corrected to
function  TSAPLogonControl.NewConnection: IDispatch;
   // had to add Result :=
   Result := DefaultInterface.NewConnection;
Failure to correct this results to all new Connections being returned null afterwards, making connections to R3 practically impossible. After you correct all the functions with warnings, you should be finished. Just press "Install" on the package dialog and you 're done The examples in BAPI RFC with Delphi page are good and they may get you started immediately. If I ever get any examples of my own I will post them in this page for future reference.


reinandang said...
This comment has been removed by the author.
reinandang said...

Dear Thanassis,

Three year ago, i have the same problem like you.
I think almost delphi programmer have the same problem with me at that time. I used SAP GUI 6.10 patch level 8 and used delphi 6. After i found the solution, I wrote an article "Connection of SAP r/3 with Delphi" to help others delphi programmer. That article, i was put it in my previous web

When the SAP publish a new SAP GUI 6.10 patch level 18 until 21, I found some of my delphi 6.0 program which used SAP ActiveX can't be compile. Then, i wrote a new revision for my article "Connection of SAP r/3 with Delphi (Revision 2 : Calling SAP ActiveX using Create Object)". You can find in
A few days ago, i trying to moved all program and used a Code Gear Delphi 2007, and i can still used my old delphi code that i wrote it in Delphi 6.0 (according my last article).

In the middle of 2007, sap publish a new SAP GUI version 7.10 patch level 1, I found a new problem again but not fatal like patch level before. Every time i call "LogOn(0,false) or LogOn(0,true)" then the SAP ActiveX in that version will be clear the entered password (password is set to blank/empty).

I thought, better all delphi programmer should be avoid to using a SAP GUI 7.10 patch level 1 above when develop application. May be, there are same code in SAP Active X is changed by SAP. Until this day, i still used SAP GUI 6.40 patch 21, and never found any error. :-)

I hope this information can help other delphi programmer.

Best Regards,

Albertus Reinandang