mgo
OS/400 Edition
Volume 3, Number 1 -- January 10, 2003

Call RPG Subprocedures from CL


Hey, Ted:

I have an RPG subprocedure that accepts one or two parameters and returns the name of a client. How do I reference this subprocedure from a CL module in order to get a client name for a display screen?


-- Doug


Use the Call Procedure (CALLPRC) command. The client name will come back in the RTNVAL parameter.

If you plan to call an RPG subprocedure from CL, keep these points in mind:

  • Pass the parameters by reference, not by value.
  • Use only character and packed decimal data types.
  • In the CL module, define one-byte character parameters as two-byte variables. Use the first byte only in your CL code.

Here's an example. First, here is the member of prototypes for the subprocedures that open a customer master file, retrieve a customer record, and close the customer master file.

DRtvCustomer      pr            25a         
D   PCompany                     3p 0 const 
D   PCustomer                    5p 0 const 
                                            
DOpenCustomers    pr             1a         
                                            
DCloseCustomers   pr             1a         

Here is the source code for the subprocedures.

H nomain                                                  
                                                          
Fcustomer  if   e           k disk    usropn              
                                                          
 /copy prototypes,rtvcust                                 
                                                          
PRtvCustomer      b                   export              
D                 pi            25                        
D   PCompany                          const like(Company) 
D   PCustomer                         const like(CustNbr) 
                                                          
C     Key           klist                                 
C                   kfld                    PCompany      
C                   kfld                    PCustomer     
C                                                         
C     key           chain     CustRec                     
C                   if        %found                      
C                   return    CustName                    
C                   else                     
C                   return    '*Not on file*'
C                   endif                    
                                             
P                 e                          
                                             
POpenCustomers    b                   export 
D                 pi             1a          
                                             
C                   open (e)  Customer       
C                   if        not %error     
C                   return    *off           
C                   else                     
C                   return    *on            
C                   endif                    
                                             
P                 e                          
                                             
PCloseCustomers   b                   export
D                 pi             1a         
                                            
C                   close(e)  Customer      
C                   if        not %error    
C                   return    *off          
C                   else                    
C                   return    *on           
C                   endif                   
                                            
P                 e                         

Here's part of the CL caller.

                                                       
dcl  &pcomp   *dec   3                                 
dcl  &pcust   *dec   5                                 
dcl  &status  *char  2                                 
dcl  &cname   *char 25                                 
                                                       
callprc  prc(OpenCustomers) rtnval(&status)            
if (%sst(&status 1 1) *ne '0') do                      
      sndpgmmsg  msgid(CPF9898) msgf(qcpfmsg) +        
         msgdta('Customer file could not be opened') + 
         msgtype(*escape)                              
enddo                                                  

callprc  prc(RtvCustomer) parm(&pcomp &pcust) rtnval(&cname) 

callprc  prc(CloseCustomers) rtnval(&status)                 

This example covers all three points I mentioned.

  • The const keyword in the prototype means that the company and customer number parameters are passed by reference, but they cannot be modified.
  • All data is packed decimal or character.
  • Only the first byte of the status code is referenced in the CL module.

As of V5R1, you can make the subprocedure pass one-byte values correctly by using the EXTPROC keyword in the procedure prototype. The first parameter of EXTPROC is the special value *CL. The second parameter is the name of the subprocedure.

Here is the revised prototype member.

DRtvCustomer      pr            25a                                 
D   PCompany                     3p 0 const                         
D   PCustomer                    5p 0 const                         
                                                                    
DOpenCustomers    pr             1a   extproc(*CL:'OPENCUSTOMERS')  
                                                                    
DCloseCustomers   pr             1a   extproc(*CL:'CLOSECUSTOMERS')

The CL module could define the one-byte character field properly.

dcl  &status  *char  1

if (&status *ne '0') do

-- Ted


Sponsored By
ADVANCED SYSTEMS CONCEPTS

Data Retrieval
on the AS/400 and iSeries
HAS NEVER BEEN EASIER
- Windows -
- Web -
- E-mail / FTP -
- Host Interface -

SEQUEL is the easiest, most versatile way to deliver AS/400 and iSeries data, no matter what your needs dictate! END USERS can create queries with almost no learning curve and without even knowing the database. PROGRAMMERS can get information in virtually any conceivable format, quickly and easily!

Read More / View Streaming Video


THIS ISSUE
SPONSORED BY:

ASC
WorksRight Software


BACK ISSUES

TABLE OF
CONTENTS

Call RPG Subprocedures from CL

Predicting Bad Query Performance

Reader Feedback and Insights: Another Vote for ISDB



Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris

Managing Editor
Mari Barrett

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com



Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.