Calling a .NET Class from Native COBOL, Using CCW

The COM-callable-wrapper (CCW) is a .NET technique for COM interoperability. CCW wraps managed code as a COM object so that native code can call it like any other COM object.

It effectively exposes a .NET managed class in any language as a COM interface. For more background, see the Visual Studio Help topic COM Callable Wrapper.

To expose a .NET class as a COM object, you configure the project so that the class is registered for COM interop. When you build the project, the class is registered and thereby made available to native COBOL.

All public methods defined and implemented in the .NET class are exposed as a COM interface. Your COM client application can then invoke those methods as if they were methods of a COM object.

To call a .NET class from native COBOL

  1. In Visual Studio, open the project containing the .NET class to expose, by clicking File > Open > Project and browsing to the project.
  2. Click Project > myProject Properties.
  3. On the COBOL tab, click Advanced, and check Register for COM interop.

    The native client can now invoke methods in the .NET class as if it was any other COM object, by using the COM programmatic ID of the .NET class. This ID is defined as DefaultNamespace.Class. Where the default namespace, DefaultNamespace, is defined on the Application tab in the project properties and is by default set to the name of the project.

    For example, a native COBOL client can invoke the .NET class called ManagedCOM, as follows:

    class ManagedCOM as "$OLE$COMDemo.ManagedCOM"

To call the exposed .NET class from native COBOL

The native COBOL calls the .NET class as if it were a COM object, by including the following elements of code:

  1. Set the Checker directive OOCTRL(+P) in your program:
    $SET OOCTRL(+P)

    This checker directive enables the run-time system to map from COBOL data types to COM data types.

  2. Map each COM class to an OO COBOL proxy class in the CLASS-CONTROL paragraph. Give the COM class an OO COBOL class-name, and specify that it is in the COM domain, using a statement of the form:
    class-name IS CLASS "$OLE$windows-registry-name"

    where windows-registry-name is one of the following:

    • the ProgID for the server
    • the CLSID for the server

    The ProgID and CLSID are defined in the Windows registry.

  3. Create an instance of the OO COBOL COM class.

    Send the message "new" to the OO COBOL proxy class. This starts the COM server if it is not already running. You are returned an OO COBOL handle which you can use to send messages to the COM server.

  4. Send messages to the COM server.
  5. To access properties, prefix the property name with "Set" to set a property or "Get" to retrieve a property.

    When setting a property, pass a single parameter. When getting a property the value is returned as the RETURNING parameter.