
A1.2 Adding C/C
++
code: a simple dynamic link library 3
• All functions have the same format:
– OXCALL defines the calling convention;
– rtn is the return value of the function. It is a pointer to an OxVALUE which
is the container for any Ox variable. On input, it is an integer (OX
INT)of
value 0. If the function returns a value, it should be stored in rtn.
– pv is an array of cArg OxVALUEs, holding the actual arguments to the func-
tion.
– cArg is the number of arguments used in the function call. Unless the func-
tion has a variable number of arguments, there is no need to reference this
value.
• If the function is written in C
++
instead of C, it must be declared as:
extern "C" void OXCALL FnThrees
(OxVALUE *rtn, OxVALUE *pv, int cArg)
• First, we check whether the arguments are of type OX INT (we know that there are
two arguments, which have index 0 and 1 in pv). The call to OxLibCheckType
tests pv (the function arguments) from index 0 to index 1 for type OX
INT.
Arguments must be checked for type before being accessed. Make
sure there is a call to OxLibCheckType for each argument (unless
you inspect the arguments ‘manually’).
In this case, a double would also be valid, but automatically converted to an
integer by the OxLibCheckType function. Any other argument type would result
in a run-time error (checking for the number of arguments is done at compile
time).
• For convenience, we copy the first argument to r, and the second to c. OxInt
accesses the integer in an OxVALUE. The first argument is the array of OxVALUEs,
the second argument is the index in the array. This specifies the dimension of the
requested matrix.
• The return type is a matrix, and that matrix has to be allocated in the rtn value,
using the right dimensions. This is done with the OxLibValMatMalloc function.
A run-time error is generated if there is not enough memory to allocate the matrix.
• Finally we have to set all elements of the matrix to the value 3. OxMat accesses
the allocated matrix. The dimensions of that matrix are accessed by OxMatc,
OxMatr, but here we already know the dimensions.
Note that the function arguments, as contained in pv, may only be changed if they
are declared as const. It is best to never change the arguments in the function,ex-
cept from conversion from int to double and vice versa (automatic conversion using
OxLibCheckType is always safe). Another exception is when the argument is a pointer
in which the caller expects a return value. An example will follow shortly.
Commentaires sur ces manuels