《Windows驱动开发技术详解》读书笔记(二)

        再来看基于WDM的驱动程序,

#ifdef __cplusplus
extern   " C "
{
#endif
#include 
< wdm.h >
#ifdef __cplusplus
}
#endif  

typedef 
struct  _DEVICE_EXTENSION
{
    PDEVICE_OBJECT fdo;
    PDEVICE_OBJECT NextStackDevice;
    UNICODE_STRING ustrDeviceName;    
//  设备名
    UNICODE_STRING ustrSymLinkName;     //  符号链接名
} DEVICE_EXTENSION,  * PDEVICE_EXTENSION;

#define  PAGEDCODE code_seg("PAGE")
#define  LOCKEDCODE code_seg()
#define  INITCODE code_seg("INIT")

#define  PAGEDDATA data_seg("PAGE")
#define  LOCKEDDATA data_seg()
#define  INITDATA data_seg("INIT")

#define  arraysize(p) (sizeof(p)/sizeof((p)[0]))

NTSTATUS HelloWDMAddDevice(IN PDRIVER_OBJECT DriverObject,
                           IN PDEVICE_OBJECT PhysicalDeviceObject);
NTSTATUS HelloWDMPnp(IN PDEVICE_OBJECT fdo,
                        IN PIRP Irp);
NTSTATUS HelloWDMDispatchRoutine(IN PDEVICE_OBJECT fdo,
                                 IN PIRP Irp);
void  HelloWDMUnload(IN PDRIVER_OBJECT DriverObject);

extern   " C "
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,
                     IN PUNICODE_STRING RegistryPath);

 

HelloWDM.cpp

      同前面一样,编译也是两种方式,第一种用DDK编译,准备两个脚本文件,makefileSources,其中Sources有所不同,如下所示:

TARGETNAME = HelloWDM
TARGETTYPE
= DRIVER
DRIVERTYPE
= WDM
TARGETPATH
= OBJ

INCLUDES
=$( BASEDIR )\ inc ;\
         
$( BASEDIR )\ inc \ ddk ;\

SOURCES
= HelloWDM . cpp \

        另一种是用VC进行编译,有三点需要注意:

1, 选择c/c++选项卡,将原来的Project Options全删掉,换成下面的:

/ nologo  / Gz  / MLd  / W3  / WX  / Z7  / Od  / D WIN32 = 100   / D _X86_ = 1   / D WINVER = 0x500   / D DBG = 1   / Fo " MyDriver_Check/ "   / Fd " MyDriver_Check/ "   / FD  / c

2, 选择Link选项卡,将原来的Project Options全删掉,换成下面的:

wdm . lib  / nologo  / base: " 0x10000 "   / stack: 0x400000 , 0x1000   / entry: " DriverEntry "   / subsystem :console   / incremental :no   / pdb: " MyDriver_Check/HelloWDM.pdb "   / debug   / machine :I386   / nodefaultlib  / out: " MyDriver_Check/HelloWDM.sys "   / pdbtype :sept   / subsystem :native   / driver  / SECTION :INIT , / RELEASE  / IGNORE: 4078

3, 修改include目录时,加入

 

D: \ WINDDK \ 3790.1830 \ INC \ DDK \ WDM \ W2K

  否则会报错如下:

fatal error C1083: Cannot open include file: 'wdm . h': No such file or directory

 

   最后是驱动的安装,WDM驱动的安装需要写一个inf文件,如下所示:

; --------- Version Section ---------------------------------------------------

[Version]
Signature
= " $CHICAGO$ " ;
Provider
= Phinecos_Device
DriverVer
= 20 / 2 / 2000 , 3.0 . 0.3

;   If   device  fits one of the standard classes ,   use  the name and GUID here ,
;  otherwise create your own  device  class and GUID as this example shows .

Class
= PhinecosDevice
ClassGUID
= {EF2962F0-0D55-4bff-B8AA-2221EE8A79B0}


; --------- SourceDiskNames and SourceDiskFiles Section -----------------------

;  These sections identify source disks and  files   for  installation .  They are
;  shown here as an example ,  but commented out .

[SourceDisksNames]
1   =   " HelloWDM " , Disk1 ,,

[SourceDisksFiles]
HelloWDM
. sys  =   1 , MyDriver_Check ,

; --------- ClassInstall / ClassInstall32 Section -------------------------------

;   Not  necessary  if  using a standard class

;  9X Style
[ClassInstall]
Addreg
= Class_AddReg

;  NT Style
[ClassInstall32]
Addreg
= Class_AddReg

[Class_AddReg]
HKR
,,,, %DeviceClassName%
HKR
,, Icon ,, " -5 "

; --------- DestinationDirs Section -------------------------------------------

[DestinationDirs]
YouMark_Files_Driver 
=   10 , System32 \ Drivers

; --------- Manufacturer and Models Sections ----------------------------------

[Manufacturer]
%MfgName%
= Mfg0

[Mfg0]

;  PCI hardware Ids  use  the form
;  PCI \ VEN_aaaa&DEV_bbbb&SUBSYS_cccccccc&REV_dd
; 改成你自己的ID
%DeviceDesc%
= YouMark_DDI ,  PCI \ VEN_9999&DEV_9999

; ---------- DDInstall Sections -----------------------------------------------
;  --------- Windows 9X -----------------

;  Experimentation has shown that DDInstall root names greater than  19  characters
;  cause problems in Windows  98

[YouMark_DDI]
CopyFiles
= YouMark_Files_Driver
AddReg
= YouMark_9X_AddReg

[YouMark_9X_AddReg]
HKR
,, DevLoader ,, *ntkern
HKR
,, NTMPDriver ,, HelloWDM . sys
HKR
,   " Parameters " ,   " BreakOnEntry " ,   0x00010001 ,   0

;  --------- Windows NT -----------------

[YouMark_DDI
. NT]
CopyFiles
= YouMark_Files_Driver
AddReg
= YouMark_NT_AddReg

[YouMark_DDI
. NT . Services]
Addservice 
=  HelloWDM ,   0x00000002 ,  YouMark_AddService

[YouMark_AddService]
DisplayName 
=  %SvcDesc%
ServiceType 
=   1   ;  SERVICE_KERNEL_DRIVER
StartType 
=   3   ;  SERVICE_DEMAND_START
ErrorControl 
=   1   ;  SERVICE_ERROR_NORMAL
ServiceBinary 
=   %10 % \ System32 \ Drivers \ HelloWDM . sys

[YouMark_NT_AddReg]
HKLM
,   " System\CurrentControlSet\Services\HelloWDM\Parameters " ,\
" BreakOnEntry " ,   0x00010001 ,   0


;  ---------  Files   ( common )  -------------

[YouMark_Files_Driver]
HelloWDM
. sys

; --------- Strings Section ---------------------------------------------------

[Strings]
ProviderName
= " Phinecos. "
MfgName
= " Vista Soft "
DeviceDesc
= " Hello World WDM! "
DeviceClassName
= " Phinecos_Device "
SvcDesc
= " Phinecos "

    这里有两种安装方式,一种是进入控制面板,选择添加硬件,加载进inf文件完成安装,如图所示:

 

 

     另一种可选的安装测试方式是使用Driver Studio中的EzDriverInstaller工具来进行安装,如图所示:

你可能感兴趣的:(windows)