Int 21H [2.0] Function 46H (70) Redirect handle

Given two handles, makes the second handle refer to the same device or file at the same location as the first handle. The second handle is then said to be redirected.

Call with:

AH = 46H

BX = handle for file or device

CX = handle to be redirected

Returns:

If function successful

Carry flag = clear

If function unsuccessful

Carry flag = set

AX = error code

Notes:

If the handle passed in CX already refers to an open file, that file is closed first.

A seek, read, or write operation that moves the file pointer for one of the two handles also moves the file pointer associated with the other.

This function is commonly used to redirect the standard input and output handles to another file or device before a child process is executed with Int 21H Function 4BH.

Example:

Redirect the standard output to the list device, so that all output directed to the console will appear on the printer instead. Later, restore the original meaning of the standard output handle.

stdin equ 0

stdout equ 1

stderr equ 2

stdaux equ 3

stdprn equ 4

dhandle dw 0 ; duplicate handle

.

.

.

; get dup of stdout

mov ah,45h ; function number

mov bx,stdout ; standard output handle

int 21h ; transfer to MS-DOS

jc error ; jump if dup failed

mov dhandle,ax ; save dup'd handle

;

; redirect standard output

; to standard list device

mov ah,46h ; function number

mov bx,stdprn ; standard list handle

mov cx,stdout ; standard output handle

int 21h ; transfer to MS-DOS

jc error ; jump if redirect failed

.

.

.

; restore standard output

; to original meaning

mov ah,46h ; function number

mov bx,dhandle ; saved duplicate handle

mov cx,stdout ; standard output handle

int 21h ; transfer to MS-DOS

jc error ; jump if redirect failed

; close duplicate handle

; because no longer needed

mov ah,3eh ; function number

mov bx,dhandle ; saved duplicate handle

int 21h ; transfer to MS-DOS

jc error ; jump if close failed

.

.

.