Explanation of Why Basic Programs Can Open Devices As Files (65935)



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
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 7.1
  • Microsoft GW-BASIC 3.2
  • Microsoft GW-BASIC 3.22
  • Microsoft GW-BASIC 3.23

This article was previously published under Q65935

SUMMARY

This article explains how Microsoft Basic compilers, QuickBasic, and GW-Basic can open character device drivers as if they were files, as in the following line of code:
   OPEN "\DEV\CON" FOR OUTPUT AS #1
				
Basic can interact with character device drivers in this way because this feature is built into MS-DOS.

This information applies to Microsoft QuickBasic versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50 for MS-DOS; to Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS; to Microsoft Basic Professional Development System (PDS) 7.00 and 7.10 for MS-DOS; and to all versions of GW-Basic (3.23, 3.22, 3.20 and earlier) for MS-DOS.

MORE INFORMATION

DOS supports two types of device drivers: character device drivers and block device drivers. Character device drivers drive those devices that input and output data one character at a time. A typical character device is the keyboard or the line printer.

DOS provides the ability to open the character device driver for I/O; you can access the driver by name as if it were a file. The name can be up to eight characters in length. This "name" is internal to the device driver and is unrelated to the actual piece of hardware you are interacting with. DOS keeps a list internally of the names of character device drivers installed at boot time plus some default devices that are always present.

DOS provides several character device drivers by default. Any program can open CON (the console), AUX (the communications port), PRN (the line printer), and LPT1 or LPT2. For example, when you install ANSI.SYS, this device driver replaces the default DOS CON device driver; therefore, anything printed to the CON device will actually be filtered by ANSI.SYS.

For an example of sending escape codes to ANSI.SYS in Basic through the CON device, search for a separate article in this Knowledge Base using the following words:

ANSI and ESCAPE and CON and QUICKBasic

Some character device drivers can interact with your program by sending and receiving control data strings. Basic provides the IOCTL$ function and the IOCTL statement to input and output data control strings to character device drivers.

Note: Very few device drivers support IOCTL data control strings. Basic's IOCTL and IOCTL$ work correctly only with device drivers that have this ability built in.

For more information on device drivers, consult a book on programming DOS such as "Advanced MS-DOS Programming, Second Edition" by Ray Duncan, published by Microsoft Press (1988).

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