## Physical Device Driver Reference

[prev][next][parent][TOC]

### Redirecting Data to Another Device

Character device monitors can be used to redirect data from one character device to another, as long the corresponding physical device drivers provide monitor support. This is done by:

1.Opening both devices

2.Registering a monitor with the first device

3.Registering a monitor with the second device

4.Intercepting data from the data stream belonging to the first device by calling DosMonRead

5.Using the data taken from the data stream belonging to the first device and creating a monitor record in the format defined by the second physical device driver

6.Placing the new data record into the data stream belonging to the second device by calling DosMonWrite

The following pseudocode example shows how keystroke data can be redirected to the printer:

    --------------------------------------------------------------

                    Redirecting Keystroke Data

      CALL DosMonOpen to open keyboard device and obtain a monitor
        handle for the keyboard
      CALL DosMonOpen to open printer device and obtain a monitor
        handle for the printer
      CALL DosSetPrty to set priority of thread high
      CALL DosMonReg to register a keyboard monitor for the data
        stream associated with the current session
      CALL DosMonReg to register a printer monitor for the printer
        device's "data" monitor chain

    WHILE continuing to redirect data

      CALL DosMonRead to take a data record from the keyboard data stream
      CALL DosMonWrite to return the keyboard monitor data record
        to the keyboard device driver.
           Some data (for example, FLUSH records) must be returned
             to the keyboard data stream.
           Translate the keystroke monitor data record into a
             printer monitor data record.
      CALL DosMonWrite to place the printer monitor data record
        into printer data stream

    END WHILE

      CALL DosMonClose to terminate the printer monitor and close
        the printer monitor handle
      CALL DosMonClose to terminate the keyboard monitor and
        close the keyboard monitor handle
      CALL DosExit to terminate this application

    --------------------------------------------------------------

Note: Physical device drivers and the monitor dispatcher expect the return of certain monitor data records from its monitors. This requires that certain data not only be redirected to another data stream, but also be returned to its original data stream.

The advantage of this technique of redirecting data from one device to another is that it can be done without modifying or rewriting a character device driver. The disadvantage of using this technique is reduced performance. When performance is critical, data must be redirected between physical device drivers by using the inter-device-driver communication mechanism provided by OS/2 2.1 (and later).