Http://www.yesky.com/20020819/1625888.shtml
After designing and developing our own WDM driver, we must compile and install them to run the driver.
How to compile the device driver
After DDK is installed, there are check and free compiling environments in the DDK program group. The check environment is used to compile the driver with debugging information, and free is the environment for compiling the officially released version. Generally, the compilation of the device driver uses the command line method. Some settings can be used to compile in the integration environment of VC ++.
Generally, it takes four files to compile the most basic device driver. The first one is the driver, that is, the C language source program file (such as vdisk. c. Note that all the examples below are described in vdisk. The second is the RC file (for example, vdisk. RC); the third is the sources file; the fourth is the makefile. RC file. The sources file is similar to the Make file, which is used to specify the files to be compiled and the library files to be connected. These three auxiliary files are very simple. In every routine of DDK samples, there are three such files, and they can be understood in the form of samples.
1. Example Analysis
The following uses the vdisk program as an example. The vdisk. RC code is as follows:
/Vdisk. RC/ # Include # Include # Define ver_filetype vft_drv # Define ver_filesubtype vft2_drv_system # Define ver_filedescription_str "SCSI vdisk driver" # Define ver_internalname_str "vdisk. sys" # Define ver_originalfilename_str "vdisk. sys" # Include "common. Ver" /End of vdisk. RC/ |
The build utility is generally used for device drivers.NmakeAn external packaging program. Build itself is actually quite simple. Most of the compilation work is actually passed to buildNmake.
/Sources/ Targetname = vdisk Targettype = driver Targetpath = $ (basedir) \ Lib Targetlibs = $ (basedir) \ Lib \\$ (ddkbuildenv) \ scsiport. Lib Supported des =... \. \ Inc Sources = vdisk. c vdisk. RC /End of sources/ Note that the sources file name does not have any extension. # Makefile # # Do not edit this file !!! Edit. \ sources. If you want to add a new source # File to this component. This file merely indirects to the real make File # That is shared by all the driver components of the Windows NT DDK # ! Include $ (ntmakeenv) \ makefile. Def # End of makefile |
Makefile is the same for all drivers, and Microsoft also warns against editing this file. If necessary, you can edit and modify the sources file to achieve the same effect. For device drivers, the C compiler used basically selects VC ++ without exception.
2. Basic compilation steps
(1) first enter the check or free compiling environment and initialize the DDK compiling environment.
(2) Run vcvars32.bat in the bin directory under the VC installation directory to initialize the VC ++ compiling environment.
(32.16run build.exe for compilation.
■ Install and start the device driver
1. Add a key value in the Registry
During Windows NT boot, the driver list is constructed by scanning the registry. This list includes both self-started drivers and drivers to be manually started. This list is actually all the devices listed in the device Applet in the control panel. All device drivers should be in the HKEY_LOCAL_MACHINE \ SYSTEM \ currentcontrol-
Set \ Services \ has the corresponding key value. The following uses vdisk as an example to describe how to add a key value:
First, add a sub-item vdisk under HKEY_LOCAL_MACHINE \ SYSTEM \ current ControlSet \ Services \. Note that the name here should be consistent with your driver name. For example, if the driver name is vdisk. sys, the sub-item name here is vdisk. Then add the following key values under vdisk:
Name |
Data Type |
Description |
Type |
REG_DWORD |
Driver types |
Start |
REG_DWORD |
Start Time of the driver |
Errorcontrol |
REG_DWORD |
Handle failed driver loading errors |
Group |
REG_SZ |
Driver group name |
Dependongroup |
Reg_multi_sz |
Other drivers |
Tag |
REG_BINARY |
Loading Sequence of drivers in the same group |
Parameters |
(Key) |
Driver-specific parameter keys |
1 indicates the kernel-mode driver; 2 indicates the file system driver.
The value of errorcontrol is 0, indicating logging errors and ignoring them. The value of 1 indicates logging errors and a dialog box is displayed. The value of 2 indicates logging errors and restarts with the correct configuration; A value of 3 indicates that the log is incorrect. If the correct configuration has been used, an error is returned.
In any device driver, the first three parameters in the preceding table are required.
2. Control the loading sequence of the driver.
Sometimes it is necessary to control the Loading Order of multiple drivers. For example, a set of drivers contains three drivers: jbchanger. sys, changerdisk. sys, and vdisk. sys. Jbchanger and changerdisk are two scsi drivers, both of which depend on the SCSI small port (mini port driver). At the same time, changerdisk must be started after jbchanger is started. Vdisk is a virtual disk driver. It can be started successfully only after both jbchanger and changerdisk are started.
3. Start value of the driver
The start value of the driver in the above registry controls the start time of the driver in the system. Currently, start can take the following values, and leave room for expansion for this value to apply to new requirements:
(L) 0x0 (service_boot_start): This value specifies that the driver should be started by the operating system loader. The general driver does not use this value, because the system is barely started at this time, and most systems are not available yet.
(2) 0x1 (service_system_start): The value indicates that the driver is started after the operating system is loaded but initialized.
(3) 0x2 (service_auto_start): This value indicates that it is loaded by the Service Control Manager after the entire system is started and run.
(4) 0x3 (service_demand_start): The value indicates that the driver must be started manually. You can use the control panel device applet or Win32 API programming to start the program.
(5) 0x4 (service_disabled): indicates that the driver is disabled.
Note that when debugging the driver, it is best to set the start value to 3 for manual start, because if it is set to automatic start, if an exception occurs during the startup of the driver, the system may fail to start.
If you do not need to restore the disk urgently, You can first try to start the system with the known configuration at startup to check whether the system can be started successfully. If the driver fails, you can use DOS to delete the problematic driver in the \ % SystemRoot % \ system32 \ DRIVERS directory, and then the system can start.
However, if NT is installed in the NTFS partition, the partition will not be visible after DOS is started, so that the hard disk must be mounted to another NT System to delete the file. You can set start to control the startup of the driver at different times. To solve the dependency problem, use the group and dependongroup values.
First, determine the group name used by your driver. The system has some defined group names. For the group names existing in the current system, observe the \ HKEY_LOCAL_MACHINE \ SYSTEM \ currentcontrol-
Set \ Control \ servicegrouporder \ list key value. For example, this value can be set:
... SCSI miniport Port Primary Disk SCSI class Scsi cdrom class Filter Boot File System ... |
Each row is a group name. Generally, a driver belongs to a group. When the system starts, the drivers in each group are started according to the order of the groups in the list. For example, both jbchanger and changerdisk belong to the SCSI class group. If you think the group names in the table are not suitable, you can add a new group name in the appropriate position of the list.
The dependongroup value must start another group of drivers when controlling the startup of the driver. For example, the startup of jbchanger and changerdisk depends on the SCSI miniport group. Therefore, the dependongroup values of jbchanger and changerdisk are both SCSI miniport.
4. Modify the Registry
You can manually modify these values in the registry, or use the Win32 API to add them. You can also use the INI file to add these values. The following is an example of an INI file named vdisk. ini.
\ REGISTRY \ MACHINE \ System \ currentcontrol Set \ Services \ vdisk Type = REG_DWORD 0x00000001 Start = REG_DWORD 0x00000003 Errorcontrol = REG_DWORD 0x00000001 Group = SCSI class Parameters Driveletter = N: |
Run regini. EXE with vdisk. ini as the parameter. The corresponding items are automatically added to the Registry.
After these items are added to the Registry, you must restart the system so that the added device driver can be listed in the device Applet of the control panel and then perform other operations.
5. Start the device driver.
After adding and modifying the registry, restart the system. If the selected Start values are 0, 1, and 2, if everything is normal, the driver should have been started. You can view the device list in the device Applet of the control panel. If start is set to 3, it can be started directly.
6. debugging tools
Currently, only windbg and SoftICE are debugging tools for the NT driver. The use of windbg requires a dual-host environment. SoftICE is strongly recommended. Pay attention to the current setup of SoftICE 3.2 for NT on the FTP server in China. the INS file is incorrect. It will cause the installer to not know your NT. You can use the setup in 3.0. the INS file replaces the setup of 3.2. INS.