Int 21H [2.0] Function 45H (69) Duplicate handle

Given a handle for a currently open device or file, returns a new handle that refers to the same device or file at the same position.

Call with:

AH = 45H

BX = handle to be duplicated

Returns:

If function successful

Carry flag = clear

AX = new handle

If function unsuccessful

Carry flag = set

AX = error code

Notes:

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 can be used to efficiently update the directory for a file that has changed in length, without incurring the overhead of closing and then reopening the file. The handle for the file is simply duplicated with this function and the duplicate is closed, leaving the original handle open for further read/write operations.

[3.3] See also Int 21H Function 68H (Commit File).

Example:

Duplicate the handle stored in the variable fhandle, then close the duplicate. This ensures that all buffered data is physically written to disk and that the directory entry for the corresponding file is updated, but leaves the original handle open for subsequent file operations.

fhandle dw 0 ; file handle

.

.

.

; get duplicate handle

mov ah,45h ; function number

mov bx,fhandle ; original file handle

int 21h ; transfer to MS-DOS

jc error ; jump if dup failed

; now close dup'd handle

mov bx,ax ; put handle into BX

mov ah,3eh ; function number

int 21h ; transfer to MS-DOS jc error

; jump if close failed

.

.

.