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 >
Wrap
Text File
|
1992-04-11
|
9KB
|
241 lines
FORCE FAQ (Frequently Asked Questions) (inter_c 1.2) 1
--------------------------------------------------------------------
Topic: INTERFACING WITH C Author: David Holmes
What you'll find here:
( ) General discussion and easy FAQ answers
( ) How to pass simple parameters from FORCE to C functions
( ) How to pass arrays between FORCE and C functions
( ) Other, miscellaneous, but frequently asked C/FORCE questions
Examples: C_EXMPL1.ZIP, C_EXMPL2.ZIP, C_EXMPL3.ZIP
General discussion and easy FAQ answers:
-------------------------------------------------------------------
Mixing FORCE with C is one of FORCE's obvious attractions and
major strong points. However, there are a few pitfalls that you
have to watch out for, but we'll come to those later. For now,
let's look at the bright side.
Compatibility:
-------------
Q: Is FORCE compatible with XxX brand C compiler?
A: It is possible to mix FORCE with C code generated by any
compiler that follows the MicroSoft standard for object code
generation. That includes at least MicroSoft and Borland, though
there are probably others.
You will problems using the Borland C++ [2,3].0 C library,
however, since several symbols have been added to it that conflict
with the turboc.obj startup object code. Borland has also greatly
changed their memory management techniques, and many functions may
not be compatible with FORCE at all.
I've heard that Zortech C code works, but I can't confirm it.
Q: Is FORCE compatible with any C++ compilers?
A: None that I know of. C++ object code is usually somewhat
different from standard MicroSoft object code, due to 'Class'
storage. However, there are a few UNIX C++ compilers that DO
generate 'correct' UNIX object code by translating C++ into C first.
These compilers obviously won't do you any good, but you may be able
to find a DOS version of that compiler (if you do, and you can get
it to work, let me know). You might want to try the GNU ports.
Q: Is FORCE compatible with XxX brand C Linkers?
A: FORCE is compatible with Borland's TLINK (versions 2.0 and
under), and with MicroSoft LINK (versions 3.65 and lower). Blink
Inc.'s BLINKER 1.5 works also, and I've heard that Zortech's BLINK
(no relation to Blink, Inc.'s BLINKer) works okay. Any others and
you're on your own, so to speak.
--------------------------------------------------------------------
1
FORCE FAQ (Frequently Asked Questions) 2
--------------------------------------------------------------------
Linking C objects and FORCE objects:
-----------------------------------
Q: I have the C code written, and the FORCE code written, now
how do I put them together?
A: Follow two rules of thumb:
1) ALWAYS remember to compile your C code with the Large
memory model. If you don't, you'll probably get a
corrupted stack when you try to run your program.
2) If you call any C library functions from either your C
or your FORCE routines, be sure to call the appropriate
setup-*() function, and be sure to link with the startup
code provided with FORCE, or you'll get subtle and]
nearly untraceable bugs in your .EXE.
If you don't have both of these right, you'll probably never
get to the next questions.
How FORCE Passes Parameters
--------------------------------------------------------------------
Before I answer the questions coming up, I think we should
have a quick discussion on how FORCE passes parameters. Understand-
ing of this is crucial to mixing C and FORCE effectively, so here
we go:
FORCE passes parameters in two different ways: by reference
and by value. When you pass by reference, FORCE passes the address
of the parameter rather than pushing the entire parameter on to the
stack, as is done when you pass by value. That is, when you pass
by value, you pass a copy of the original, and when you pass by
reference, you pass a pointer to the original.
FORCE decides on how to pass parameters by looking at the
function prototypes, and checking to see whether the "PARAMETERS"
part of the function prototype has the modifiers VALUE or CONST.
Make sense? Clear as mud, right? Here's code to say what I mean:
FUNCTION INT hash PROTOTYPE
PARAMETERS VALUE INT && will pass the actual integer, say 5
FUNCTION INT hash2 PROTOTYPE
PARAMETERS INT && will pass the full address, say 023A:0004
How to pass simple parameters from FORCE functions to C functions
--------------------------------------------------------------------
Q: What do I have to know to pass any kind of FORCE data type to
a C function?
A: You can pass any FORCE data type to C, but only CHAR, (U)INT,
(U)LONG, LOGICAL, and DBL will be easy, so let's talk about
them.
--------------------------------------------------------------------
2
FORCE FAQ (Frequently Asked Questions) 3
--------------------------------------------------------------------
But first, if you haven't read the above section, "How FORCE
passes Parameters," do so now.
The simple FORCE data types, CHAR, (U)INT, etc, correspond
directly to the C data types you'd expect, all except for CHAR, which
we'll talk about in a second.
FORCE data types corresponding C data types Byte Length
------------------ ----------------------------- -----------
LOGICAL int 2
BYTE unsigned char 1
INT int 2
UINT unsigned int 2
LONG long 4
ULONG unsigned long 4
DBL double 8
CHAR char * ( or char [] ) 4
*- the following data types have no equivalent in C,
*- But if you need to pass these, you can get help from
*- Tech Support. We'll send you the corresponding struct { }'s
DATE /* none */
FILE /* none */
MEMO /* none */
ALIAS /* none */
When you write C functions that will be called from FORCE,
you must know what type of data to expect as parameters: either a
VALUE or an ADDRESS. The C functions corresponding to the two hash
functions in the above section would have to look like this:
int hash( int key ) { /* etc */ }
int hash2( int *key ) { /* etc */ }
Beware when you pass strings. If you declare something like this:
VARDEF
char string
ENDDEF
FORCE will allocate 255 spaces for the string, not one, and if you
pass "string" by VALUE, FORCE will push each of the 255 characters
on to the stack, which is probably not what you want if you're
interfacing with C.
How to pass arrays between FORCE and C functions
--------------------------------------------------------------------
Q: I'm having trouble passing arrays between FORCE and C. What's
wrong?
A: Before we go any further, be sure that you have read the above
section, "How FORCE Passes Parameters."
--------------------------------------------------------------------
3
FORCE FAQ (Frequently Asked Questions) 4
--------------------------------------------------------------------
When you pass an array of any kind to a C function, be SURE that you
do not prototype your parameters with either CONST or VALUE, because
C ALWAYS expects the address of an array, not the array itself.
Here's a quick example of a prototype you might write:
FUNCTION INT hash PROTOTYPE
PARAMETERS INT array[10] && passes the address
and the corresponding C function would look like:
int hash( array )
int array[]; /* or int *array */
Finally, when you call your C function, pass the array like this:
hashvalue = hash( key[] )
Other, miscellaneous, but frequently asked C/FORCE questions
--------------------------------------------------------------------
Q: What is the FORCE equivalent of a C structure?
A: Unfortunately, there is no equivalent of a user-defined
structure in the current version of FORCE.
Q: Will a future release provide one?
A: We don't know. There are no current plans for user-defined
structures, but the idea has certainly come up before.
-------------------------------------------------------------------
4