1. Strings declared
in HTBasic that will be passed to or accessed by a DLL must be dimensioned
at least one character larger than the string will ever be.
C and C++ require
a NULL character to be at the end of every string. Although HTBasic
does not use this same scheme, it will accommodate the DLL by appending
a NULL character to a string as it is being passed. The string must
be large enough to hold the extra character. Strings that have been
set or modified by the DLL must also have the NULL character at the
2. DLLs may use
the _cdecl or _stdcall calling convention.
Some languages (such
as Visual Basic) that use the _stdcall calling convention are supported
via the stdcall keyword:
DLL GET "STDCALL VOID DLL:FUNCTION" AS "ALIAS"
DLL GET "CDECL VOID DLL:FUNCTION" AS "ALIAS"
DLL GET "VOID DLL:FUNCTION" AS "ALIAS" ! defaults to cdecl
3. DLLs must appropriately
use their access to HTBasic's memory.
There are two ways
of passing parameters to a function or routine: "by reference" and "by
value". "By value" means that a copy of the variable's value is passed
to the function, so the function has no way to affect the actual variable.
"By reference" means that the address in memory of the variable is sent
to the function, so if the function changes the variable, it is actually
changing the original. By default HTBasic passes variables by reference,
so the DLL would then have access into HTBasic's memory. Using that
access incorrectly will corrupt HTBasic's memory. The programmer needs
to make sure that the DLL is expecting the same type of variable that
HTBasic is passing. To force a variable to be passed by value instead
of by reference, enclose the variable in an extra set of parenthesis
in the parameter list. Literal numbers are always passed by value.
4. Each DLL function or routine called
from HTBasic can have a maximum of 80 bytes worth of parameters.
All parameters require 4 bytes except for
a REAL that is passed by value, which requires 8 bytes. Hence, there
can never be more than 20 parameters.
5. A DLL function or routine cannot have
the same name as an HTBasic function or routine.
Actually, they can have the same name,
but if they do, there will be no way to call the DLL function. HTBasic
will always find the HTBasic routine first.
6. HTBasic and the DLL must agree on the
size of the variable or literal being written to or read from.
HTBasic, C and C++ all designate sizes
of variables according to their declared types. If different size designations
for the same variable are made by HTBasic and the DLL, it is possible
to corrupt memory or even crash the system. It is up to the programmer
to enforce the compatibility between HTBasic and the DLL.