home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / FOXPRO / FFAQ / INTER_C.FAQ < prev    next >
Text File  |  1992-04-11  |  9KB  |  241 lines

  1.     FORCE FAQ (Frequently Asked Questions)   (inter_c 1.2)             1
  2.     --------------------------------------------------------------------
  3.  
  4.     Topic:   INTERFACING WITH C                     Author: David Holmes
  5.  
  6.     What you'll find here:
  7.  
  8.        ( ) General discussion and easy FAQ answers
  9.        ( ) How to pass simple parameters from FORCE to C functions
  10.        ( ) How to pass arrays between FORCE and C functions
  11.        ( ) Other, miscellaneous, but frequently asked C/FORCE questions
  12.  
  13.     Examples: C_EXMPL1.ZIP, C_EXMPL2.ZIP, C_EXMPL3.ZIP
  14.  
  15.     General discussion and easy FAQ answers:
  16.     -------------------------------------------------------------------
  17.  
  18.        Mixing FORCE with C is one of FORCE's obvious attractions and
  19.     major strong points.  However, there are a few pitfalls that you
  20.     have to watch out for, but we'll come to those later.  For now,
  21.     let's look at the bright side.
  22.  
  23.     Compatibility:
  24.     -------------
  25.  
  26.     Q:    Is FORCE compatible with XxX brand C compiler?
  27.  
  28.     A:    It is possible to mix FORCE with C code generated by any
  29.     compiler that follows the MicroSoft standard for object code
  30.     generation.  That includes at least MicroSoft and Borland, though
  31.     there are probably others.
  32.  
  33.           You will problems using the Borland C++ [2,3].0 C library,
  34.     however, since several symbols have been added to it that conflict
  35.     with the turboc.obj startup object code.  Borland has also greatly
  36.     changed their memory management techniques, and many functions may
  37.     not be compatible with FORCE at all.
  38.  
  39.     I've heard that Zortech C code works, but I can't confirm it.
  40.  
  41.     Q:    Is FORCE compatible with any C++ compilers?
  42.  
  43.     A:    None that I know of.  C++ object code is usually somewhat
  44.     different from standard MicroSoft object code, due to 'Class'
  45.     storage.  However, there are a few UNIX C++ compilers that DO
  46.     generate 'correct' UNIX object code by translating C++ into C first.
  47.     These compilers obviously won't do you any good, but you may be able
  48.     to find a DOS version of that compiler (if you do, and you can get
  49.     it to work, let me know).  You might want to try the GNU ports.
  50.  
  51.     Q:    Is FORCE compatible with XxX brand C Linkers?
  52.  
  53.     A:    FORCE is compatible with Borland's TLINK (versions 2.0 and
  54.     under), and with MicroSoft LINK (versions 3.65 and lower).  Blink
  55.     Inc.'s BLINKER 1.5 works also, and I've heard that Zortech's BLINK
  56.     (no relation to Blink, Inc.'s BLINKer) works okay.  Any others and
  57.     you're on your own, so to speak.
  58.  
  59.     --------------------------------------------------------------------
  60.                                                                        1
  61.     FORCE FAQ (Frequently Asked Questions)                             2
  62.     --------------------------------------------------------------------
  63.  
  64.     Linking C objects and FORCE objects:
  65.     -----------------------------------
  66.  
  67.     Q:    I have the C code written, and the FORCE code written, now
  68.           how do I put them together?
  69.  
  70.     A:    Follow two rules of thumb:
  71.              1) ALWAYS remember to compile your C code with the Large
  72.                 memory model.  If you don't, you'll probably get a
  73.                 corrupted stack when you try to run your program.
  74.  
  75.              2) If you call any C library functions from either your C
  76.                 or your FORCE routines, be sure to call the appropriate
  77.                 setup-*() function, and be sure to link with the startup
  78.                 code provided with FORCE, or you'll get subtle and]
  79.                 nearly untraceable bugs in your .EXE.
  80.  
  81.           If you don't have both of these right, you'll probably never
  82.           get to the next questions.
  83.  
  84.  
  85.     How FORCE Passes Parameters
  86.     --------------------------------------------------------------------
  87.           Before I answer the questions coming up, I think we should
  88.     have a quick discussion on how FORCE passes parameters.  Understand-
  89.     ing of this is crucial to mixing C and FORCE effectively, so here
  90.     we go:
  91.  
  92.           FORCE passes parameters in two different ways:  by reference
  93.     and by value.  When you pass by reference, FORCE passes the address
  94.     of the parameter rather than pushing the entire parameter on to the
  95.     stack, as is done when you pass by value.  That is, when you pass
  96.     by value, you pass a copy of the original, and when you pass by
  97.     reference, you pass a pointer to the original.
  98.  
  99.           FORCE decides on how to pass parameters by looking at the
  100.     function prototypes, and checking to see whether the "PARAMETERS"
  101.     part of the function prototype has the modifiers VALUE or CONST.
  102.     Make sense?  Clear as mud, right?  Here's code to say what I mean:
  103.  
  104.     FUNCTION INT hash PROTOTYPE
  105.        PARAMETERS VALUE INT      && will pass the actual integer, say 5
  106.  
  107.     FUNCTION INT hash2 PROTOTYPE
  108.        PARAMETERS INT      && will pass the full address, say 023A:0004
  109.  
  110.  
  111.     How to pass simple parameters from FORCE functions to C functions
  112.     --------------------------------------------------------------------
  113.     Q:    What do I have to know to pass any kind of FORCE data type to
  114.           a C function?
  115.  
  116.     A:    You can pass any FORCE data type to C, but only CHAR, (U)INT,
  117.           (U)LONG, LOGICAL, and DBL will be easy, so let's talk about
  118.               them.
  119.     --------------------------------------------------------------------
  120.                                                                        2
  121.     FORCE FAQ (Frequently Asked Questions)                             3
  122.     --------------------------------------------------------------------
  123.           But first, if you haven't read the above section, "How FORCE
  124.     passes Parameters," do so now.
  125.  
  126.           The simple FORCE data types, CHAR, (U)INT, etc, correspond
  127.     directly to the C data types you'd expect, all except for CHAR, which
  128.     we'll talk about in a second.
  129.  
  130.        FORCE data types   corresponding C data types    Byte Length
  131.        ------------------ ----------------------------- -----------
  132.        LOGICAL            int                            2
  133.        BYTE               unsigned char                  1
  134.        INT                int                            2
  135.        UINT               unsigned int                   2
  136.        LONG               long                           4
  137.        ULONG              unsigned long                  4
  138.        DBL                double                         8
  139.        CHAR               char *      ( or char [] )     4
  140.  
  141.        *- the following data types have no equivalent in C,
  142.        *- But if you need to pass these, you can get help from
  143.        *- Tech Support.  We'll send you the corresponding struct { }'s
  144.  
  145.        DATE               /* none */
  146.        FILE               /* none */
  147.        MEMO               /* none */
  148.        ALIAS              /* none */
  149.  
  150.           When you write C functions that will be called from FORCE,
  151.     you must know what type of data to expect as parameters:  either a
  152.     VALUE or an ADDRESS.  The C functions corresponding to the two hash
  153.     functions in the above section would have to look like this:
  154.  
  155.     int hash( int key )   { /* etc */  }
  156.  
  157.     int hash2( int *key ) { /* etc */  }
  158.  
  159.     Beware when you pass strings.  If you declare something like this:
  160.        VARDEF
  161.           char              string
  162.        ENDDEF
  163.  
  164.     FORCE will allocate 255 spaces for the string, not one, and if you
  165.     pass "string" by VALUE, FORCE will push each of the 255 characters
  166.     on to the stack, which is probably not what you want if you're
  167.     interfacing with C.
  168.  
  169.  
  170.     How to pass arrays between FORCE and C functions
  171.     --------------------------------------------------------------------
  172.  
  173.     Q:    I'm having trouble passing arrays between FORCE and C.  What's
  174.     wrong?
  175.  
  176.     A:    Before we go any further, be sure that you have read the above
  177.     section, "How FORCE Passes Parameters."
  178.  
  179.     --------------------------------------------------------------------
  180.                                                                        3
  181.     FORCE FAQ (Frequently Asked Questions)                             4
  182.     --------------------------------------------------------------------
  183.     When you pass an array of any kind to a C function, be SURE that you
  184.     do not prototype your parameters with either CONST or VALUE, because
  185.     C ALWAYS expects the address of an array, not the array itself.
  186.  
  187.     Here's a quick example of a prototype you might write:
  188.  
  189.        FUNCTION INT hash PROTOTYPE
  190.             PARAMETERS INT array[10]    && passes the address
  191.  
  192.     and the corresponding C function would look like:
  193.  
  194.        int hash( array )
  195.        int array[];      /* or int *array */
  196.  
  197.     Finally, when you call your C function, pass the array like this:
  198.  
  199.        hashvalue = hash( key[] )
  200.  
  201.  
  202.  
  203.     Other, miscellaneous, but frequently asked C/FORCE questions
  204.     --------------------------------------------------------------------
  205.  
  206.     Q:    What is the FORCE equivalent of a C structure?
  207.  
  208.     A:    Unfortunately, there is no equivalent of a user-defined
  209.     structure in the current version of FORCE.
  210.  
  211.     Q:    Will a future release provide one?
  212.  
  213.     A:    We don't know.  There are no current plans for user-defined
  214.     structures, but the idea has certainly come up before.
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.     -------------------------------------------------------------------
  240.                                                                       4
  241.