Anda di halaman 1dari 4

Calling COBOL from C#

Source Code: mfcs2cob.zip



The Microsoft .NET Framework is built upon the C! pronounced "c sharp#!
lan$ua$e. This "new# lan$ua$e is ob%ect oriented and has a different manner of
definin$ data t&pes than C'(') does. *hat .NET pro+ides thou$h is a means to
enable businesses to use their e,istin$ C'(') business lo$ic while enhancin$
the applications the& support with new front-ends de+eloped in C.
This article e,plores callin$ a standard procedural based C'(') pro$ram from
C. .t also pro+ides an e,ample of callin$ an ob%ect oriented C'(') class from
C/ There are two methods that can be emplo&ed when callin$ a procedural C'(')
pro$ra: a direct call or a call throu$h an interface pro$ram. 0 direct call ma&
re1uire chan$es to the ori$inal procedural C'(') code2 usin$ an interface would
re1uire no chan$es to the ori$inal procedural code. *e ha+e elected to
demonstrate the interface method. The interface method creates an ob%ect
oriented C'(') pro$ram that utilizes the standard .NET data t&pes as input
parameters but then chan$es! or marshalls! the data to standard C'(') data
t&pes 34.C 5! 4.C 67 before callin$ the procedural C'(') pro$ram. 0$ain! the
interface method +irtuall& eliminates codin$ chan$es to the procedural C'(')
pro$rams. For a more indepth e,planation of how to work with code in other
lan$ua$es see the topic 8Interoperating with Managed Code in Other Languages
in the Micro Focus Net E,press 9elp.
To be$in! here is the C screen we created:
C# Calling COBOL Page 1
*e will assume &ou are comfortable with the C lan$ua$e. .f &ou ha+e an&
1uestions in this area please consult with one of &our local C e,perts.
Since the code we are callin$ is an 'b%ect 'riented C'(') module and the
methods are static there is no additional codin$ that has to be added to the
C code to instantiate the C'(') class. *ithin the :EFE:ENCEs for the
C pro%ect the user has to add references to the C'(') interface pro$ram! the
Micro Focus C'(') en+ironment and the modules the interface pro$ram will be
callin$.
C# Calling COBOL Page 2

To in+oke the interface class that will in turn call the procedural C'(')
pro$ram we use the 8button;click< e+ent. The code to enable this is:

The sub routine first declares three +ariables of inte$er data t&pe and then
con+erts the input the user has entered to .NT=> 3a numeric .NET data t&pe7
and stores them in the newl& declared +ariables. The e+ent then calls the
method 80??E:< in the interface pro$ram. The method name is the same as the
pro$ram name the interface will be callin$. (& usin$ the same name for both
the MET9'?-.? and the procedural C'(') the pro$rammer will find this to be
+er& eas& to remember and if maintenance is re1uired will know immediatel&
where to $o in the interface pro$ram to make the necessar& chan$es.
Notice the presence of the @ref@ attribute on the +ariables bein$ passed to
the C'(') module. @:EF@ instructs C to work with the same +ariable as the
C# Calling COBOL Page 3
one bein$ used in the function call! not %ust a +ariable that has the same
+alue. The onl& issue when usin$ @:EF@ all +ariables must not onl& be
declared but must be initialized.
0nother method would be to use the @'AT@ parameter but this would then
ha+e re1uired a chan$e to the C'(') module to add the custom attribute
@'ut0ttribute@ to the third parameter.
Wrap-Up
The B.4 file has all the necessar& source code for &ou to follow alon$ and
see how the code is to be structured. 'ne +er& important item to remind &ou
of is the need to add in references to other pro%ects when &ou are passin$
control between them. This is probabl& one of the most common errors when
startin$ in the .NET en+ironment. 'nce &ou $et used to the en+ironment thou$h
those issues will $o awa&/
Happy Coding!
C# Calling COBOL Page 4

Anda mungkin juga menyukai