KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q60852: Passing Far Strings to C Using StringAddress and StringLength

Article: Q60852
Product(s): See article
Version(s): 7.00   | 7.00
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | SR# S900328-95 | mspl13_basic
Last Modified: 19-APR-1990

Programs compiled with the /Fs option, functions and procedures
(compiled with /Fs) in a Quick library, and programs designed to run
under the QuickBASIC Extended (QBX.EXE) environment now need to handle
strings passed to non-BASIC routines differently.

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 Hex 0 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\n", segadd);
  printf("Len: %i\n", 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

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.