Passing Basic PDS Variable-Length String By Far Reference to C (79374)



The information in this article applies to:

  • 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

This article was previously published under Q79374

SUMMARY

The example of passing a Basic variable-length string to C by far reference on page 25 of the Microsoft application note titled, "How to Pass Parameters Between Basic and C," BQ0080, needs to be modified in two places to work with Basic PDS versions 7.0 and 7.1. The corrected example is shown below.

MORE INFORMATION

This application note (BQ0080) can be obtained by calling Microsoft Technical Support or by searching in the Microsoft Knowledge Base.

Please see the following articles in the Microsoft Knowledge Base:

50062 How to Pass Parameters Between Basic and C (Part 1 of 2)

71274 How to Pass Parameters Between Basic and C (Part 2 of 2)

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

The following are two changes to the example of "Passing Basic Variable-Length String to C by Far Reference" in the application note BQ0080:

  1. The first change is in the Basic code. The call to StringFar, which reads as follows:
          Call StringFar ( SADD(a$), VARSEG (a$), LEN(a$))
    						
    Should read as follows:
          Call StringFar ( SADD(a$), SSEG (a$), LEN(a$))
    						
    In Basic PDS, VARSEG is used to find the segment of fixed-length variables. SSEG must be used to get the segment address of far variable-length strings because the segment is obtained from the descriptor for these variables, not from the location of the data itself, as is the case with VARSEG.
  2. The second change you must make is in the C code. If you run the example, even with the above change, you will see that the first C printf fails, while the other two work. The problem is that the address being passed to the printf in the first printf is a near pointer, when the pointer needs to be a far address. To force the printf to interpret "a" as a far address, simply replace the %s with %Fs. If you compile the C code for large memory model, however, this problem does not occur, because all references are then far by default.

Corrected Code Example

REM ===== Basic PROGRAM =====

DECLARE SUB StringFar CDECL (_
        BYVAL p1o AS INTEGER,_
        BYVAL p1s AS INTEGER,_
        SEG p3 AS INTEGER)
CLS
a$ = "This is a test" + CHR$(0)
' For the second parameter in the call to StringFar:
' Use SSEG(a$) for Basic PDS 7.0/7.1.
' Use VARSEG(a$) for QuickBasic 4.0/4.0b/4.5 or Basic Compiler 6.0/6.0b.
CALL StringFar(SADD(a$), SSEG(a$), LEN(a$))
END

/* ===== C ROUTINE ===== /*

#include <stdio.h>
void StringFar(a,len)
   char far *a;
    int *len;
 {
    int i;
    /*  If linking this C code with Basic PDS, use %Fs as shown: */ 
    printf("The string is : %Fs \n\n",a);
    printf(" Index       Value       Character\n");
    for (i=0;i < *len; i++)
       {
         printf("  %2d          %3d            %c\n",i,a[i],a[i]);
       };
 }
				
===== OUTPUT =====

The string is : This is a test

 Index       Value       Character
   0           84            T
   1          104            h
   2          105            i
   3          115            s
   4           32
   5          105            i
   6          115            s
   7           32
   8           97            a
   9           32
  10          116            t
  11          101            e
  12          115            s
  13          116            t
  14            0
				

Modification Type:MajorLast Reviewed:10/20/2003
Keywords:kbcode KB79374