Created On:  16 November 2012

Problem:

Customer is using a C# Windows Forms project in his application and they need to be able to call some native COBOL .dlls from C#.
What is the easiest method to do this in Visual COBOL?

Resolution:

The easiest method is probably to create a wrapper program using managed COBOL that C# can call directly.
The managed COBOL wrapper program would then format the parameters passed to it according to what is expected by the native COBOL program.

There is a demo program attached to this article that shows one method that this can be done.

Here is the description from the demo:

                      TestWrapper Demo

 This example program demonstrates how C# can call a native COBOL program by using a managed COBOL
 wrapper program. The managed COBOL wrapper program is compiled with the ilsmartlinkage directive
 which causes the compiler to generate a .NET class for each item in the linkage section. This allows 
 C# to create an instance on the parameter class and populate the individual elementary items of the
 COBOL record and to pass it to COBOL as a refrence parameter.

 The COBOL Runtime system will marshall the data correctly from managed COBOL to native COBOL so they
 can share the same record area in their linkage sections.

 In order to make things easy to find, both the managed wrapper and the native COBOL projects have
 their output folders set to be the same as the main C# project, testwrapper. In doing this all required 
 executable files will be output to the same folder.

 A reference has also been added in the testwrapper project to point to the COBWrapper project so that
 Intellisense will work in C# for the COBOL classes.

 Run the C# project testwrapper by pressing F5 (or step into using F11).
 Fill the data fields on the form and then click the Call COBOL button.
 The C# Click event for the button will then create a new instance of the managed COBOL class and 
 the parameter classes for LegacyRecord and LegacyStatus and will set these to the values in the form fields.
 testwrapper will then call COBWrapper and pass the parameters.
 COBWrapper will then turn around and call the native COBOL program using the same linkage section record
 that was passed by C#.

 The native COBOL program will then open a text file and write a record to it using the data that was
 passed in the linkage section. It will then set the LegacyStatus parameter to the value of the file status
 and return back to COBWrapper. COBWrapper then returns to testwrapper and the LegacyStatus is displayed
 back to the form.