How to Create a Near String when Using Far Strings (85265)



The information in this article applies to:

  • Microsoft Basic Professional Development System for MS-DOS 7.1

This article was previously published under Q85265

SUMMARY

This article describes how a Basic program compiled for far strings can create and pass a near string descriptor to a procedure written in C or a language other than Basic.

MORE INFORMATION

The following example Basic program creates a near string descriptor and calls a sample C function that expects to receive a near string descriptor:

Example Program

TYPE descriptortype     ' This TYPE statement defines a near string
                        ' descriptor.
    length AS INTEGER
    offset AS INTEGER
END TYPE

DECLARE SUB stringnear CDECL (descript AS descriptortype)

DIM descriptor AS descriptortype
CLS
DIM tempstring AS STRING * 255  ' This string must be one character
                                ' longer than the maximum length you
                                ' expect your string to be.
a$ = "It Works!!" + CHR$(0)     ' Some library routines expect a NUL
                                ' appended to the end of the string.
descriptor.length = LEN(a$)
tempstring = a$
descriptor.offset = VARPTR(tempstring)' Get the address of the string.
PRINT "In basic length of the string is "; LEN(a$)
CALL stringnear(descriptor)' Call the C subprogram.
PRINT: PRINT: PRINT
END
				
The following C function receives a near string descriptor as a parameter:
#include <stdio.h>
struct descriptor_struct  /* Defines the near string descriptor. */ 
{
  int length;
  char *offset;
};

void stringnear(struct descriptor_struct *descriptor){
  printf("In C length of the string is: %d \n\n", descriptor->length);
  printf("The string text is : %s \n\n", descriptor->offset);
}
				
Compile the Basic program above with:
   BC /FS Basic.BAS;
				
Compile the C program above with:
   CL -c -AM  C.C
				
(NOTE: The C compiler used to compile this program must be compatible with Basic PDS.)

Link the modules together with:
   LINK /NOE Basic.OBJ+C.OBJ,,, BRT71EFR.LIB+MLIBCE.LIB;
				
For more information about calling C from Basic, query on the following word in the Microsoft Knowledge Base:

BAS2C


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