Created On:  26 April 2012

Problem:

Converting a native Net Express Dialog System application to Visual COBOL.
Visual COBOL application uses a managed main program which does a platform invoke call to a native Dialog System program using the Dialog System Addpack.

The Dialog System screenset contains an ActiveX control which has a control program.

The managed program calls the native program fine but when the screenset is being initialized and the control program for the ActiveX control is being executed an error occurs in Create-ActiveX-Instance section on:

   invoke wsEventManager "initializeOLE"

The error states that OLE has already been initialized using a different threading model.
Why is this ocurring?

Resolution:

When mixing .NET managed and native/ActiveX the customer will need to understand how COM threading models work.
.NET uses MTA threads as the default, whereas ActiveX requires STA.

When using .NET as the main program this needs to be marked as an STA thread.

In COBOL you would need to create a static class method rather than use procedural COBOL.

Example:

       class-id managedclass. 
       working-storage section.

       method-id Progam static attribute STAThread().
       local-storage section.
       procedure division.

           call "NATIVEDS"
           goback.
       end method.

       end class.

Alternatively, consider using a Win Form template and just delete the Form code.

There are several articles online related to this; e.g. http://blogs.msdn.com/b/pedram/archive/2007/08/05/net-thread-apartment-and-com-interop.aspx
and indeed many large books have been written on the subject.

Dialog System applications can largely be viewed as the equivalent of a VB app in these articles.
Incident #2568175