Microsoft KB Archive/69850

= Example of Passing Odd-Length String in User-Defined Type to C =

Article ID: 69850

Article Last Modified on 11/21/2006

-

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

-



This article was previously published under Q69850



SUMMARY
The following example demonstrates how to pass a user-defined type record containing odd-length fixed strings from compiled Basic to Microsoft C. By default, the C compiler packs structure members on two-byte boundaries. You must specify the compiler switch /Zp1 to force the C compiler to pack structure members on one-byte boundaries. This will prevent errors caused by alignment problems.

This information about inter-language calling applies to QuickBasic versions 4.00, 4.00b, and 4.50 for MS-DOS; to Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS and MS OS/2; and to Microsoft Basic Professional Development System version 7.00 and 7.10 for MS-DOS and MS OS/2.



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 knowledge base and in the Microsoft Knowledge Base using the following word:

BAS2C

To run the following code examples, compile and link as follows:

BC /O test.bas ;

CL /c /AM /Zp1 cfunc.c ;

LINK /noe test cfunc ;

If you fail to compile the C code with /Zp1, then the variable after the odd-length string will be zero or corrupted.

Code Example
' ===== Basic PROGRAM: test.bas ===== TYPE record a AS INTEGER b AS STRING * 19 c AS SINGLE END TYPE DECLARE SUB CFunc CDECL (p1 AS record) CLS DIM element AS record element.a = 123 element.b = &quot;an odd-length string&quot; + CHR$(0) element c = 54.6 CALL CFunc(element) END

/* ===== C ROUTINE: cfunc.c ===== */ struct record{ int a;       char b[19]; float c; }; void CFunc(struct record *element) { printf(&quot;Record.A = %d\n&quot;,element->a); printf(&quot;Record.B = %s\n&quot;,element->b); printf(&quot;Record.C = %f\n&quot;,element->c); }
 * 1) include 

OUTPUT
Record.A = 123

Record.B = an odd-length string

Record.C = 54.599998

Additional query words: QuickBas BasicCom

Keywords: KB69850

-

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

© Microsoft Corporation. All rights reserved.