Calling IBM i system APIs in C# with NTi

03/29/2024

Rémi Rouillot

IBM i News

I'm going to briefly show you how, in just a few lines, you can easily call up an IBM i API using NTi.

Introduction

The IBM i APIs offer many interesting features, both for administration and for specific tasks such as spool file management.

I'm going to briefly show you how, in just a few lines, you can easily call an IBM i API using NTi.

Choosing an API: QSYS.QCMDCHK

For this example, we'll use the QCMDCHK API from the QSYS library. This API is used to check the syntax and validity of a CL command before executing it.

The first thing to do is check its documentation here

We discover that QCMDCHK expects two input parameters:

  • 1 - Command string - I/O - Char(*)
  • 2 - Length of command string - Input - Packed(15, 5)

These parameters correspond respectively to the command to be checked and its length in number of characters.

Using NTi

In this example, we'll call this API to test the validity of the following command:

CRTLIB CLIENTS

Step 1: Setting up the NTi connection

First, we create an NTiConnection object from which to call the API:

var conn = new NTiConnection(connectionString);
conn.Open();

Step 2: Parameter declaration

Next, we need to prepare the various parameters that will be passed to our API by creating a list of NTiParameter objects, where each element in the list corresponds to a parameter to be passed to the API:

var cmd = "CRTLIB CLIENTS";          
var parms = new List<NTiProgramParameter>()
{
    new NTiProgramParameter(cmd, cmd.Length),
    new NTiProgramParameter(cmd.Length, 15, 5)
};

NTi supports all parameter types: integers, packed decimal, strings...

Step 3: Call the API

Finally, we just need to call the API:

conn.CallProgram("QSYS", "QCMDCHK", parms);

To summarize

This code is used to call QCMDCHK:

var conn = new NTiConnection(connectionString);
conn.Open();
var cmd = "CRTLIB CLIENTS";         
var parms = new List<NTiProgramParameter>()
{
    new NTiProgramParameter(cmd, cmd.Length),
    new NTiProgramParameter(cmd.Length, 15, 5)
};
conn.CallProgram("QSYS", "QCMDCHK", parms);

When these few lines are executed, there are two possibilities:

The command is valid: In this case, no error is raised, return code 0.

The command is incorrect: In this case, an error is raised on the C# side with a code CPFXXXX accompanied by an explanation. This error is then handled according to the use case.

Conclusion

With NTi, calling a system API takes just a few lines. All kinds of processing can be carried out in C#, without ever having to go through a green screen. By coupling this with CL commands and SQL services, your IBM i administration tasks can be automated and carried out in modern, intuitive user interfaces.

The integration of IBM i and your procedures is completely transparent. Don't waste any more time and use programming languages that are familiar to everyone, to secure your IBM i investments and gain in efficiency!

This is a relatively simple case, as we only had to supply two parameters, but everything works in the same way, no matter how complex the API used.

To find out more and try out NTi, don't hesitate to contact me.

Rémi Rouillot

Back