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

EXPORTCH int chglobalsymboladdrbyname_chdl(void *varg) {
  ChInterp_t  interp;
  ChVaList_t ap;
  int *ch_i;
  int **ch_p;
  float *ch_f;
  int *ch_a, **ch_aptr;
  char **str;

  Ch_VaStart(interp, ap, varg);
  /* ch_i shall be an extern variable in the calling Ch program */
  ch_i = (int *)Ch_GlobalSymbolAddrByName(interp, "ch_i"); 
  if(ch_i!=NULL) {
     printf("ch_i value in dynamically loaded object passed from Ch program is: %d\n", 
        *ch_i);
    *ch_i = 1000; /* global variable ch_i is assigned with another value */
  }
  else {
     printf("ch_i is not defined in Ch program\n");
  }

  ch_p = (int **)Ch_GlobalSymbolAddrByName(interp, "ch_p"); 
  if(ch_p!=NULL) {
     if(*ch_p != NULL)  {
        printf("ch_p value in dynamically loaded object passed from Ch program is: %p\n", 
        *ch_p);
        printf("*ch_p value in dynamically loaded object passed from Ch program is: %d\n", 
        **ch_p);
     }
     else {
        printf("ch_p in Ch space is NULL\n");
     }
  }
  else {
     printf("ch_p is not defined in Ch program\n");
  }

  /* ch_f shall be an extern variable in the calling Ch program */
  ch_f = (float *)Ch_GlobalSymbolAddrByName(interp, "ch_f"); 
  printf("ch_f value in dynamically loaded object passed from Ch program is: %f\n", 
        *ch_f);

  *ch_f = 2000; /* global variable ch_f is assigned with another value */

  /* ch_aptr is a pointer to pointer to int */
  ch_aptr = (int **)Ch_GlobalSymbolAddrByName(interp, "ch_a"); 
  ch_a = *ch_aptr;
  /* or ch_a = *(int **)Ch_GlobalSymbolAddrByName(interp, "ch_a");  */
  printf("ch_a[0] = %d ch_a[1] = %d, ch_a[2] = %d\n", 
         ch_a[0], ch_a[1], ch_a[2]);

  /* ch_a is a pointer to int */
  ch_a = (int *)Ch_GlobalSymbolAddrByName(interp, "a"); 
  /* or ch_a = *(int **)Ch_GlobalSymbolAddrByName(interp, "ch_a");  */
  printf("ch_a[0] = %d ch_a[1] = %d, ch_a[2] = %d\n", 
         ch_a[0], ch_a[1], ch_a[2]);

  /* ch_a is a pointer to int */
  ch_a = (int *)Ch_GlobalSymbolAddrByName(interp, "aa");
  /* or ch_a = *(int **)Ch_GlobalSymbolAddrByName(interp, "ch_a");  */
  printf("ch_a[0] = %d ch_a[1] = %d, ch_a[2] = %d\n",
         ch_a[0], ch_a[1], ch_a[2]);

  str = (char**)Ch_GlobalSymbolAddrByName(interp, "str"); 
  printf("str[0] = %s str[1] = %s\n", str[0], str[1]);

  Ch_VaEnd(interp, ap);
  return 0;
}
