system() is a runtime C library function that performs the same purpose as the QCMDEXC API. With system() you pass a pointer to the command string and it is executed, no need for the for the command string length.  The API is a good choice as it allows for capturing of error messages.  These messages can then be used to take actions within the calling program.

[cc lang=”php”]
/If Defined(*Crtbndrpg)
H Dftactgrp(*No)
H Bnddir(‘QC2LE’)
// ——————————————————- Prototypes

d GoCmd pr 10I 0 Extproc(‘system’)
d CmdString * Value
d Options(*String)

d $GetMessage pr extpgm(‘QMHRTVM’)
d WRcvVar 65535A Options(*VarSize)
d WRcvVarLen 10I 0 Const
d WFormat 8A Const
d WMsgID 7A Const
d WQualMsgF 20A Const
d WReplData 65535A Const Options(*VarSize)
d WReplDataLen 10I 0 Const
d WDoReplace 10A Const
d WUseCtrlChars 10A Const
d WErrorCode 65535A Options(*VarSize)
d WRetrOpt 10A Const Options(*NoPass)
d WRetrOpt 10A Const Options(*NoPass)
d WCCSID_out 10I 0 Const Options(*NoPass)
d WCCSID_in 10I 0 Const Options(*NoPass)

// program variables
d Errmsgid s 7 Import(‘_EXCP_MSGID’)
d errorcode s 7
d LibraryNotFound…
d c ‘CPF2110’
d NullString c -1
d ObjectNotFound c ‘CPF3142’
d ObjectInUse c ‘CPF3156’
d Returncode s 10I 0
d Success c 0
d Userid s 10a
d WReplaceData s 100A Inz(*Blanks)
d WMessage s 100a Inz(*blanks)
d WMsgFile s 10A
d WMsgLibrary s 10A
d WMsgFileLib s 20A
d WMaxMsgLen s 10i 0 inz(%size(WMessage))

d ErrorDS ds qualified
d bytesProv 10i 0 inz(0)
d bytesAvail 10i 0 inz(0)

d RTVM0100 ds qualified
d WMsgLen 10i 0 overlay(RTVM0100:9)
d WMsgData 32767a overlay(RTVM0100:25)


*inlr = *on;

returncode = Gocmd(‘DLTF MYLIB/MYFILE’);

when returncode = Success; // Command was successful
// …
when returncode = NullString; // Command string was null
// …
when errmsgid = ObjectNotFound; // CPF3142
// …
when errMsgid = ObjectInUse; // CPF3156
// …
when errMsgid = LibraryNotFound; // CPF2110
// …
other; // Some other error
// …

// added for fun — return CPF message text
if errMsgid <> *blanks;
exsr $getTheMessageDescription;


// $GetTheMessageDescription – return message description
begsr $GetTheMessageDescription;

WMsgFile = ‘QCPFMSG’;
WMsgLibrary = ‘QSYS’;
WMsgFileLib = WMsgFile + WMsgLibrary;
WReplaceData = userid;

: %size( RTVM0100 ) // return data
: ‘RTVM0100’ // return data
: errMsgid // msg id
: WMsgFileLib // msg file,
: WReplaceData // msg parameter
: %size(WReplaceData) // msg parameter
: ‘*YES’ // substitute MSG
: ‘*NO’ // control chars
: ErrorDS

if (RTVM0100.WMsgLen > *Zeros);
if (RTVM0100.WMsgLen > WMaxMsgLen);
RTVM0100.WMsgLen = WMaxMsgLen;
// retrieve the message with message data inserted
WMessage = %subst(RTVM0100.WMsgData : 1 : RTVM0100.WMsgLen);
WMessage = *Blanks;



system – Command Processing API


SYSTEM – API CMD Processor

Leave a Reply

Your email address will not be published.