13.2. Windows Dynamic Driver Loading

13.2.1. Windows Driver Types

Windows drivers can be implemented as either of the following types:

  • Windows Driver Model (WDM) drivers: Files with the extension *.sys on Windows 7/Vista/Server 2008/Server 2003/XP/2000/Me/98 (e.g., windrvr6.sys).
    WDM drivers are installed via the installation of an INF file (see below).
  • Non-WDM / Legacy drivers: These include drivers for non-Plug-and-Play Windows operating systems (Windows NT 4.0) and files with the extension *.vxd on Windows 98/Me, as well as all Kernel PlugIn driver files (e.g., MyKPDriver.sys).
    [Note]
    Starting from version 6.21 of WinDriver, *.vxd drivers are no longer supported.

The WinDriver Windows kernel module – windrvr6.sys – is a fully WDM driver, which can be installed using the wdreg utility, as explained in the following sections.

13.2.2. The wdreg Utility

WinDriver provides a utility for dynamically loading and unloading your driver, which replaces the slower manual process using Windows' Device Manager (which can still be used for the device INF). This utility is provided in two forms: wdreg and wdreg_gui. Both versions can be found in the WinDriver\util directory, can be run from the command line, and provide the same functionality. The difference is that wdreg_gui displays installation messages graphically, while wdreg displays them in console mode.

This section describes the use of wdreg/ wdreg_gui on Windows operating systems.

[Note]
  1. wdreg is dependent on the Driver Install Frameworks API (DIFxAPI) DLL – difxapi.dll, unless when run with the -compat option (described below). difxapi.dll is provided under the WinDriver\util directory.
  2. The explanations and examples below refer to wdreg, but any references to wdreg can be replaced with wdreg_gui.

13.2.2.1. WDM Drivers

This section explains how to use the wdreg utility to install the WDM windrvr6.sys driver on Windows, or to install INF files that register Plug-and-Play devices (such as PCI or PCMCIA) to work with this driver on Windows 7/Vista/Server 2008/Server 2003/XP/2000.

[Note]
You can rename the windrvr6.sys kernel module and modify your device INF file to register with your renamed driver, as explained in Section 15.2.1. To install your modified INF files using wdreg, simply replace any references to windrvr6 below with the name of your new driver.
[Note]
This section is not relevant for Kernel PlugIn drivers, since these are not WDM drivers and are not installed via an INF file. For an explanation on how to use wdreg to install Kernel PlugIn drivers on Windows 7 / Vista / Server 2008 / Server 2003 / XP / 2000, refer to Section 13.2.2.2.

Usage: The wdreg utility can be used in two ways as demonstrated below:

  1. wdreg -inf <filename> [-silent] [-log <logfile>]
    [install | preinstall | uninstall | enable | disable]
  2. wdreg -rescan <enumerator> [-silent] [-log <logfile>]

  • OPTIONS
    wdreg supports several basic OPTIONS from which you can choose one, some, or none:
    • -inf – The path of the INF file to be dynamically installed.
    • -rescan <enumerator> – Rescan enumerator (ROOT, ACPI, PCI, etc.) for hardware changes. Only one enumerator can be specified.
    • -silent – Suppress display of all messages (optional).
    • -log <logfile> – Log all messages to the specified file (optional).
    • -compat – Use the traditional SetupDi API instead of the newer Driver Install Frameworks API (DIFxAPI).
  • ACTIONS
    wdreg supports several basic ACTIONS:

    • install – Installs the INF file, copies the relevant files to their target locations, and dynamically loads the driver specified in the INF file name by replacing the older version (if needed).
    • preinstall Pre-installs the INF file for a non-present device.
    • uninstall – Removes your driver from the registry so that it will not load on next boot (see note below).
    • enable – Enables your driver.
    • disable – Disables your driver, i.e., dynamically unloads it, but the driver will reload after system boot (see note below).

