ILOG CPLEX 10.1 User's Manual > Programming Considerations > Licensing an Application > Examples

Here are some code samples that illustrate the use of those runtime license routines and methods. The first example illustrates the routine CPXputenv when opening the CPLEX environment.

Notes
This example assumes a Microsoft Windows file directory structure that requires an additional backslash when specifying the path of the file containing the key. It also assumes that the application uses an environment variable called MYAPP_HOME to identify the directory in which it was installed.

The string argument to CPXputenv must remain active throughout the time ILOG CPLEX is active; the best way to do this is to malloc the string.

CPXputenv Routine for C and C++ Users

char *inststr = NULL;
char *envstr  = NULL;
 
/* Initialize the CPLEX environment */
 
envstr = (char *) malloc (256);
if ( envstr == NULL ) {
   fprintf (stderr, "Memory allocation for CPXputenv failed.\n");
   status = FAIL;
   goto TERMINATE;
}
else {
   inststr = (char *)  getenv("MYAPP_HOME");
   if ( inststr == NULL ) {
      fprintf (stderr, "Unable to find installation directory.\n");
      status = FAIL;
      goto TERMINATE;
   }
   strcpy (envstr, "ILOG_LICENSE_FILE=");
   strcat (envstr, inststr);
   strcat (envstr, "\\license\\access.ilm");
   CPXputenv (envstr);
}
 
env = CPXopenCPLEX (&status);
 

The putenv Method for Java Users

Here is an example using Concert Technology for Java users:

IloCplex.putenv("ILOG_LICENSE_FILE=\\license\\access.ilm");
try {
   cplex = new IloCplex();
}
catch (IloException e) {
   System.err.println("Exception caught for runtime license:" + e);
}
 

The Putenv Method for .NET Users

Here is an example using Concert Technology for .NET users:

 Cplex.Putenv("ILOG_LICENSE_FILE=../../../certify/access.e.ilm");
      try {
         cplex = new Cplex();
      }
      catch (ILOG.Concert.Exception e) {
          System.Console.WriteLine("Concert exception caught: " + e);
      }

CPXRegisterLicense Routine for C and C++ Users

The following is an example showing how to use the routine CPXRegisterLicense.

static char *ilm_license=\
 "LICENSE ILOG Incline\n\
  RUNTIME CPLEX 9.200 21-Jul-2005 R81GM34ECZTS N , options: m ";
static int ilm_license_signature=2756133;
 
   CPXENVptr     env = NULL;
   int           status;
 
   /* Initialize the CPLEX environment */
 
    status = CPXRegisterLicense (ilm_license, ilm_license_signature);
    if ( status != 0) {
       fprintf (stderr, "Could not register CPLEX license, status %d.\n",
                status);
       goto TERMINATE;
    }
    env = CPXopenCPLEX (&status);
    if ( env == NULL ) {
       char  errmsg[1024];
       fprintf (stderr, "Could not open CPLEX environment.\n");
       CPXgeterrorstring (env, status, errmsg);
       fprintf (stderr, "%s", errmsg);
       goto TERMINATE;
    }
 
 

The registerLicense Method for Java Users

Here is an example for Java users applying IloCplex.registerLicense:

static String ilm_CPLEX_license=
"LICENSE ILOG Test\n RUNTIME CPLEX 9.200 021-Jul-2005 R81GM34ECZTS N  ,
options: m ";
static int ilm_CPLEX_license_signature=2756133;
 
public static void main(String[] args) {
 
   try {
      IloCplex.registerLicense(ilm_CPLEX_license, ilm_CPLEX_license_signature);
      IloCplex cplex = new IloCplex();
   }
   catch (IloException e) {
      System.err.println("Exception caught for runtime license:" + e);
   }
}
 

The RegisterLicense Method for .NET Users

Here is an example for .NET users applying Cplex.RegisterLicense:

  internal static string ilm_CPLEX_license="LICENSE ILOG User\n RUNTIME CPLEX
     9.200 05-Aug-2005 62RAR21A8NC5 N any , options: m ";
  internal static int ilm_CPLEX_license_signature=863909;
 public static void Main(string[] args) {
    try {
      Cplex.RegisterLicense(ilm_CPLEX_license, ilm_CPLEX_license_signature);
      Cplex cplex = new Cplex();
    }
    catch (ILOG.Concert.Exception e) {
      System.Console.WriteLine("Expected Concert exception caught: " + e);
    }
}