The following examples show how to access a COM area or individual COM variables from within a CSUB.
This example shows how to access a COM area from a C subprogram.
10 COM /Test/ B, INTEGER I(9), A$[100]
20 MAT I=(10)
30 CALL Tcom1
40 PRINT I(0)
50 END
60 SUB Tcom1
70 COM /Test/B,INTEGER I(*),A$
80 I(0)=I(2)*56
90 SUBEND
Prototype
1 COM /Test/ B,INTEGER I(9),A$[100]
2 END
10 SUB Tcom1
20 COM /Test/ B,INTEGER I(*),A$
30 SUBEND
C Program
#include "csub.h"
struct test /* COM /Test/ data area def */
{
T_FLT b; /* REAL B */
T_INT i[10]; /* INTEGER I(0:9) */
T_SUBS l; /* A$[100] current length */
U_CHAR a[100]; /* A$ string data */
};
tcom1( npar )/* ACCESS FULL COM DATA AREA */
int npar; /* number of parameters */
{
T_INT c; /* working variable */
struct test *t; /* COM data area pointer */
t = com_var("Test", 0, 0); /* get COM /Test/ data ptr */
if( !t ) /* area found? */
return( 47 ); /* no, area not found */
c = t->i[2]; /* yes, get 3rd INTEGER element */
t->i[0] = c * 56; /* set value of first element */
return( 0 ); /* no error */
}
The next example shows how to access individual COM variables and their array descriptions from within a C subprogram.
BASIC Program
10 COM /Test/ B, INTEGER I(9), A$[100]
20 MAT I=(1)
30 CALL TCOM2
40 PRINT I(0),I(1),I(2),A$
50 END
60 SUB TCOM2
70 COM /Test/ B, INTEGER I(*), A$
80 I(0)=RANK(I)
90 I(1)=BASE(I,1)
100 I(2)=SIZE(I,1)
110 A$="This is a test of strings"
120 SUBEND
Prototype
1 COM /Test/ B, INTEGER I(9), A$[100]
2 END
10 SUB Tcom2
20 COM /Test/ B, INTEGER I(*), A$
30 SUBEND
C Program
#include "csub.h"
#include <string.h>
tcom2( npar ) /* ACCESS SPECIFIC COM VARIABLES */
int npar; /* number of parameters */
{
intptr ip; /* integer pointer */
realptr dp; /* real pointer */
DIM *ep; /* dimension pointer */
T_STR *sp; /* string pointer */
dp = com_var("Test", 1, 0); /* get B data pointer */
ip = com_var("Test", 2, 0); /* get I data pointer */
ep = com_var("Test", 2, 1); /* get I dim entry ptr */
if( !dp || !ip || !ep) /* variables found? */
return( 47 ); /* no, variable not found */
ip[0] = NOD(ep); /* return number of subscripts */
ip[1] = ep->sbs[0].base; /* subscript base */
ip[2] = ep->sbs[0].size; /* and size values */
sp = com_var("Test", 3, 0); /* get A$ data pointer */
ep = com_var("Test", 3, 1); /* get A$ dim entry ptr */
if( !sp || !ep ) /* variables found? */
return( 47 ); /* no, variable not found */
memcpy( sp->str, "This is a test of strings", 25);
sp->clen = 25; /* set the length */
return( 0 ); /* no error */
}
This section described the com_var function, used to locate COM memory variables, the movable nature of COM data, and the use of COM statements in CSUB prototype definitions. Two example C programs were presented that demonstrate how to access COM variables.