Invoke MiSys Equation API From .NET


This article explains how to invoke MiSys Equation API (or any AS/400 program in general) through IBM i Access
for Windows component. The code is about calling an RPG compiled program (or
any OS/400 command) on IBM AS/400 iSeries server from .NET application


MiSys Equation is the core banking system at one of my client, hosted on an iSeries server. we
need to expose Equation API/commands as .NET Services for any application to consume them regardless of the platform.
BizTalk and WCF is the platform for our middleware SOA implementation. IBM iSeries Access to Windows is a library from IBM to connect to iSeries application over TCP protocol.
This walk through describes how to expose MiSys Equation functionality as .NET library. we need to build a WCF or BizTalk service on top of this library method to expose it as web service.

Code Walk through

1. Install IBM iSeries Access to Windows. Also check for service packs
at this URL

2. Create a new Console Application project in Visual Studio

3. Add Project Reference to cwbx.dll ActiveX from C:\Program Files
(x86)\IBM\Client Access\Shared

4. Add the attached API.cs file to your project

5. Now, we are going to connect to iSeries. we need to provide iSeries Server IP/Name, User Name and Password to connect.

//code snippet from attached API.cs

AS400System as400 = new AS400SystemClass()

Program program = new Program(); // iSeries Program (PGM)  

as400.Define(""); //Server Name/IP
program.system  = as400;
program.system.UserID =User;
program.system.Password = Password;

//Connect to execute Remote commands only

6. Once we are connected to iSeries, we need to initialize
liability(context) with Equation application. This context specifies to which Equation application instance we are going to connect, as there could be multiple application instances running on the server.
Parameters.Append() is used to defined the parameters and their length. All the parameters will be passed as a Byte array and the response will be a Byte array as well.

program.LibraryName = "KAPBASELIB";
program.ProgramName = "KAPUNLIBL"; 

StringConverter stringConverter = new StringConverterClass();
ProgramParameters parameters = new ProgramParametersClass();

parameters.Append("Env", cwbrcParameterTypeEnum.cwbrcInput, 3);
parameters.Append("A1", cwbrcParameterTypeEnum.cwbrcInput,4); 

parameters["Env"].Value = stringConverter.ToBytes("DEV");//Equation Instance
parameters["A1"].Value = stringConverter.ToBytes("*DFT");//default value 


7. Next step is to define and assign values to In and Out parameters and call Equation
API. you need to consult MiSys Equation Journal File Documentation to check for parameters length and whether they are Input or Output parameters

program.ProgramName = "H46HMR";//Inquiry API Wrapper
ProgramParameters parameters = new ProgramParametersClass();

SetInParameter(ref parameters, "EPGM",10, "H68EER");//Bal Inq Function identifier
SetInOutParameter(ref parameters, "DSAIF",3000, "00001xxxxx"); //Account No. 


8. Final step is to parse the response received from Equation. As you can see in the above code, DSAIF is input/output parameter and will have the response from Equation in the form of Byte array. we need to parse the byte array through StringConverter helper method

_response.Bytes = parameters[2].Value;

StringConverter stringConverter = new StringConverterClass();
stringConverter.Length = 0; // Use all bytes in source byte array to build string
string str = stringConverter.FromBytes(_response.Fields["HZCUN"].Value);

Points of Interest

couple of points worth noting

1. Input and Output parameters are Byte array. i.e. you need to take
care of order of parameters when using parameters.append()

2. use StringConverter, PackedConverter helper methods to convert
from Byte array to string

see the attached zip file for a sample implementation for Account
Balance Inquiry API.

Thanks to Joe Rattz for this article, which was starting point for me.


IBM iSeries Access for Windows Error


I created a sample application to call AS/400 API from .NET. when initializing the class as

cwbx.AS400System as400 = new cwbx.AS400SystemClass();
cwbx.Program program = new cwbx.Program();

got the following error

{"Retrieving the COM class factory for component with CLSID {86F9DB12-15D5-11D2-849C-0004AC94E175} failed due to the following error: 80040154."}

I looks like that this version of library ( is not compatible with 64-bit Windows Server 2008 R2.

The workaround is to change your application configuration to 32-bit from visual studio project properties


Team Explorer: TF265000

Error: TF265000: Your client does not support the selected query type. To run this query, either upgrade your client to the version compatible with Team Foundation Server or run the query from Team System Web Access.

Reason: Team Explorer in VS 2008 does not support queries that have ‘Tree of Work Items’. This a new feature in TFS 2010 and we have to use Team Explorer 2010 for this type of queries

SSO Error Code: 0x80131700


Could not create SSOSQL. To fix the problem, reinstall SSO or try ‘regasm SSOSQL.dll’ from a Visual Studio command prompt.  Error Code: 0x80131700

To fix this, we need to register SSOSQL.dll again by running

regasm c:\Program Files\Common Files\Enterprise Single Sign-On\SSOSQL.dll

we need to run this command from 64-bit command prompt of VS 2008

Environment: Windows 2008 R2, BizTalk 2009