Listing 2: Managed C++ code used to create the assembly naglib, which contains the namespace NAGLIB and wraps the NAG C library functions in the class NAG_FUNCTIONS.

extern "C" {
   #include <stdio.h>
   #include <nag.h>
   #include <nag_stdlib.h>
}
#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;
using namespace System;
namespace NAGLIB {
   public __delegate double INTEGRAND_FUN_TYPE(Double x);
   public __delegate void OBJ_FUN_TYPE (Int32 n, double *x, 
                                    double *objf, double *g, Int32 comm);
   [DllImport("nagc")]
   extern "C" void e04dgc(Int32 n, OBJ_FUN_TYPE *f, Double *x, Double *objf,
               Double *g, Nag_E04_Opt *options, Int32 comm, NagError *flag);
   [DllImport("nagc")]
   extern "C" void e04xxc(Nag_E04_Opt *options);
   [DllImport("nagc")]
   extern "C" void d01ajc(INTEGRAND_FUN_TYPE *f, Double a, Double b,
        Double epsabs, Double epsrel, Int32 max_num_subint, Double* result,
        Double *abserr, Nag_QuadProgress *qp, NagError *flag);
   [DllImport("nagc")]
   extern "C" void f02aac(Int32 n, Double *a, Int32 tda, 
                         Double *r, NagError *eflag);
   [DllImport("nagc")]
   extern "C" Double s15abc(Double x);
   public __gc class NAG_FUNCTIONS
   {
      public:
      Double QUADRATURE_epsabs;
      Double QUADRATURE_epsrel;
      Int32 QUADRATURE_max_subint;
      NAG_FUNCTIONS()
      { // the constructor: set default values
      QUADRATURE_epsabs = 0.0;
      QUADRATURE_epsrel = 0.0001;
      QUADRATURE_max_subint = 200;
      }
      void REAL_SYMM_EIGEN (Int32 n, Double *a, Int32 tda, 
                                                 Double *r, Int32 *flag)
      {
         NagError eflag;
         INIT_FAIL(eflag);
         f02aac(n, a, tda, r, &eflag);
         *flag = (Int32)eflag.code;
     }
     Double CUM_NORM (Double x) {
        return s15abc(x);
     }
     void OPTIMIZE (Int32 n, Double *x, Double *g, Double *objf, 
                                       Int32 *flag, OBJ_FUN_TYPE *the_fun)
     {
        NagError eflag;
        Nag_E04_Opt options;
        INIT_FAIL(eflag);
        e04xxc(&options);
        options.print_level = Nag_NoPrint;
        options.list = 0;
        options.verify_grad = Nag_NoCheck;
        e04dgc(n, the_fun, x, objf, g, &options, (Int32)0, &eflag);
        *flag = (Int32)eflag.code;
     }
     void QUADRATURE (Double a, Double b, Double *result, 
                     Double *abserr, Int32 *flag, INTEGRAND_FUN_TYPE *the_fun)
     {
       Nag_QuadProgress qp;
       NagError eflag;
       INIT_FAIL(eflag);
       d01ajc(the_fun, a, b, QUADRATURE_epsabs, QUADRATURE_epsrel, 
                         QUADRATURE_max_subint, result, abserr, &qp, &eflag);
       *flag = (Int32)eflag.code;
     }
  };
}