Re: Accessing C variables from c4gl
Posted in 1991
Mitch C. Amiano writes - > Got a Question. How do I gain read access to a variable > that was declared in a C or ESQL/C file. If you don't mind doing this bottom-about-face, how 'bout like this ? 4GL code: ---8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<---- GLOBALS DEFINE intvar1, intvar2, intvar3, intvar4 INTEGER END GLOBALS MAIN CALL assignments() DISPLAY "In 4gl: ", intvar1, intvar2, intvar3, intvar4 CALL checking() END MAIN ---8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<---- C code: ---8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<---- #include <stdio.h> extern long intvar1, intvar2, intvar3, intvar4; int checking(nargs) int nargs; { printf("in C, intvar1: %d, intvar2: %d, intvar3: %d, intvar4: %d\\n", intvar1, intvar2, intvar3, intvar4); return 0; } int assignments(nargs) int nargs; { intvar1 = 1 * 1; intvar2 = 2 * 2; intvar3 = 3 * 3; intvar4 = 4 * 4; return 0; } ---8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<---- The GLOBALS definition in the 4gl code means that you can hit the variables from the C code. Note that 4gl integers are C longs, tho'. They're extern ints because the 4gl defines them, rather than just declaring them. Any easy way 'round this, guys, so the 4gl declares them and the C defines them ? > We have an application with both > ESQL/C and 4GL modules, which will need to operate under one > of two units of measure depending upon where it gets installed. > We want to have access to the C variables rather than writing > and maintaining library functions to do it. Ex: > > in C file: > #define GU_METRIC 1 > #define GU_ENGLISH 2 > int fgl_gu_english = GU_ENGLISH; > int fgl_gu_metric = GU_METRIC; > int fgl_what_measure(); /* returns GU_METRIC or GU_ENGLISH, depending */ > /* on a UNIX shell variable. */ > > in 4gl file: > > Function myfunc() # Do something specific for Metric, > something else for English > [stuff...] > if fgl_what_measure() = fgl_gu_metric then > [some other stuff...] > end if > End Function > > > We would prefer to maintain only one file of constants and > corresponding functions. If we have to, we'll write functions, > but thats not as clean to maintain. I'm not sure that globals vars are better than [global] functions. If you're just doing y/n tests, how about this ? 4GL code: ---8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<---- MAIN IF metric_meas() THEN DISPLAY "going metric ..." ELSE DISPLAY "staying imperial" END IF END MAIN ---8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<---- C code: ---8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<---- #include <stdio.h> char * getenv(); int metric_meas(nargs) int nargs; { char *s; s = getenv("MEAS_TYPE"); if (s != NULL) printf("got %s\\n", s); if (s && strcmp(s, "METRIC") == 0) /* going metric ... */ retint(1); else retint(0); /* default imperial */ return 1; /* one returned var */ } ---8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<---- > Mitch C. Amiano, Software Contractor > Rochester, NY > 716-226-2024 Hope this helps. -- Cheers - Tony. ____________________________________________________________________________ Tony Heskett: theskett@lhr-sys.ssf-sys.dhl.com | Vox: (+44) 81 747 8330 DHL Systems : Kings House, Great West Road, TW8 9HS | Fax: (+44) 81 747 0875