Build a WDM development environment in VC ++

Source: Internet
Author: User
Tags configuration settings

WDM (Win32 Driver Model), A Win32 driver model, is a brand new driver model pushed by Microsoft. It aims to simplify the development of the driver in a flexible way, reduce and reduce the number and complexity of necessary drivers on the basis of implementing support for new hardware.

At present, the main tool used for WDM driver development is the DDK (driver development kit) provided by Microsoft, which contains many kernel functions and data structure definitions; it contains many useful debugging, testing, and construction tools, as well as many driver instances and rich help documents. In addition, developers usually create and edit Device Driver projects in the integrated Visual Studio development environment.

After a program project is created in Visual Studio, the program executable file must be compiled and linked. However, because Visual Studio cannot be used to construct the kernel program, you often need to exit the editing environment and use the dedicated tool build provided in DDK to construct the device driver.

To improve the development efficiency of the driver program and simplify the construction process of the driver program, we can directly construct the WDM driver from Visual Studio by changing the settings.

This article will take a simple Windows Device Driver wdmtest as an example to introduce its creation and construction process.

System settings

1. Install DDK

Before writing drivers for WDM devices, you must install the Microsoft driver development kit DDK.

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.

2. environment variable settings

After the DDK is successfully installed, you need to set the environment variable for it.

First, set an environment variable named ddkroot, which points to the base Directory of Windows2000 DDK or Windows98 DDK.

Then, set an environment variable named wdmdir, which points to the base Directory of the driver.

On Windows 98, add these environment variables to autoexec. in the BAT file. on the Windows platform, set the environment variables in the "environment variables" tab of the "System" program on the control panel.

Composition of WDM drivers

A basic device driver should contain two parts: the driver source file and the driver constructor file.

Table 1 lists all source files used by wdmtest, and Table 2 lists all constructors used by wdmtest.

---- Source file of Table 1 wdmtest ----
Init. cpp access and uninstall code
Main IRP distribution routines of dispatch. cpp
PNP. cpp plug-and-play and code
Power. cpp Power Management Code
Wdmtest. RC version Resource
Wdmtest. h Driver header file
Guids. h guid Definition
IOCTL. h IOCTL Definition
Resource. h resource editor header file
----------------------------------------
----- Wdmtest: Construct a file -----
Sources build
Makefile standard makefile
Makedrvier makefile project batch file
----------------------------------------
Driver Construction

The DDK build command line utility is the main tool used to construct the driver. It uses the Correct Compiler and connector settings to construct the driver by calling the nmake utility. Build displays detailed information about the construction progress and error results to the standard output. Build itself is actually quite simple. Most of the compilation work is actually passed to nmake by build.
Because build uses the same compilation and Connection Tool as Visual Studio, You can activate build from Visual Studio. Therefore, you can set VC ++ to directly construct a WDM driver from Visual Studio. This will make the driver editing and construction in the same environment, making it easier to develop.

A makefile project is used to activate the build command line utility to construct the driver. All necessary construction files must be correctly set, including the sources file, makedrvier. bat, and makefile files.

1. Sources File

Build finds an nmake macro file named sources in the current directory to learn more about the driver structure. The sources file of the wdmtest project is as follows:

Targetname = wdmtest
Targettype = driver
Drivertype = WDM
Targetpath = sys
Supported des = $ (basedir)/INC;
Sources = init. CPP/
Dispatch. CPP/
PNP. CPP/
Power. CPP/
Wdmtest. RC

Targetname indicates that the program's target name is wdmtest. sys; targettype and drivertype indicate that the program is a WDM driver, targetpath indicates that the program is constructed in the SYS subdirectory, and des indicates that the DDK Inc directory is added to the search list of header files; the sources macro specifies the list of files to be compiled.

2. makefile

The MAKEFILE file activates the standard construction file makefile. Def in the DDK Inc directory. The MAKEFILE file of the wdmtest project is as follows:

! Include $ (ntmakeenv)/makefile. Def

3. makedrvier. BAT file

Makedrvier. bat is a batch processing file that calls the setenv command of DDK. The setenv. BAT file exists in the bin directory of the DDK. It is used to set the environment variables during the construction process and call other batch files in the bin directory.

Makedrvier. BAT must pass at least four parameters: DDK base Directory (% 1), source drive (% 2), source directory (% 3), and construction type (% 4) ("free" or "checked"), any other parameter (% 5 ......) Directly passed to build.

Makedrvier. bat first checks whether setenv. Bat exists, then calls the etenv command to correctly set the environment variable for the build target, changes the directory to the source drive and source directory, and finally calls build.

The screen output of the makedrvier command file appears in the Visual Studio output window.

The makedrvier. BAT file of the wdmtest project is as follows:

@ Echo off
If not exist % 1/bin/setenv. Bat goto warning
Call % 1/bin/setenv % 1% 4
% 2
Cd % 3
Build-B-w % 5% 6% 7% 9
Echo OK makedriver
Goto exit
: Warning % 1
Echo warning % 1/bin/setenv. BAT not exist
Goto exit
: Exit

4. makefile construction environment

When a new makefile project is created, Visual Studio provides two build configurations by default: "Win32 debug" and "Win32 release ". To correspond to the DDK configuration, we usually use the "compile-configuration" menu to delete the two configurations and add two new configurations: "Win32 checked" and "Win32 free ".
For the "Win32 free" configuration, set the project settings as shown in Table 3. For the Win32 checked configuration, change "free" to "checked" in the CREATE command line ".

----- Table 3 Win32 free configuration settings -----
CREATE command line makedriver % ddkroot % C: % wdmdir %/wdmtest/sys free
Rebuilding all options-nmake/
Output file name wdmtest. sys
Create a command line to run the makedrvier batch processing file and use the ddkroot and wdmdir environment variables.
----------------------------------------

5. Driver construction and compilation

After the driver is edited, run "construct wdmtest" in the "compile" menu in Visual C ++ to compile and construct the driver.

If an error occurs, double-click the error prompt to quickly return to the source code for modification. If no error occurs, the driver WDM test. sys is stored in the free or check constructor directory.

Conclusion

In summary, this article introduces how to construct a WDM driver by creating a makefile project and activating the "DDK build" command line utility in Visual Studio. This method enables us to directly construct a WDM driver from Visual Studio by changing the VC ++ settings, so as to improve the development efficiency of the driver and simplify the construction process of the driver.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.