Bryan's RPG IV Journal
Executing Commands with system() Function
It's common to use the QCMDEXC or QCAPCMD API when you want to execute a CL command from an RPG program. But you may find it more convenient to use a C runtime library function, system(), to accomplish the same purpose. The system() function will pass a command string to the command processor, without the need to pass the length of the command string, or any other parameters for that matter.
To call the system() function, you simply pass it a pointer to the command string. Here's the suggested prototype (along with some necessary H-specs):
If the system() function fails (i.e., return code is 1), it sets a global variable _EXCP_MSGID with the CPF message ID. You can import this variable into your program to check for specific errors, as the
following example shows:
To call the system() function, you simply pass it a pointer to the command string. Here's the suggested prototype (along with some necessary H-specs):
/If Defined(*Crtbndrpg)When it comes time in your program to execute a command, you can refer to the prototype. The command string may be a a variable, literal, named constant, or an expression. The following example shows a typical use:
H Dftactgrp(*No)
/Endif
H Bnddir('QC2LE')
// ------------------------------------------------------- Prototypes
D GoCmd PR 10I 0 Extproc('system')
D CmdString * Value
D Options(*String)
/If Defined(*Crtbndrpg)The return code will let you check for the success or failure of the system() function. The return code is zero if the command is successful, or 1 if the command fails. If you pass a null pointer to a string, system() returns -1, and the command processor is not called.
H Dftactgrp(*No)
/Endif
H Bnddir('QC2LE')
// ------------------------------------------------------- Prototypes
D GoCmd PR 10I 0 Extproc('system')
D CmdString * Value
D Options(*String)
D NullString C -1
D Success C 0
D Returncode S 10I 0
D User S 10 Inz(*User) Varying
/Free
Returncode = Gocmd('WRKSPLF SELECT(' + User + ') OUTPUT(*PRINT)');
Select;
When Returncode = Success; // Command was successful
...
When Returncode = NullString; // Command string was null
...
Other; // Command failed
...
Endsl;
/End-free
If the system() function fails (i.e., return code is 1), it sets a global variable _EXCP_MSGID with the CPF message ID. You can import this variable into your program to check for specific errors, as the
following example shows:
/If Defined(*Crtbndrpg)To use the system() function, you must refer to binding directory QC2LE when compiling and/or binding the program. The above examples name QC2LE in the H-specs.
H Dftactgrp(*No)
/Endif
H Bnddir('QC2LE')
// ------------------------------------------------------- Prototypes
D GoCmd PR 10I 0 Extproc('system')
D CmdString * Value
D Options(*String)
D NullString C -1
D Success C 0
D ObjectNotFound C 'CPF3142'
D ObjectInUse C 'CPF3156'
D Errmsgid S 7 Import('_EXCP_MSGID')
D Returncode S 10I 0
/Free
Returncode = Gocmd('DLTF MYLIB/MYFILE');
Select;
When Returncode = Success; // Command was successful
...
When Returncode = NullString; // Command string was null
...
When Errmsgid = ObjectNotFound; // CPF3142
...
When ErrMsgid = ObjectInUse; // CPF3156
...
Other; // Some other error
...
Endsl;
/End-free
Log of Journal Entries |
|
Use this scrollable blog map to view other journal entries: |
|
| It is your responsibility to ensure procedures, techniques, and code used from this website are accurate and appropriate for your installation. No warranty or support is implied or expressed. |
![]() |
| Privacy and Email Policy |
| AS/400®, eServer, i5/OS, IBM i, Integrated Language Environment®, iSeries, OS/400®, RPG/400®, System i5, VisualAge®, and WebSphere® are trademarks of IBM Corporation. Note: IBM® System i is the latest member of the family of eServer iSeries. This site might refer to System i as System i5, iSeries, or AS/400. |
|
If you are having trouble navigating the menu at the top of this page, you may use the following scrollable site map instead: |
|
|











International visitors: