See the CCW example, which is with the Visual Studio examples in the examples folder of the installation of your COBOL development system. This example contains a .NET solution and a COBOL application.
The .NET solution ManagedCOM contains the following programs:
- ManagedCOM.cbl. This simple .NET class implements an interface defined in InterfaceDef.cbl. The project has been configured to register this class as a COM object. See Project > ManagedCOMProperties > Configuration Properties > Build > Register for COM interop.
- InterfaceDef.cbl. This is the definition of the interface which is implemented by the .NET class in ManagedCOM.cbl. The methods are all public by default and so they are exposed by the COM object registered by the build process. The interface will be exposed by the COM object as ICustomInterface.
- PrivateClass.cbl. By default, all public classes are exposed to COM clients when registering for COM interop and this is not always desirable. By using the class level attribute ComVisible we can hide this class.
The project has been configured so that when you build it, it will:
- Register the ManagedCOM class as a COM object. See Project > ManagedCOMProperties > Configuration Properties > Build > Register for COM interop.
- Set COMDemo as the default namespace to use for the classes in this assembly. See Common Properties > General.
The COBOL application client.app contains the following program:
- callcom.cbl. This is unmanaged COBOL and not part of the project. This is the client program that uses the .NET class ManagedCOM.cbl.
This program invokes methods in the .NET class as if the class was any other COM object, by using the COM programmatic ID of the .NET class. This ID takes the form DefaultNamespace.Class. Where DefaultNamespace is defined as described above and Class is the name of the source file containing the class.
When you use COM objects in COBOL you prefix the COM class name (or prog ID) with $OLE$. So the program refers to the class as:
class ManagedCOM as "$OLE$COMDemo.ManagedCOM"
To run the example:
- In Visual Studio, open the solution ManagedCOM.sln and rebuild it.
- In Net Express, animate the program callcom and step through it.