## The PNPEVENT Sample

The PNPEVENT Sample accompanies Chapter 6 and illustrates how to intercept Plug and Play events in user mode.

The sample includes two components:

- PNPEVENT.SYS (SYS subdirectory) is a WDM driver for a non-existent hardware device.
- TEST.EXE (TEST subdirectory) is a user-mode test program for PNPEVENT.SYS.

### Building PNPEVENT

To build PNPEVENT.SYS, follow the general instructions for building a WDM sample. The Microsoft Visual C++ project file is named PNPEVENT.DSP in the SYS subdirectory.

To build TEST.EXE, follow the general instructions for building user-mode programs. The Microsoft Visual C++ project file is named TEST.DSP in the TEST subdirectory.

### Testing PNPEVENT

First launch the TEST.EXE program from the TEST\DEBUG subdirectory. TEST will present a simple dialog box with the title "PnP Event Driver Test Applet".

With the TEST dialog visible, install the PNPEVENT driver. Follow the general instructions for using FastInst in Windows XP or Windows 98/Me. The INF file is named SYS\PNPEVENT.INF.

In Windows 98/Me, you’ll also need to have

WDMSTUB.SYS installed in your drivers directory. PNPEVENT.SYS will otherwise fail to load because of its reference to IoReportTargetDeviceChangeAsynchronous.

When you install PNPEVENT, it registers a device interface for which TEST has called RegisterDeviceNotification. You should see a message about the arrival of the device.

Now try disabling PNPEVENT in the Device Manager. In Windows XP, Device Manager will ask you if you’re sure you want to disable the device, and you should answer, "Yes." (In Windows 98/Me, you'll be checking the check box labelled "Disable In This Hardware Profile" on the properties page, and the Device Manager won't acknowledge your selection.) TEST will also ask you if it’s OK to remove the device. Answering "No" at this point will prevent PNPEVENT from actually being removed until you reboot the computer.

If you answer "Yes" to TEST’s query about removing the device, TEST will then present a message box to inform you it’s closing its device handle. Thereafter, you’ll see a message in TEST’s dialog to the effect that the PNPEVENT device has been removed.

The Send Event button will be enabled while the PNPEVENT device is present, but you won’t be able to see what it does unless you run the

PNPMON sample at the same time.

It's useful to run the PNPDTEST in Windows XP in conjunction with this test. (Do I detest PnP? Not half so much as I detest Power Management.)

### What to look for

The interesting code in this sample is in TestDlg.cpp, which contains comments describing how to request and deal with device notifications.