mov bx, OldHandle ;handle to duplicate
mov ah, 45h ;Duplicate File Handle
int 21h
jc error_handler ;carry set means error
mov NewHandle, ax ;refers to same file as OldHandle
Duplicate File Handle (Function 45h) creates a new file handle that can be used to read from or write to the same file or device associated with the original handle.
OldHandle
Identifies the handle to be duplicated.
If the function is successful, the carry flag is clear and the AX register contains the new handle. Otherwise, the carry flag is set and the AX register contains an error value, which may be one of the following:
Value | Name |
0004h | ERROR_TOO_MANY_OPEN_FILES |
0006h | ERROR_INVALID_HANDLE |
If this function is used to duplicate the handle of an open file, the file pointer for the new handle is set to the same position as the pointer for the old handle. Using either handle to read from or write to the file changes the file pointer for both handles.
Duplicate File Handle can also be used to keep a file open while its directory entry is changed. If a program creates a duplicate handle and then closes the original handle, the file's directory entry is updated, but the duplicate handle can still be used to read from or write to the file.
Function 46h Force Duplicate File Handle