Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  Function 46H (decimal 70) has a somewhat misleading name because it really
  does not create a duplicate handle as does function 45H. Instead, function
  46H associates an existing open handle with a different device. This is
  the key to implementing input/output redirection in DOS.

  Call function 46H with an open handle in BX and a second handle in CX.
  When function 46H returns, the handle in CX is associated with the same
  device as the open handle in BX. If the handle in CX was previously
  associated with an open device, function 46H closes the device (which
  might otherwise be without a handle). If no errors occur, the function
  clears the carry flag. Otherwise, the carry flag is set, and AX contains
  an error code: 04H (no more handles) or 06H (invalid handle).

  To see how function 46H works, consider how you would redirect output from
  the standard output device (the video screen) to a file:

  mov bx,stdout           ; BX = handle of standard output device
  mov ah,45h              ; AH = function number ("Duplicate Handle")
  int 21h                 ; get duplicate handle into AX
  jc  Error               ; (trap errors)
  mov stdoutDup,ax        ; save the duplicate handle in a memory variable

  mov bx,FileHandle       ; BX = handle of open file
  mov cx,stdout           ; CX = handle to be redirected
  mov ah,46h              ; AH = function number ("Force Duplicate Handle")
  int 21h                 ; redirect stdout to the file
  jc  Error
                          ; at this point, all output to stdout
                          ;   goes into the file

  To undo this redirection, associate the standard output device with the
  saved duplicate:

  mov bx,stdoutDup        ; BX = duplicate of previous stdout
  mov cx,stdout           ; CX = handle to be redirected
  mov ah,46h              ; AH = function number ("Force Duplicate Handle")
  int 21h                 ; restore stdout to what it was
  jc  Error

  mov bx,stdoutDup        ; BX = duplicate
  mov ah,3Eh              ; AH = function number ("Close")
  int 21h                 ; discard duplicate handle

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson