Calling out to a C++ class from the Progress ABL

On a programmer mailing list, there was a question about how to call out to a class in C++ from the Progress ABL. The following gives an example of doing so.

First, here is a bit of C++ code that hopefully illustrates a wrapper on how to call out to a class from the Progress interface (explanation follows):

------------ 2.cpp -----------


extern "C" {

int TestRoutine () ;

}




class A {

 private :

 int A1;

 public :

 int SetA1 (int V) {

   A1 = V;

 }

 int GetA1 () {

   return A1;

 }

}; // class A


int TestRoutine () {

 int returnvalue;

 A *B = new A();

 B->SetA1(1);

 returnvalue = B->GetA1();

 delete B;

 return returnvalue;

} // TestRoutine

First, there is a extern "C" declaration for TestRoutine. This means in the library, use the CDECL format for it's entry point. This way the function won't be wrapped up in C++ library goods. It is how one mixes C and C++ code.

Next, we have a very simple class named "A" that has a setter and a getter for a private variable A1.

Following that class, we have the function that will be the entry point for the Progress ABL code called TestRoutine. It is a simple routine where we instance an object of A, set it's value to 1, pull back it's return value, delete the instance (important for memory leaks!), and finally return that value back to the ABL.

If you were compiling something up with a C++ library - simply link your extern C functions linked to the shared library for the classes with the proper header files and you should have a library that links to a library - that would be the library you use at the Progress level.

Here is an example of the Progress ABL code that can reach into the library and call TestRoutine (explanation follows):

------ 2.p ---------

procedure  TestRoutine external "/tmp/cpp/2.so" CDECL:

 define return parameter i as short.

end.

define variable V as integer.

run TestRoutine (output V).

display V.

Note we create a procedure but mention it is "external" and can be found in the file 2.so and that it has an entry format of CDECL. Since there are no input parameters, we only define the return parameter that the return statement in TestRoutine will send back to the ABL.

Next we defile a variable for holding that in the ABL.

Then we simply call into the shared library like a normal Progress procedure via the definition for it previously spoken of.

Then we display it out!

A lot more information can be found in the document "Programming Interfaces" downloadable from the Progress web site for your version of Progress.

I merely illustrate that it is possible to call C++ code from Progress, but you will need a bit of a wrapper for it.

----- Misc: makefile used on Linux -----

build:
 g++ -shared -o 2.so 2.cpp