IBM Support

ILE RPG Example of QCAPCMD API

Troubleshooting


Problem

This document contains an example written in ILE RPG of calling the QCAPCMD API. This API can be used as an alternative to QCMDEXC.

Resolving The Problem

The advantage of using the QCAPCMD versus QCMDEXC is that you can pass the error code parameter. The error code parameter allows you to check for any errors that can occur on the command.

RPGLE source named CPACMD3:
**FREE
Dcl-F newfile    Keyed usropn;
Dcl-S command         Char(40)        inz(' ');
Dcl-S cmdlng          Int(10)         inz(40);
Dcl-Ds ocb;
  type            Int(10)         inz(0);
  DBCSdh          Char(1)         inz('0');
  prompt          Char(1)         inz('0');
  cmdsyntax       Char(1)         inz('0');
  msgrtvkey       Char(4)         inz(x'00000000');
  ccsidcmdst      Int(10)         inz(0);
  reserve1        Char(5)         inz(x'0000000000');
End-Ds;
Dcl-S ocblength       Int(10)         inz(x'00000020');
Dcl-S ocbfmt          Char(8)         inz('CPOP0100');
Dcl-S chgcmd          Char(1)         inz(' ');
Dcl-S lngchgcmd       Int(10)         inz(0);
Dcl-S lngchgrtn       Int(10)         inz(0);
Dcl-Ds ErrorCode;
  BytesProvided   Int(10)         inz(%size(ErrorCode));
  BytesAvail      Int(10);
  ExceptionID     Char(7);
  RESERVED        Char(1);
  ExceptionData   Char(256);
End-Ds;
// Prototype for QCAPCMD
Dcl-Pr Pgm_QCAPCMD ExtPgm('QCAPCMD');
  command         Char(40);
  cmdlng          Int(10);
  ocb             LikeDs(ocb);
  ocblength       Int(10);
  ocbfmt          Char(8);
  chgcmd          Char(1);
  lngchgcmd       Int(10);
  lngchgrtn       Int(10);
  ErrorCode       LikeDs(ErrorCode);
End-Pr;
command = 'OVRDBF NEWFILE SHARE(*YES)';
Pgm_QCAPCMD(command : cmdlng : ocb : ocblength : ocbfmt :
      chgcmd : lngchgcmd : lngchgrtn : ErrorCode);
If BytesAvail > 0;
  Dsply 'Error on API';
  Dsply ExceptionID;
EndIf;
Open newfile;
Setll *loval fmt1;
Read fmt1;
*IN30 = %Eof;
If *in30 = '0';
  Dsply fld1;
Else;
  Dsply 'No Records';
EndIf;
Close newfile;
*INLR = '1'; 
DDS for newfile:
                R FMT1
                  FLD1          10
                  FLD2          10
                K FLD1 

Compile the program using  CRTBNDRPG PGM(<yourlib>/CPACMD3) SRCFILE(<yourlib>/QRPGLESRC) SRCMBR(CPACMD3)
Add a record to database file NEWFILE.
Call the program from command line:

> call cpacmd2     
  DSPLY  rec1fld1  



To test the program functionality, place the program in debug, and place a breakpoint on stmt "If BytesAvail > 0;"
Use F21=Command entry and issue  DSPJOB OPTION(*FILOVR) to examine the file override details.

image-20250408113525-1
 
To test the API QCPACMD error handling, the following syntax was used deliberately on the OVRDBF cmd to force an error:
command = 'OVRDBF NEWFILE SHARE(*XES)';  
> call cpacmd3                               
  '*XES      ' not valid for parameter SHARE.
  DSPLY  Error on API                        
  DSPLY  CPF0001                             
  DSPLY  rec1fld1                            


 

[{"Type":"MASTER","Line of Business":{"code":"LOB68","label":"Power HW"},"Business Unit":{"code":"BU070","label":"IBM Infrastructure"},"Product":{"code":"SWG60","label":"IBM i"},"ARM Category":[{"code":"a8m0z0000000CHtAAM","label":"Programming ILE Languages"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions"}]

Historical Number

405981682

Document Information

Modified date:
08 April 2025

UID

nas8N1015122