Get Program Name in CL

Examples of retrieving program name in CL.

The trick is to make a program send a message to itself and retrieve the program name from the message about the sender. The following code works in OPM CL programs, ILE CL programs, and ILE CL modules.

dcl   &MsgKey        *char      4                               
dcl   &PgmName       *char     10                               
dcl   &Sender        *char     80                               
                                                                
sndpgmmsg msg(' ') topgmq(*same) msgtype(*info) keyvar(&msgkey) 
rcvmsg    pgmq(*same) msgtype(*info) sender(&sender) rmv(*yes)     
chgvar    &PgmName   %sst(&Sender 56 10) 
    

In the case of CL modules, you should know that this routine returns the name of the program in which this module is bound, not the name of the module. In ILE modules, try the following code to retrieve both program name and module name.

dcl   &MsgKey        *char      4                             
dcl   &PgmName       *char     10                             
dcl   &ModName       *char     10                             
dcl   &Sender        *char    720                             
                                                              
sndpgmmsg msg(' ') topgmq(*same) msgtype(*info) keyvar(&msgkey
rcvmsg    pgmq(*same) msgtype(*info) rmv(*yes) +              
             sender(&sender) senderfmt(*long)                 
chgvar    &PgmName   %sst(&Sender 42 10)                      
chgvar    &ModName   %sst(&Sender 54 10)