Blog Archive

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     

13 comments :

  1. To avoid problems I should define result as 15s2 (iso 6s0)

    ReplyDelete
  2. cannot compile the last program using option 14 as well as CRTPGM command, can you please advise , Thanks

    ReplyDelete
    Replies
    1. On my machine there are two MATH objects.

      WRKOBJ MATH....
      - QSYS2/MATH and mylib/MATH

      probably, the first one is the IBM default object.

      make sure your MATH (*SRVPGM) is exist.

      WRKBNDDIR MATHDIR....make sure your *SRVPGM is on the list.


      Delete
  3. Any way I'll be subscribing to your feed and I hope you post again soon.
    Signature:
    i like play happy wheels demo online and play happy wheels games full friv4school , games for girls , games go girls

    ReplyDelete
  4. All the best blogs that is very useful for keeping me share the ideas
    of the future as well this is really what I was looking for, and I am
    very happy to come here. Thank you very much
    earn to die
    earn to die 2
    earn to die 3
    Hi! I’ve been reading your blog for a while now and finally got the
    earn to die 4
    courage to go ahead and give youu a shout out from
    earn to die 6
    Austin Texas! Just wanted to tell
    earn to die 5
    Hi! I’ve been reading your blog for a while now and finally got the
    happy wheels
    strike force heroes
    slitherio
    you keep up the fantastic work!my weblog
    age of war
    earn to die 5
    good game empire
    tank trouble
    tank trouble 2
    strike force heroes

    ReplyDelete
  5. Good blog post. I want to thank you for interesting and helpful information and I like your point of view. Thank you!
    slither io | wings io | science kombat | tank trouble 4

    ReplyDelete
  6. I was working and suddenly I visits your site frequently and recommended it to me to read also. Age Of War 2
    Big Farm | Slitherio
    Tank Trouble | Happy Wheels
    Goodgeme Empire | Slither.io

    ReplyDelete
  7. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

    Discover our website bounty of free online games now!
    Our website has the biggest collection of free online games. Totally new games are added every day!

    age of war 2
    gold Miner 2
    unfair Mario 2
    cubefield 2
    tanki Online 2

    ReplyDelete
  8. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

    Discover our website bounty of free online games now!
    Our website has the biggest collection of free online games. Totally new games are added every day!

    age of war 2
    gold Miner 2
    unfair Mario 2
    cubefield 2
    tanki Online 2

    ReplyDelete