在Windows上,Windows DDK提供的开发环境是基于命令行的,操作起来极为不便,而Visual Studio 6.0给我们提供了非常友好易用的集成环境,让我们有如虎添翼之感。
那么,能否利用Visual Studio的集成环境来开发驱动程序呢?答案是可以的。通过对Visual Studio集成环境的简单设置,创建好自己的驱动开发集成环境就可以了。
1,
第一:安装Vc++6.0,我装的是英文版,中文版应该也可以,不过我没试。
第二:安装winXP DDK,注意,安装目录中间不能有空格,比如D:\Program Files不行,
我直接装在了D盘,装完后设置环境变量DDKROOT为安装目录D:\WINDDK\2505。
2,创建一个目录,作为开发目录,我是利用<<PCI设备开发宝典>>的光盘中的工程文件PCI9052Demo,直接考到E盘,所以,工作目录下是E:\PCI9052Demo
3,工作目录下创建一个批处理文件MakeDrvr.bat,内容如下:
@echo off
if "%1"=="" goto usage
if "%3"=="" goto usage
if not exist %1\bin\setenv.bat goto usage
call %1\bin\setenv %1 %4
%2
cd %3
build -b -w %5 %6 %7 %8 %9
goto exit
:usage
echo usage   MakeDrvr DDK_dir Driver_Drive Driver_Dir free/checked [build_options]
echo eg      MakeDrvr %%DDKROOT%% F: %%WDMWorkshop%% free -cef
:exit
解释以下:
1% 是DDK_dir,也就是ddk的安装目录
2% 是Driver_Drive,是你工作目录所在的盘符,这里是E:
3% 是Driver_Dir,是你工作目录的路径,这里是E:\PCI9052Demo
4% 是编译模式,checked表示调试模式,free表示发行模式,这里是出问题的地方,后面再说。
该批处理首先对传递的参数作一些检查,然后调用ddk的setenv命令设置环境变量,然后改变目录为源程序所在驱动器和目录,并最后调用build,-b保证显示完全的错误信息,-w保证在屏幕上输出警告,在vc ide里的output窗口中可以看到这些错误和警告。
4,建立一个空白工程
选File的new菜单项,然后选project栏的makefile,然后输入路径,一路next下去即可,visual studio提供两种配置win32 debug和win32 release. 按照cant的《windows wdm 设备驱动程序开发指南》方法,可以删除掉,添加Win32 Checked和 Win32 Free
5,VC的Project->settings里面改变设置
修改这两种配置
=============================================================
Free
=============================================================
build command line:
e:\PCI9052Demo \MakeDrvr %DDKROOT% e: e:\PCI9052Demo free

rebuild all opinions:
-nmake /a
output filename:
PCI9052Demo.sys
browse info file name:
objfre\i386\PCI9052Demo.bsc

=============================================================
Checked
=============================================================
build command line:
e:\PCI9052Demo\ MakeDrvr % DDKROOT% e: e:\PCI9052Demo checked

rebuild all opinions:
-nmake /a
output filename:
PCI9052Demo.sys
browse info file name:
objfre\i386\PCI9052Demo.bsc
6.添加源文件到工程
可以新建,也可以添加,这和普通的win32开发一样。

7.添加资源文件
选INSERT的RESOURCE菜单项即可

8.把文件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

9.把文件Sources放入源程序目录,内容为
TARGETNAME=RamDrive//这是要生成的驱动程序.sys文件的名字
TARGETPATH=obj //.sys文件所在目录的上层目录,(由于ddk的bug)应手工在obj目录下创建checked和free目录,以作为.sys的最终存放目录
TARGETTYPE=DRIVER //驱动程序的类型,一般不变
INCLUDES=$(BASEDIR)\\inc //ddk包含文件路径,一般不变
SOURCES=RamDrive.cpp RamDrive.rc //源文件(不要头文件),资源文件
BROWSER_INFO = 1 //若想要浏览信息,则要有本行;否则可无
这样设置完之后,按编译会出现错误的,错误如下:
usage: setenv <directory> [fre|chk] [64] [hal]
Example: setenv d:\ddk chk        set checked environment
Example: setenv d:\ddk            set free environment (default)
Example: setenv d:\ddk fre 64     sets 64 bit free environment
Example: setenv d:\ddk hal        sets free hal environment
Example: setenv d:\ddk fre hal    Same
'build' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
PCI9052Demo.sys - 0 error(s), 0 warning(s)
问题出在什么地方呢?关键在于XP的DDK的编译模式已经不再是free和checked了,而是上面的fre和chk,另外还加了fre 64,hal, fre hal。
解决办法就是:VC的Project->settings里面设置的编译模式改成fre和chk,然后,输出就正常了,
--------------------Configuration: PCI9052Demo - Win32 fre--------------------
BUILD: Object root set to: ==> objfre
BUILD: Adding /Y to COPYCMD so xcopy ops won't hang.
BUILD: /i switch ignored
BUILD: Compile and Link for i386
BUILD: Loading D:\WINDDK\2505\build.dat...
BUILD: Computing Include file dependencies:
BUILD: e:\pci9052demo\sources. unexpected in directory with DIRS file
BUILD: Ignoring e:\pci9052demo\sources.
BUILD: pci9052demo found in dirs., is not a subdirectory of e:\pci9052demo
BUILD: Examining e:\pci9052demo directory tree for files to compile.
    e:\pci9052demo
BUILD: Linking e:\pci9052demo directory
BUILD: Done
PCI9052Demo.sys - 0 error(s), 0 warning(s)