Microsoft KB Archive/47756

= Example of C Function Returning a String to Basic =

Article ID: 47756

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q47756



SUMMARY
The two programs shown below demonstrate how a C function can return a string to a compiled Basic program.

For Microsoft Basic PDS 7.00 and 7.10, this example applies only to near strings. If you are using far strings (BC /Fs on compile or when using QBX.EXE), 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 far strings, see Chapter 13 of &quot;Microsoft Basic 7.0: Programmer's Guide&quot; 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, query in the Microsoft Knowledge Base using the following word:

BAS2C

Code Example
The following Basic program is BSTRF.BAS, which calls the C function and prints out the returned string and its length: DECLARE FUNCTION CFUNC$ CDECL a$ = CFUNC$ PRINT a$  PRINT len(a$) The following program is CSTRF.C, which builds a string descriptor that is passed back to the calling Basic program: struct stringdesc {       int length;        /* length of the string */ char *string;     /* near pointer to the string */ }; struct stringdesc *std; char thestring[18];     /* In the medium memory model this  */ /* string will be in DGROUP - which */ /* is required for Basic   */ struct stringdesc *cfunc { std->length = 18;      /* length of the string */ strcpy(thestring, &quot;This is the string&quot;); std->string = thestring; return(std);          /* return pointer to string descriptor */ } 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:
 * 1) include 

This is the string

18

Additional query words: QuickBas BasicCom

Keywords: KB47756

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.