[Note]
To successfully disable/uninstall your driver, make sure that there are no open handles to the WinDriver service (windrvr6.sys or your renamed driver (refer to Section 15.2), and that there are no connected and enabled Plug-and-Play devices that are registered with this service.

13.2.2.2. Non-WDM Drivers

This section explains how to use the wdreg utility to install non-WDM drivers, namely Kernel PlugIn drivers, on Windows 7/Vista/Server 2008/Server 2003/XP/2000.

Usage:

wdreg [-file <filename>] [-name <drivername>] [-startup <level>] [-silent] [-log <logfile>] Action [Action ...]

  • OPTIONS
    wdreg supports several basic OPTIONS from which you can choose one, some, or none:
    • -startup: Specifies when to start the driver. Requires one of the following arguments:
      • boot: Indicates a driver started by the operating system loader, and should only be used for drivers that are essential to loading the OS (for example, Atdisk).
      • system: Indicates a driver started during OS initialization.
      • automatic: Indicates a driver started by the Service Control Manager during system startup.
      • demand: Indicates a driver started by the Service Control Manager on demand (i.e., when your device is plugged in).
      • disabled: Indicates a driver that cannot be started.
      [Note]
      The default setting for the -startup option is automatic.
    • -name – Sets the symbolic name of the driver. This name is used by the user-mode application to get a handle to the driver. You must provide the driver's symbolic name (without the *.sys extension) as an argument with this option. The argument should be equivalent to the driver name as set in the KP_Init [B.6.1] function of your Kernel PlugIn project: strcpy(kpInit->cDriverName, XX_DRIVER_NAME) .
    • -filewdreg allows you to install your driver in the registry under a different name than the physical file name. This option sets the file name of the driver. You must provide the driver's file name (without the *.sys extension) as an argument.
      wdreg looks for the driver in the Windows installation directory
      (%windir%\system32\drivers). Therefore, you should verify that the driver file is located in the correct directory before attempting to install the driver.

      Usage:
      wdreg -name <Your new driver name> -file <Your original driver name> install

    • -silent – Suppresses the display of messages of any kind.
    • -log <logfile> – Logs all messages to the specified file.
  • ACTIONS
    wdreg supports several basic ACTIONS:
    • create – Instructs Windows to load your driver next time it boots, by adding your driver to the registry.
    • delete – Removes your driver from the registry so that it will not load on next boot.
    • start – Dynamically loads your driver into memory for use. You must create your driver before starting it.
    • stop – Dynamically unloads your driver from memory.
  • Shortcuts
    wdreg supports a few shortcut operations for your convenience:
    • install – Creates and starts your driver.
      This is the same as first using the wdreg stop action (if a version of the driver is currently loaded) or the wdreg create action (if no version of the driver is currently loaded), and then the wdreg start action.
    • preinstall – Creates and starts your driver for a non-connected device.
    • uninstall – Unloads your driver from memory and removes it from the registry so that it will not load on next boot.
      This is the same as first using the wdreg stop action and then the wdreg delete action.

13.2.3. Dynamically Loading/Unloading windrvr6.sys INF Files

When using WinDriver, you develop a user-mode application that controls and accesses your hardware by using the generic windrvr6.sys driver (WinDriver's kernel module). Therefore, you might want to dynamically load and unload the driver windrvr6.sys – which you can do using wdreg.
In addition, in WDM-compatible operating systems, you also need to dynamically load INF files for your Plug-and-Play devices. wdreg enables you to do so automatically on Windows 7/Vista/Server 2008/Server 2003/XP/2000.
This section includes wdreg usage examples, which are based on the detailed description of wdreg contained in the previous section. Examples:

  • To start windrvr6.sys on Windows 7/Vista/Server 2008/Server 2003/XP/2000:
    wdreg -inf <path to windrvr6.inf> install
    This command loads windrvr6.inf and starts the windrvr6.sys service.
  • To load an INF file named device.inf, located in the c:\tmp directory:
    wdreg -inf c:\tmp\device.inf install

    You can replace the install option in the example above with preinstall to pre-install the device INF file for a device that is not currently connected to the PC.

[Note]
If the installation fails with an ERROR_FILE_NOT_FOUND error, inspect the Windows registry to see if the RunOnce key exists in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion. This registry key is required by Windows Plug-and-Play in order to properly install drivers using INF files. If the RunOnce key is missing, create it; then try installing the INF file again.

To unload the driver/INF file, use the same commands, but simply replace install in the examples above with uninstall.

13.2.4. Dynamically Loading/Unloading Your Kernel PlugIn Driver

If you have used WinDriver to develop a Kernel PlugIn driver, you must load your Kernel PlugIn after loading the WinDriver generic driver windrvr6.sys.
When uninstalling your driver, you should unload your Kernel PlugIn driver before unloading windrvr6.sys.

[Note]
Kernel PlugIn drivers are dynamically loadable, and thus do not require a reboot in order to load.

To load/unload your Kernel PlugIn driver (<Your driver name>.sys) use the wdreg command as described above for windrvr6, with the addition of the 'name' flag, after which you must add the name of your Kernel PlugIn driver.

[Note]
You should not add the *.sys extension to the driver name.

Examples:

  • To load a Kernel PlugIn driver called KPDriver.sys, run this command:
    wdreg -name KPDriver install
  • To load a Kernel PlugIn driver called MPEG_Encoder, with file name MPEGENC.sys, run this command:
    wdreg -name MPEG_Encoder -file MPEGENC install
  • To uninstall a Kernel PlugIn driver called KPDriver.sys, run this command:
    wdreg -name KPDriver uninstall
  • To uninstall a Kernel PlugIn driver called MPEG_Encoder, with file name MPEGENC.sys, run this command:
    wdreg -name MPEG_Encoder -file MPEGENC uninstall