Created On:  15 April 2011

Problem:

How can a Visual COBOL managed code .NET class be called by an unmanaged Net Express native program?

Resolution:

VCCOMDemo

This demonstration program shows how a Visual COBOL .NET managed class can be called from a Net Express (or Visual COBOL) native program. The technology used is part of the .NET Interop technology and is known as COM Callable Wrapper or CCW. Basically what happens is that the .NET class is registered as a COM server so that the native COBOL program can call it as such using the COM support available in Net Express. Actually this is not limited to being called by a Net Express application as any COM enabled client can call this server.

The demonstration consists of two parts, a managed Visual COBOL project called VCCOMServer and a native Net Express project called NXCOMClient. Please unzip the attached file VCCOMDemo.zip onto your C:\drive, retaining the folder structure of the zip file.

VCCOMServer: This class becomes a CCW by turning on the option Register for COM Interop which is found at the bottom of the COBOL tab on the Project Properties page. This option causes the compiler to generate all public methods as COM visible methods and to publish the Interface information to the registry so that any COM client can instantiate the class and call it’s methods. Parameters are automatically converted from .NET to COM types and vice versa as required.

Using Visual COBOL 2010 open up the solution C:\VCCOMDemo\VCCOMServer\VCCOMServer\VCCOMServer.sln and on the build menu select Rebuild Solution. This will register VCCOMServer.dll as a COM Server. 

NXCOMClient: This program uses the COM support in Net Express to create an instance of the VCCOMServer class and then to call its methods passing various types of parameters depending on the method.

Using Net Express 5.1 open up the project C:\VCCOMDemo\NXCOMClient\NXCOMClient.app and on the Project menu select Rebuild All.

If you wish to see what methods are available and what parameter types they are expecting you can use the Net Express Type Library Assistant. From the Tools menu select Type Library Assistant. Scroll down in the Registered Type Libraries list until you find VCCOMServer and double click it. To generate COBOL source files and copybooks, specify a copyfile name or leave it as the default TypeLib.cpy. Check the boxes for Add file to Project, Generate Interface Classes and Generate Dual Interfaces and then click the Generate button. Right click in the source pool and select add files to project and add ivccomserver.cbl. This file was generated from the Interface definition in VCCOMServer.cbl and will contain all the available method definitions.

Animate the client program in Net Express to step through the available methods. In this demo empty parameters are used in the call and then they are populated in the VCCOMServer and returned to the client. They are then displayed upon return.

Notice that in order to pass a COBOL group item to a .NET class through COM the group item must be defined as a string in the COM interface class. This is because only .NET native types can be used in this manner and a COBOL group item is not a .NET native type. The group itself can be passed in the client program and will automatically be converted to a COM BSTR type. This is received in the .NET program as a string and can then be mapped onto the group using a set statement.