RPGLE Service Program Example - iSeries (AS400)

Create a Service Program and how to use binding directory. In the following example I am going to create RPGLE functions but in reality you can have functions in any language supported by the iSeries and the calling program can be in a different language.

Declare the procedure prototype

Create a separate source so that you can use the same prototypes definition in your program as well as in the module function definition.
d Add             PR            15s 2  
d num1                          15s 2  
d num2                          15s 2  
                                       
d Substract       PR            15s 2  
d num1                          15s 2  
d num2                          15s 2  
                                       
d Multiply        PR            15s 2  
d num1                          15s 2  
d num2                          15s 2  
                                       
d Divide          PR            15s 2  
d num1                          15s 2  
d num2                          15s 2  

Declare the procedure with nomain in h-spec

A module can contain one or more procedures
h nomain                                                    
                                                            
 /copy *libl/qrpglesrc,mathpr                               
                                                            
p Add             b                   export                
d Add             PI            15s 2                       
d num1                          15s 2                       
d num2                          15s 2                       
                                                            
d result          s              6s 0                       
                                                            
 /free                                                      
          result = num1 + num2 ;                            
          return result;                                    
 /end-free                                                  
p Add             e                                         
                                                            
p Substract       b                   export                
d Substract       PI            15s 2                       
d num1                          15s 2                       
d num2                          15s 2                       
                                                            
d result          s              6s 0                        
                                             
 /free                                       
          result = num1 - num2 ;             
          return result;                     
 /end-free                                   
p Substract       e                          
                                             
p Multiply        b                   export 
d Multiply        PI            15s 2        
d num1                          15s 2        
d num2                          15s 2        
                                             
d result          s              6s 0        
                                             
 /free                                       
          result = num1 * num2 ;             
          return result;                     
 /end-free                                   
p Multiply        e                          
                                             
p Divide          b                   export 
d Divide          PI            15s 2        
d num1                          15s 2        
d num2                          15s 2
                                     
d result          s              6s 0
                                     
 /free                               
          result = num1 / num2 ;     
          return result;             
 /end-free                           
p Divide          e                  

Create the module

Use Option 15 ( CRTRPGMOD ) to compile and the output will be in type *MODULE

Creating a Binder Directory If one doesn't exist or you want a new one

CRTBNDDIR BNDDIR(your_library/MATHDIR)

Create the Binding Source (BND member type)

             STRPGMEXP  PGMLVL(*CURRENT) SIGNATURE('MYMATH')  
             EXPORT     SYMBOL('ADD')                         
             EXPORT     SYMBOL('SUBSTRACT')                   
             EXPORT     SYMBOL('MULTIPLY')                    
             EXPORT     SYMBOL('DIVIDE')                      
             ENDPGMEXP     

TIP: Please sure in future just add new functions to the end of the export list or you will have signature issues

Create the Service Program

Now that we have the function module, binding directory and the binding source we are ready to create the Service Program.
CRTSRVPGM SRVPGM(your_library/MATH) MODULE(your_library/MATH) 
SRCFILE(your_library/source_file) SRCMBR(MATHBNDSRC)

Add the Service Program to the Binding Directory

ADDBNDDIRE BNDDIR(your_library/MATHDIR) OBJ((MATH *SRVPGM))

Your Program calling the Service Program

h option(*nodebugio) bnddir('MATHDIR')                        
h dftactgrp(*NO)  actgrp('as400code')                         
                                                              
 /copy *libl/qrpglesrc,mathpr                                 
                                                              
d $num1           s             15s 2 inz(20)                 
d $num2           s             15s 2 inz(2)                  
d result          s             15s 2                         
                                                              
c                   eval      result = Add($num1:$num2)       
c     result        dsply                                     
                                                              
c                   eval      result = Substract($num1:$num2) 
c     result        dsply                                     
                                                              
c                   eval      result = Multiply($num1:$num2)  
c     result        dsply                                     
                                                              
c                   eval      result = Divide($num1:$num2)    
c     result        dsply                                     
                                                              
c                   eval      *inlr = *on     

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.