(Continued from previous message)
4. C RTL (Run Time Library) functions
-------------------------------------
One last point that may be obvious to some of you is that any external C
functions you call from PowerBASIC can *NOT* use any of the C run-time
library functions (i.e. printf, itoa, etc.). The reason for that being
that the C run-time library is not available.
------------------------------------------------------------------------------
-
Let's look at a concrete (if somewhat simplistic) example. The following
C function takes an integer, adds it to itself and returns the result to
your PowerBASIC program.
C source.
---------
/------------------------------------------------------------------------
// File: times2.c
// Desc: Example of a C function to be linked with PowerBASIC code
//
// Compile with: BCC -c -ml -zCcode -zRdata times2.c
//
// The above switches do the following:
// -c = Compile (only, don't link) to .OBJ file
// -ml = Use LARGE memory model (required for PowerBASIC)
// -zCcode = Name of code segment is CODE
// -zRdata = Name of data segment is DATA
//
// The int pascal below tells BCC (or BC) to use PASCAL parameter passing
// conventions (also used by PowerBASIC).
------------------------------------------------------------------------
int pascal times2(int x)
{
int y;
y = x + x;
return y;
}
PowerBASIC source.
------------------
=========================================================================
' File: ctest.bas
' Desc: Example of using a C routine with PowerBASIC
========================================================================
$CPU 8086 ' Works on all PCs
$DIM ALL ' All variables must be declared before using
hem
$LIB ALL OFF ' No additional libraries are needed
$ERROR ALL OFF ' Turn off all of PowerBASI's error checking
$COMPILE EXE ' Generate an EXEcutable file
$OPTIMIZE SPEED ' Optimize for faster code
DECLARE FUNCTION Times2(BYVAL INTEGER) AS INTEGER
$LINK "times2.obj"
DIM MyVar AS INTEGER
MyVar = 4 ' Assign an initial value to
yVar
PRINT "MyVar's initial value: "; MyVar ' Show that value
MyVar = Times2(MyVar) ' Call C routine to process it
PRINT "MyVar after Times2: "; MyVar ' Show new value
When you run this program, the first PRINT statement will show 4 which
is the value assigned to MyVar. The second PRINT statement will show
8 which is the value returned by the C function ""times2".
==============================================================================
=
Part II: Using Microsoft C, C++, etc...
==============================================================================
=
Most of what was said above concerning Borland C, C++ also applies to
Microsoft C, C++ with a few exceptions.
The following notes only apply to the command line version of the Microsoft
compiler. The Microsoft command line compiler is CL.EXE. This is a
protected mode program requiring a DPMI server to be available.
1. Parameter passing conventions
--------------------------------
See Part I section 1, above.
2. Memory model
---------------
The "C" code must be compiled using the LARGE memory model (because
PowerBASIC uses FAR (32-bit) pointers exclusively. This produces
(Continued to next message)
---
* QMPro 1.53 * VaporHelp - Verb; {call MicroSoft Tech Support}
--- WILDMAIL!/WC v4.12
---------------
* Origin: Toast House * (314) 994-0312 * (1:100/560.0)
|