Example of Passing Odd-Length String in User-Defined Type to C (69850)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • 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 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 = "an odd-length string" + CHR$(0)
element c = 54.6
CALL CFunc(element)
END

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

OUTPUT

Record.A = 123
Record.B = an odd-length string
Record.C = 54.599998

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB69850