ILE C/C++ Programmer's Guide

Binding the Service Program to a Program

In the following example, a very short application consisting of a program MYPROGA is bound to the service program. The source code for MYPROGA, MYPROGA.cpp, is shown in the following figure.

Note:
This sample application has been reduced to minimal functionality. It's main purpose is to demonstrate how to create a service program.

Figure 8. Source Code for myproga.cpp


// myproga.cpp
// Finds a character string in another character string.
 
   #include <stdio.h>
   #include <iostream.h>
   #include <stdlib.h>
   #include "search.h"
   #define HS "Find the needle in this haystack"
 
   void main () {
      int i;
      Search token("needle");
      i = token.where (HS, sizeof(HS));
      cout << "The string was found in position " << i << endl;
   }

The program creates an object of class Search. It invokes the constructor with a value that represents the string of characters ("needle") to be searched for. It calls the member function where() with the string to be searched ("Find the needle in this haystack"). The string "needle" is located, and its position in the target string "Find a needle in this haystack" is returned and printed.

To create the program MYPROGA in library MYLIB, and bind it to the service program SERVICE1, enter the following:

CRTPGM PGM(MYLIB/MYPROGA) SRCSTMF(myprogA.cpp) BNDSRVPGM(MYLIB/SERVICE1)

Figure 9 shows the internal and external function calls between program MYPROGA and service program SERVICE1.

Figure 9. Calls between Program and Service Program



srvpgm

When MYPROGA is created, it includes information regarding the interface it uses to interact with the service program.

To run the program, enter:

CALL MYLIB/MYPROGA
 

During the process of making MYPROGA ready to run, the system verifies that:

If either of the above is not true, an error message is issued.

The output of MYPROGA is:

The string was found in position 9


[ Top of Page | Previous Page | Next Page | Table of Contents ]