Hi All,

I am working on an RPG program in which I have a need to use the QCMDEXC command to perform a CL operation (for example ADDPFM). I have been able to run the QCMDEXC command but I dont have a definite answer on how I can track any MONMSG's that come out of running the CL command? For instance, when trying to add a member to a physical file, if there is an error, I want to know what that error message is so that I can chose the next instruction on my RPG.

I do not want to use the option of calling a CL from my RPG to perform the commands. I have to get all this done with a single RPG.

Thanks for your help.

Who is Participating?
MurpheyApplication ConsultantCommented:
Use the Monitor message in your RPG Progam to catch the errormessage/code of the QCMDXC
here an example how to do that

h option(*nodebugio)                                 
d $cmp            s              2a                  
d $ord            s              6a                  
d $bocd           s              2a                  
d $session        s             24a                  
d $status         s              1a                  
d dtaq_name       s             10a                  
d dtaq_lib        s             10a                   
d dtaq_len        s              5p 0                   
d dtaq_data       s            256                      
d $web_path       s            101a                     
d $pdf_path       s            101a                     
d $path           s             50a   inz('/directory')   
d $pdf_name       s             50a                     
d $found          s              1a                     
d $count          s              3s 0                   
d @CmdStr         s            512a   inz               
d @Apostr         s              1a   inz(X'7D')        
 *  Program Information                                 
d progstatus     sds                                    
d  parms            *parms                              
d  progname         *proc                               
d  errmsgID              40     46                      
d  errmsg                91    169                      
d  jobname              244    253                      
d  userid               254    263                      
d  jobnumber            264    269                      
d ExcCmd          PR                  ExtPgm('QCMDEXC') 
d  Cmd_Str                     512a   Options(*VARSIZE)          
d                                     Const                      
d  Cmd_Len                      15p 5 Const                      
c     *entry        plist                                        
c                   parm                    $cmp                 
c                   parm                    $ord                 
c                   parm                    $bocd                
c                   parm                    $session             
c                   parm                    $status              
c                   eval      $status = *blanks                  
c                   eval      $pdf_name = %trim(userid) + '.PDF'  
c                   eval      dtaq_data = $cmp +           
c                                         $ord +           
c                                         $bocd +          
c                                         $path +          
c                                         $pdf_name        
c*--- Sending request to Data Queue to generate my PDF file                                                           
c                   call      'QSNDDTAQ'                   
c                   parm      '@INVOICE'    dtaq_name      
c                   parm      '*LIBL'       dtaq_lib       
c                   parm      256           dtaq_len       
c                   parm                    dtaq_data      
c                   eval      $path = %trim($path) + '/'   
c                   eval      @CmdStr = 'DLYJOB DLY(5)'    
c                   callp     ExcCmd(%Trim(@CmdStr) :      
c                                   %Len(%Trim(@CmdStr)))  
c                   dou       $count > 20                  
c                   call      'CHKIFS'                     
c                   parm                    $path          
c                   parm                    $pdf_name      
c                   parm                    $found         
c                   if        $found = 'Y'                         
c                   leave                                          
c                   endif                                          
c                   eval      @CmdStr = 'DLYJOB DLY(3)'            
c                   callp     ExcCmd(%Trim(@CmdStr) :              
c                                   %Len(%Trim(@CmdStr)))          
c                   eval      $count = $count + 1                  
c                   enddo                                          
c                   if        $found = 'Y'                         
c                   eval      $web_path = '/MySite/InvoicePdf/' +  
c                                         %trim($session) + '.pdf' 
c                   eval      $pdf_path = %trim($path) +           
c                                         %trim($pdf_name)         
c                   eval      @CmdStr = 'CPY OBJ(' + @Apostr +     
c                                       %trim($pdf_path) +         
c                                       @Apostr + ') TOOBJ(' +     
c                                       @Apostr +                  
c                                       %trim($web_path) +         
c                                       @Apostr + ') REPLACE(*YES)'
    $count = 0;                                                    
    dou $count > 10;                                               
          callp ExcCmd(%Trim(@CmdStr) :                            
          when errmsgID = 'CPFA09E';                               
             callp ExcCmd('DLYJOB DLY(3)':13);                     
             $count = $count + 1;                                  
      $status = 'Y';                                               
c                   endif                 
c                   eval      *inlr = *on 
c                   return  

Open in new window

You can't use MONMSG to monitor for messages from commands that are executed by QCMDEXC.

I do not want to use the option of calling a CL from my RPG to perform the commands.

If you're certain that you want to do it the hard way, then you'll need to learn how to use various Message Handling APIs.

You'll probably need to learn how to use the Send Program Message (QMHSNDPM) API to set a marker message in the job's message queue before calling QCMDEXC. After QCMDEXC returns, use the Receive Program Message (QMHRCVPM) API to receive messages that were sent after your marker message and analyze each one.

It'll be up to your code to determine which ones that you receive are ones that need to be acted upon.

If you know you're going to execute specific commands, it's much cleaner just to write and call into a CL procedure. CL already provides everything that you'll need. Why code a replacement for the CL run-time that already exists in the system? The CL will be a lot simpler than RPG code to do the same things and much easier to maintain. A CL module would also be compiled and far more efficient than having QCMDEXC interpret the command.

bhagataliAuthor Commented:
Thanks Tom, I completely agree with what you say about the CL. I am a big fan of doing it in CL too but in this case I have been asked to stick to RPG. I guess, now would be as good as any time for me to learn those message handling API's :).
When you put a 'marker' message onto a program or job message queue, you need to retrieve and save its 'message mark'. Later, when you need to retrieve messages, use the saved 'message mark' as your starting point. Retrieve "next" messages in order by using each message's mark to get the next one.

That usually helps you to get messages even from program message queues after that program is no longer on the call stack. It can get ugly, but that's life.

MONITOR can be used to catch *ESCAPE messages from QCMDEXC. But depending on whether your program needs to know the cause of the problem or not, you can still need to set a marker message and receive and analyze messages as noted above.

For example, you might find a CPF2817 saying 'Copy command ended because of error.' In order for your program to find the "error", you have to 'See the messages previously listed.' There can be a lot of messages like that.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.