cs2cob

Upload: wolalo

Post on 03-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 CS2COB

    1/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 ofdefinin$ data t&pes than C'(') does. *hat .NET pro+ides thou$h is a means toenable 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 fromC . t also pro+ides an e,ample of callin$ an ob%ect oriented C'(') class fromC / 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'(') code usin$ an interface wouldre1uire no chan$es to the ori$inal procedural code. *e ha+e elected to

    demonstrate the interface method. The interface method creates an ob%ectoriented C'(') pro$ram that utilizes the standard .NET data t&pes as inputparameters but then chan$es! or marshalls! the data to standard C'(') datat&pes 34 C 5! 4 C 67 before callin$ the procedural C'(') pro$ram. 0$ain! theinterface 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 otherlan$ua$es see the topic 8 Interoperating with Managed Code in Other Languagesin the Micro Focus Net E,press 9elp.

    To be$in! here is the C screen we created:

    C# Calling COBOL Page 1

  • 8/12/2019 CS2COB

    2/4

    *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 themethods are static there is no additional codin$ that has to be added to theC code to instantiate the C'(') class. *ithin the EFE ENCEs for theC pro%ect the user has to add references to the C'(') interface pro$ram! theMicro Focus C'(') en+ironment and the modules the interface pro$ram will becallin$.

    C# Calling COBOL Page 2

  • 8/12/2019 CS2COB

    3/4

    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&pe7and stores them in the newl& declared +ariables. The e+ent then calls themethod 80??E < in the interface pro$ram. The method name is the same as thepro$ram name the interface will be callin$. (& usin$ the same name for boththe 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 tothe C'(') module. @ EF@ instructs C to work with the same +ariable as the

    C# Calling COBOL Page 3

  • 8/12/2019 CS2COB

    4/4

    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& bedeclared but must be initialized.

    0nother method would be to use the @'AT@ parameter but this would thenha+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$ andsee how the code is to be structured. 'ne +er& important item to remind &ouof 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 whenstartin$ in the .NET en+ironment. 'nce &ou $et used to the en+ironment thou$hthose issues will $o awa&/

    Happy Coding!

    C# Calling COBOL Page 4