Example of Basic Function Returning a String to C (48205)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b

This article was previously published under Q48205

SUMMARY

The two programs below demonstrate how a Microsoft Basic function can return a string to C.

For Basic PDS 7.00 and 7.10, this example applies to near strings only. If you are using far strings (/Fs during compile or in the QBX.EXE environment), you must use the string-manipulation routines supplied with Basic PDS 7.00 and 7.10 (StringAssign, StringRelease, StringAddress, and StringLength). For more information about using far strings, see Chapter 13 of the "Microsoft Basic 7.0: Programmer's Guide" for versions 7.00 and 7.10.

MORE INFORMATION

For more information about passing other types of parameters between Basic and C, and a list of which Basic and C versions are compatible with each other, search in the Microsoft Knowledge Base using the following word:

BAS2C

Code Example

The following Basic program is BSTRF.BAS, which contains a function that returns a string to a calling C routine:
   DECLARE SUB CSUB CDECL ()
   CALL CSUB
   END

   FUNCTION basvarfunc$(dummy%)
      basvarfunc$ = "This is the string"
   END FUNCTION
				
The following program is CSTRF.C, which calls a Basic routine that returns a string. A string descriptor is created to receive the data returned by the Basic function.
#include <stdio.h>
struct stringdesc
       {
        int length;       /* string length */ 
        char *string;     /* string address */ 
       };
extern struct stringdesc * pascal basvarfunc(int *dummy);
struct stringdesc *std;
void csub()
{
   int i;

   std = basvarfunc(0);

   printf("Length of string: %2d\r\n", std->length);

   for(i = 0; i < std->length; i++)
      printf("%c", std->string[i]);

   printf("\r\n");

}
				
To demonstrate these programs from an .EXE program, compile and link as follows:

BC BSTRF.BAS;
CL /c /AM CSTRF.C;
LINK /NOE BSTRF CSTRF;

BSTRF.EXE produces the following output:
Length of String: 18
This is the string

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB48205