Книга: Writing Windows WDM Device Drivers

NT Style Driver Installation

NT Style Driver Installation

NT style (non-WDM) kernel mode drivers for NT 3.51, NT 4, and Windows 2000[28] can be installed by hand. This means copying the driver executable to the Windows System32Drivers directory, making the right registry settings and rebooting the system. However, it is far safer to write an installation program to do the job[29].

You need to use the appropriate Win32 function calls to copy files and alter the registry. In addition, you must use the Windows 2000 Service Control Manager functions.

The code in install.cpp (on the book's CD-ROM) shows how to install an NT style driver. This is not a complete Win32 program. You must embed it in your own installation application.

The InstallDriver routine controls the whole installation process. It calls CreateDriver and StartDriver, when appropriate. FindInMultiSz is used to see if the driver name is in a REG_MULTI_SZ value. The code follows the logic laid out in the following sections to install a driver called "AbcDriver".

install.cpp installs two sets of registry values that I have not mentioned before.

• Event log registry entries are added. See Chapter 13 for details.

• The driver has a Parameters subkey. The values in this subkey are used to control some global features of the driver. You might like to write a Control Panel applet or other control application to let users modify these values.

Install Process

1. Get the Windows System32 directory using GetSystemDirectory. Use CopyFile to copy your driver to the System32Drivers directory.

2. Create the driver service (or stop the existing driver) — see the following.

3. Make the appropriate driver registry key, e.g., HKLMSYSTEMCurrentControlSetServicesAbcDriver using RegCreateKeyEx.

Set ErrorControl, Start, and Type registry values in the previous key using RegSetValueEx.

If desired, set Group, DependOnGroup, and DependOnService registry values, etc. as described later.

4. If desired, make a Parameters registry key (e.g., HKLMSYSTEMCurrentControlSetServicesAbcDriverParameters) and set any appropriate values.

5. Open the event log registry key at HKLMSYSTEMCurrentControlSetServicesEventLogSystem.

Read the Sources value from this key.

See if your driver is in this list. If not, add your null-terminated driver name to Sources and write it back to the registry.

6. Create the driver event log registry key at HKLMSYSTEMCurrentControlSetServicesEventLogSystemAbcDriver.

Set the TypesSupported and EventMessageFile values.

7. Start the driver — see the following.

Creating or Stopping a Driver Service

1. Open the Service Control Manager using OpenSCManager.

2. Try to open your driver service using OpenService.

If OpenService exists, see if it is currently running using ControlService SERVICE_CONTROL_INTERROGATE.

If OpenService is running, stop it using ControlService SERVICE_CONTROL_STOP.

Give the driver 10 seconds to stop, checking every second with ControlService SERVICE_CONTROL_INTERROGATE.

Close the driver service handle using CloseServiceHandle. Return.

3. Create the driver service using CreateService.

4. Close the Service Control Manager using CloseServiceHandle.

Starting a Driver

1. Open the Service Control Manager using OpenSCManager.

2. Open your driver service using OpenService.

3. Get the driver run state using ControlService SERVICE_CONTROL_INTERROGATE.

4. If need be, call StartService to start your driver.

Give it 10 seconds to start, checking every second with ControlService SERVICE_CONTROL_INTERROGATE.

5. Close the driver service and Service Control Manager using CloseServiceHandle.

Driver Load Order

In NT 3.51, NT 4, and Windows 2000 various registry entries affect the load order of drivers. While these entries can be used for WDM device drivers, the Plug and Plug enumeration process usually ensures that drivers are loaded in the right order.

Each driver can be put in a group. Tags determine the driver load order within a group. Each driver can insist that it is loaded after a particular group or driver has loaded. Groups are loaded in a ServiceGroupOrder.

The DependOnGroup entry in a driver's service registry key is a REG_MULTI_SZ stating which groups must be loaded before this driver. Similarly, DependOnService is a REG_MULTI_SZ listing the drivers and services that must be loaded before this driver.

The Group entry is a REG_SZ giving the driver's group name. Tag is a REG_DWORD giving the driver tag number. The HKLMSYSTEMCurrentControlSetControlGroupOrderList registry key has a REG_BINARY entry for each group. The first byte of this binary data is the tag count. The following DWORDs contain the tags of the drivers in the order that they should be loaded. Three NULL bytes pad out the binary data.

The HKLMSYSTEMCurrentControlSetControlServiceOrderList registry key has a REG_ MULTI_SZ entry called List. The strings in the list are the driver group names in the order that they should be loaded.

A driver's Start setting, shown in Table 11.4, will override all these driver loading rules.

NT 4 Control Panel Devices Applet

In NT 4 and NT 3.51, the Control Panel Devices applet can be used to start and stop drivers and set the Startup option. Figure 11.2 shows the Devices applet in action.

Figure 11.2 NT 4 Control Panel Devices


Windows 2000 Device Management

In Windows 2000, the recommended tool for most device management tasks is the Computer Management console. This is found in the Start menu Programs+Administrative tools+Computer Management option. The Device Manager is also available from the Control Panel System applet.

Figure 11.3 shows the Computer Management console running on my computer with the Device Manager Devices selected. You can get this same view from the Control Panel System applet; select the Hardware tab and click on Device Manager.

You can see Wdm1 and Wdm2 devices in the "Other Devices" (Unknown) category, along with the DebugPrint driver. Right-click on the driver name to uninstall it or update the driver from its properties box.

You can see any non-WDM drivers by right-clicking on Devices. Select View in the pop up menu. Check the "Show hidden devices" option. The devices display now includes a "Non-Plug and Play Drivers" category. You can start and stop the NT style drivers and change their startup options.

Figure 11.3 W2000 Computer Management console


The book software includes a small tool called Servicer in the Servicer subdirectory. Run this to see the display shown in Figure 11.4. "Parallel" has been typed into the Driver name box and the Lookup button has been pressed. The Parallel driver is found to be running. You can stop and start the Parallel driver using the appropriate buttons.

Servicer could be enhanced. With a bit of ingenuity, it could list all the available drivers in a list box. It could also be enhanced to change the Startup attributes in the same way as the NT 4 Devices Control Panel applet.

Do not try to stop a WDM driver that has a device attached. It will not work.

Figure 11.4 Servicer program


Windows 98 Device Management

The familiar Device Manager display is used to manage WDM devices in Windows 98. Start the System applet in the Control Panel. The Device Manager tab shows a display very similar to the right-hand pane of the Computer Management Console Devices windows shown in Figure 11.3.

The Control Panel Add New Hardware wizard is used to add some types of new device. The Add New Hardware wizard is run automatically if a new device is detected by a bus driver.

REG Files

A quick and dirty way of installing a series of registry settings is to use .REG files. Use RegEdit to export a branch of the registry. Run RegEdit on another computer to import the . REG file to create the same registry structure, entries, and values.

Two other Windows 2000 command line tools also do the same job. regdmp writes registry information to a REG file, regini imports a REG file.

REG files help when you only have to do one or two driver installations and you do not want to write a complete installation program. Another possible use is in product support; ask a customer to use RegEdit to export a portion of their registry to send to you.

Оглавление книги


Генерация: 1.083. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз