Setting Up Your Application

  1. A set of template project files for applications have been provided for you. Copy the Form template files into the directory in which you will be building your application.
    • Files that are compatible with the Visual C++ development environment can be found in: <API Program folder>\Samples\Msc32\Form\Template.
    • If you prefer to set up the application development environment yourself, follow the procedure outlined in Setting up the C environment to use the Form library .
  2. Create a new .c source file named calculateAge.c and include any necessary header files. For every application that uses the Form Library, you must include pe_api.h. In calculateAge.c:
       #include "pe_api.h"
       #include <stdio.h>
       #include <string.h>
       #include <stdlib.h>
    
    Note: You must always include pe_api.h in any source file that uses the API.
  3. Declare all the functions implemented in calculateAge.c.
    • The initialize function initializes the API.
         r_short initialize(
         #ifdef PROTO
            void
         #endif
         );
      
    • The loadForm function calls the API function UFLReadForm that will read a specified form into memory.
         r_short loadForm(
         #ifdef PROTO
            formNodeP *form
         #endif
         );
      
    • The function getBirthDate uses the API function UFLGetLiteralByRefEx to retrieve date information from the form loaded in memory.
         r_short getBirthDate(
         #ifdef PROTO
            formNodeP form,
            int *birYear,
            int *birMonth,
            int *birDay
         #endif
         );
      
    • The saveForm function saves the form to disk by calling the API function UFLWriteForm.
         r_short saveForm(
         #ifdef PROTO
            formNodeP form
         #endif
         );
      
    • The reportError function reports errors by printing the error codes associated with a particular error.
         void reportError(
         #ifdef PROTO
            char *theMessage,
            r_short theCode
         #endif
         );
      
  4. Create your main function and declare any variables needed for your application. The main function in calculateAge.c will declare variables for the birth day, birth month, and birth year. It will also be responsible for calling the functions declared previously. Finally, main is responsible for freeing any memory used by the application by making a call to the function UFLDestroy.
       #ifdef WINDOWS
       #ifndef OLD_STYLE_PARAMS
          int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR
          lpszCmdParam, int nCmdShow)
       #else
          int PASCAL WinMain(hInstance, hPrevInstance,lpszCmdParam, nCmdShow)
          HANDLE hInstance;
          HANDLE hPrevInstance;
          LPSTR lpszCmdParam;
          int nCmdShow;
       #endif
    
       #else
       #ifndef OLD_STYLE_PARAMS
          int main(int argc, char **argv)
       #else
          int main(argc, argv)
          int argc;
          char **argv;
       #endif
       #endif
       {
       formNodeP theForm;
       int birthYear;
       int birthMonth;
       int birthDay;
       #ifndef WINDOWS
          void *hInstance = NULL;
       #else
       #ifndef MSC
       #ifndef __FLAT__
          _InitEasyWin();
       #endif
       #endif
       #endif
    
    /* First, we call a function that will initialize the special memory and 
          error handling. */
    
          if (initialize((void *)hInstance) != OK)
          {
             reportError("Error in initialize function.\n", 0);
             exit (1);
          }
    
    /* Next, we call a function that will load the form into memory. */
    
          if (loadForm(&theForm) != OK)
          {
             reportError("Error in loadForm function.\n", 0);
             exit (1);
          }
    
    /* This function retrieves the birth year, month and day values (as
          integers) from the original form. */
    
          if (getBirthDate(theForm, &birthYear, &birthMonth, &birthDay) != OK)
          {
             reportError("Error in the getBirthDate function.\n", 0);
             exit (1);
          }
    
    /* This function writes the birth year, month, and day into hidden fields
          in the original form whose values are the arguments for the compute for
          PAGE1.SHOWAGE.value */
    
          if (setBirthDate(theForm, birthYear, birthMonth, birthDay) != OK)
          {
             reportError("Error in the setBirthDate function.\n", 0);
             exit (1);
          }
    
    /* This function saves the processed form in the current directory as
          'output.xfd'. */
    
          if (saveForm(theForm) != OK)
          {
             reportError("Error in the saveForm function.\n", 0);
             exit (1);
          }
    
    /* Now that we are done with the form, we can free the memory by calling
          UFLDestroy. The parameter, 'theForm', is a pointer to the root node of
          the form. This causes the root node and all of its children (the
          complete form) to be deleted from memory. */
    
          UFLDestroy(theForm);
          return(OK);
       }
    
As with any Java™ application, you must begin by importing the necessary classes and defining the program's classes.
  1. Create a new Java source file called calculateAge.java.
  2. Any program that calls methods from the API must import the following classes:
       import com.ibm.form.api.DTK;
       import com.ibm.form.api.xfdl.FormNodeP;
       import com.ibm.form.api.xfdl.XFDL;
       import com.ibm.form.api.error.UWIException;
       import com.ibm.form.api.IFSSingleton;
    
    • You must place these lines before any class or interface definitions.
  3. Set up the rest of your application. This generally includes defining any classes and methods for your application as well as declaring and initializing any variables you may need. The following code sets up the Calculate Age application:
    • Create the public class CalculateAge and the main method for the class.
         public class CalculateAge
            {
      
    • Declare a FormNodeP object called theForm to represent the form.
            private static FormNodeP theForm;
      
    • Create the program's main method.
            public static void main(String argv[])
               {
      
    • Declare the program's variables.
            int birthYear;
            int birthMonth;
            int birthDay;
      
    • The program's main method essentially consists of a series of calls to other methods. The Form Library methods are called from the definition of these methods.
               try
               {
                  initialize();
      
                  loadForm();
      
                  birthYear = getBirthYear();
                  birthMonth = getBirthMonth();
                  birthDay = getBirthDay();
      
                  setBirthYear(birthYear);
                  setBirthMonth(birthMonth);
                  setBirthDay(birthDay);
      
                  saveForm();
      
    • Free the memory in which the form was stored. For more information see Closing a Form .
                  theForm.destroy();
               }
      
    • Finally, perform exception handling.
               catch (Exception ex)
               {
                  ex.printStackTrace();
               }
      
               /* Additional code removed */
      
            }
         }
      
This example assumes that you are developing a program in Microsoft Visual Basic using Microsoft Studio. To begin, you must first set up your development environment to use the API type library.
  1. Create a new Visual Basic project.
  2. Any project using the API must include the following type library:
    • IFS_COM_API.tlb

      To add this to your Visual Basic Project, open the References ; dialog from the Project menu and select “InternetForms API”.

  3. Set up the rest of your application. This generally includes setting up your main algorithm and declaring any variables you will need. The following code sets up the Calculate Age application:
       ' Create the Main method for the program.
    
       Sub Main()
          
          ' Declare a number of variables. TheForm represents the form, while the
          ' other variables are values we will read from the form.
    
          Dim TheForm As IFormNodeP
          Dim BirthYear As Integer
          Dim BirthMonth As Integer
          Dim BirthDay As Integer
    
          ' The program's Main consists of a number of calls to other functions.
    
          Initialize
    
          Set TheForm = LoadForm
    
          BirthYear = GetBirthYear(TheForm)
          BirthMonth = GetBirthMonth(TheForm)
          BirthDay = GetBirthDay(TheForm)
    
          SetBirthYear BirthYear, TheForm
          SetBirthMonth BirthMonth, TheForm
          SetBirthDay BirthDay, TheForm
    
          SaveForm TheForm
    
          ' Free the memory in which the form was stored.
    
          TheForm.Destroy
    
       End Sub