CEE4RAGE

From MidrangeWiki
Jump to: navigation, search

The CEE4RAGE API can be used to register a procedure that will be invoked with an activation group is destroyed.

This can be used to force your own cleanup logic to be invoked when a particular program ends, regardless of how it ends.

The following example was posted to RPG400-L by Scott Klement ...

    H NOMAIN

     D Initialized     s              1N   inz(*off)

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * Initialization routine
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P InzSrvPgm       B
     D InzSrvPgm       PI

     D CEE4RAGE        PR
     D   procedure                     *   procptr const
     D   feedback                    12A   options(*omit)

      /free
         if (Initialized);
             return;
         endif;

         // .. open files here ...
         // .. other initializations ...

         CEE4RAGE(%paddr(EndSrvPgm): *OMIT);

         Initialized = *ON;
         return;
      /end-free
     P                 e

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * EndSrvPgm():  This is called automatically when the activation
      *               group ends.  (which is when the srvpgm is removed
      *               from memory.)
      *
      *  Note that this must be exported so that CEE4RAGE can call it.
      *
      * InzSrvPgm() registers this procedure with the CEE4RAGE() API
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P EndSrvPgm       B                   export
     D EndSrvPgm       PI
     D   AgMark                      10U 0 options(*nopass)
     D   Reason                      10U 0 options(*nopass)
     D   Result                      10U 0 options(*nopass)
     D   UserRC                      10U 0 options(*nopass)
      /free

           // ... terminate any helper processes
           // ... shut down any persistent APIs
           // ... close files.
           // ... deallocate memory.

           Initialized = *OFF;
           return;
      /end-free
     P                 E


      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      *  Every exported procedure (besides EndSrvPgm) starts by calling
      *  the InzSrvPgm() subprocedure
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P SomeProc        B                   export
     D SomeProc        PI            10I 0
     D   SomeParm                   123A   const

      /free

            InzSrvPgm();

            // ... perform function of subproc here ...
            return data;

      /end-free
     P                 E


      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      *  Every exported procedure (besides EndSrvPgm) starts by calling
      *  the InzSrvPgm() subprocedure
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P AnotherProc     B                   export
     D AnotherProc     PI            10I 0
     D   AnotherParm                321A   const

      /free

            InzSrvPgm();

            // ... perform function of subproc here ...

            return data;

      /end-free
     P                 E

For more information on the API visit Register Activation Group Exit Procedure (CEE4RAGE) API