Article Q60852
◄Contents► ◄Index► ◄Back►
─────────────────────────────────────────────────────────────────────────────
◄Knowledge Base Contents► ◄Knowledge Base Index►
Passing Far Strings to C Using StringAddress and StringLength - Q60852
The following program demonstrates how to pass a variable-length far
string to a Microsoft C function using the Basic run-time routines
StringAddress and StringLength. These routines are necessary to obtain
the string's far address and length.
Code Example
------------
'----------- Here is the file TESTB.BAS
DECLARE SUB TestC CDECL (A$)
A$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + CHR$(0)
' Add 0h at the end for the C printf function.
CLS : PRINT : PRINT
PRINT "BASIC: "; A$
PRINT "Len: "; LEN(A$)
PRINT
CALL TestC(A$)
LOCATE CSRLIN + 3
PRINT "Back in BASIC"
SYSTEM
// ---------- Here is the file TESTC.C
extern char far * pascal StringAddress(long near *);
extern int pascal StringLength(long near *);
void TestC (long near * Desc)
{
int len;
char far *segadd;
len = StringLength( Desc );
segadd = StringAddress( Desc );
printf("C: %s\", segadd);
printf("Len: %i\", len);
}
Compile and link options, as follows:
BC /o TESTB;
CL -c -AM TESTC.C
LINK /noe TESTB TESTC;
The output should from this program should be as follows:
BASIC: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Len: 27
C: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Len: 27
Back in BASIC