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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/If Defined(*Crtbndrpg)
H Dftactgrp(*No)
/Endif
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)

/Free

*inlr = *on;

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
// ...
when errMsgid = LibraryNotFound; // CPF2110
// ...
other; // Some other error
// ...
endsl;

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

return;

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

WMsgFile = 'QCPFMSG';
WMsgLibrary = 'QSYS';
WMsgFileLib = WMsgFile + WMsgLibrary;
WReplaceData = userid;

$GetMessage(RTVM0100
: %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;
endif;
// retrieve the message with message data inserted
WMessage = %subst(RTVM0100.WMsgData : 1 : RTVM0100.WMsgLen);
else;
WMessage = *Blanks;
endif;

endsr;

/end-free
system – Command Processing API

API – SYSTEM

SYSTEM – API CMD Processor

Leave a Reply

Your email address will not be published. Required fields are marked *