What iSeries library is a CL program loaded from?

Most often when you develop a CL program, it is in order to use it as a ‘driver’ program for a RPG/ILE RPG/COBOL/C program. In order to make sure that the RPG/ILE RPG/COBOL/C program is called correctly you have to add the library containing the CL and RPG/ILE RPG program in question, or you can hard-code the library on the CALL command.

This is, however, not a flexible solution, as the CL and RPG/ILE RPG program cannot be copied to other libraries because the CL program will always call the RPG/ILE RPG program in the library hard-coded on the CALL command :-

Fortunately help is on it’s way 🙂

This tip is about how you can retrieve the library from which the CL program was loaded and then having OVRDBF, OVRDSPF, OVRPRTF and CALL commands point to the right library.

All it takes is a few lines of code:

/*                                                                 */
/* To be placed in DCL section of program.                         */
/*                                                                 */
             DCL        VAR(&MSGDTA) TYPE(*CHAR) LEN(100)            
             DCL        VAR(&PGMLIB) TYPE(*CHAR) LEN(10)             
/*                                                                 */
/* Find name of the library that this program is loaded from.      */
/*                                                                 */
             OVRDBF     FILE(QPPGMDMP) TOFILE(QTEMP/XYZXYZABC) +
                          OVRSCOPE(*JOB)
             DMPCLPGM                                                
             MONMSG     MSGID(CPF0570) EXEC(DO)                      
             RCVMSG     MSGTYPE(*DIAG)                               
             RCVMSG     MSGTYPE(*EXCP) MSGDTA(&MSGDTA)               
             CHGVAR     VAR(&PGMLIB) VALUE(%SST(&MSGDTA 11 10))      
             ENDDO                                                   
             DLTOVR     FILE(QPPGMDMP) LVL(*JOB)
/*                                                                 */
/* Now, &PGMLIB contains the library from where this program was */
/* loaded.                                                         */
/*                                                                 */

Place the definition of the two fields in the DCL section of your CL program and place the code in the beginning of the CL program code.

After the code has run, the variable &PGMLIB contains the library from which the CL program was loaded.

The clue is the DMPCLPGM command. First we override it’s output file to a non-existing file, and then issue the command. As the files does not exists, the DMPCLPGM command fails and sends the *Escape message CPF0570. We retrieve this message as it holds the library from which the CL program was loaded. Simple as can be 🙂

Leave a Reply