Using the Handle Functions

The handle file- and record-management functions access files in a fashion similar to that used under the UNIX/XENIX operating system. Files are designated by an ASCIIZ string (an ASCII character string terminated by a null, or zero, byte) that can contain a drive designator, path, filename, and extension. For example, the file specification

C:\SYSTEM\COMMAND.COM

would appear in memory as the following sequence of bytes:

43 3A 5C 53 59 53 54 45 4D 5C 43 4F 4D 4D 41 4E 44 2E 43 4F 4D 00

When a program wishes to open or create a file, it passes the address of the ASCIIZ string specifying the file to MS-DOS in the DS:DX registers (Figure 8-6). If the operation is successful, MS-DOS returns a 16-bit handle to the program in the AX register. The program must save this handle for further reference.

mov ah,3dh ; function 3dh = open

mov al,2 ; mode 2 = read/write

mov dx,seg filename ; address of ASCIIZ

mov ds,dx ; file specification

mov dx,offset filename

int 21h ; request open from DOS

jc error ; jump if open failed

mov handle,ax ; save file handle

.

.

.

filename db 'C:\MYDIR\MYFILE.DAT',0 ; filename

handle dw 0 ; file handle

Figure 8-6. A typical handle file operation. This sequence of code attempts to open the file designated in the ASCIIZ string whose address is passed to MS-DOS in the DS:DX registers.

When the program requests subsequent operations on the file, it usually places the handle in the BX register before the call to MS-DOS. All the handle functions return with the CPU's carry flag cleared if the operation was successful, or set if the operation failed; in the latter case, the AX register contains a code describing the failure.

MS-DOS restricts the number of handles that can be active at any one time——that is, the number of files and devices that can be open concurrently when using the handle family of functions——in two different ways:

The maximum number of concurrently open files in the system, for all active processes combined, is specified by the entry

FILES=nn

in the CONFIG.SYS file. This entry determines the number of entries to be allocated in the system file table; under MS-DOS version 3, the default value is 8 and the maximum is 255. After MS-DOS is booted and running, you cannot expand this table to increase the total number of files that can be open. You must use an editor to modify the CONFIG.SYS file and then restart the system.

The maximum number of concurrently open files for a single process is 20, assuming that sufficient entries are also available in the system file table. When a program is loaded, MS-DOS preassigns 5 of its potential 20 handles to the standard devices. Each time the process issues an open or create call, MS-DOS assigns a handle from the process's private allocation of 20, until all the handles are used up or the system file table is full. In MS-DOS versions 3.3 and later, you can expand the per-process limit of 20 handles with a call to Int 21H Function 67H (Set Handle Count).

The handle file- and record-management calls may be gathered into the following broad classifications for study:

Function Action

Common handle file operations

3CH Create file (requires ASCIIZ string).

3DH Open file (requires ASCIIZ string).

3EH Close file.

Common handle record operations

42H Set file pointer (also used to find file size).

3FH Read file.

40H Write file.

Less commonly used handle operations

41H Delete file.

43H Get or modify file attributes.

44H IOCTL (I/O Control).

45H Duplicate handle.

46H Redirect handle.

56H Rename file.

57H Get or set file date and time.

5AH Create temporary file (versions 3.0 and later).

5BH Create file (fails if file already exists;

versions 3.0 and later).

5CH Lock or unlock file region (versions 3.0 and

later).

67H Set handle count (versions 3.3 and later).

68H Commit file (versions 3.3 and later).

6CH Extended open file (version 4).

Compare the groups of handle-type functions in the preceding table with the groups of FCB functions outlined earlier, noting the degree of functional overlap. Section 2 of this book, "MS-DOS Functions Reference," gives detailed specifications for each of the handle functions, along with assembly-language examples.