/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
#include<dlfcn.h>
#include "class1_ch.h"

Class1::Class1() { 
  void *fptr;

  /* Here to load the dynamically loaded library if necessary.
     g_sample_dlhandle is a global pointer pointing to the loaded DLL
     g_sample_dlcount  is a global int counting instances for all classes
     They are declared in the header file class1_ch.h for Ch space.
  */
  if(g_sample_dlhandle == NULL || g_sample_dlcount == 0) {
    g_sample_dlhandle = dlopen("libproject.dl", RTLD_LAZY);
    if(g_sample_dlhandle == NULL) {
       printf("Error: %s(): dlopen(): %s\n", __class_func__, dlerror());
       return;
    }
  }

  /* to get the address by function name */
  fptr = dlsym(g_sample_dlhandle, "Class1_Class1_chdl");
  if(fptr == NULL) {
    printf("Error: %s(): dlsym(): %s\n", __class_func__, dlerror());
    return;
  }

  /* to call the chdl function in dynamically loaded 
     library by address */
  dlrunfun(fptr, NULL, NULL);
  g_sample_dlcount++; // to increase count of instance
}
