博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Windows驱动开发技术详解》读书笔记(二)
阅读量:5906 次
发布时间:2019-06-19

本文共 4689 字,大约阅读时间需要 15 分钟。

  再来看基于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编译,准备两个脚本文件,makefile和Sources,其中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,D /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工具来进行安装,如图所示:
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2009/02/20/1395023.html,如需转载请自行联系原作者
你可能感兴趣的文章
使用ASP.NET Atlas SortBehavior实现客户端排序
查看>>
图像滤镜处理算法:灰度、黑白、底片、浮雕
查看>>
Office文档出错的几种原因与解决方法
查看>>
正则表达式 学习笔记1.1
查看>>
AssetBundle进阶内存优化(Unity 4.x)
查看>>
《从零开始学Swift》学习笔记(Day 40)——析构函数
查看>>
Exchange2003-2010迁移系列之十,Exchange证书攻略
查看>>
extmail集群的邮件负载均衡方案 [lvs dns postfix]
查看>>
更改UIView的背景
查看>>
APUE第15章学习扎记之程序的存储区布局试验
查看>>
ubuntu升级16.04 inter idea 中文输入法无效
查看>>
三目运算判断jsp脚本里面的值
查看>>
sshtunnel在本地访问云服务器mysql
查看>>
小蚂蚁学习APP接口开发(1)—— json方式封装通信接口
查看>>
我的友情链接
查看>>
CDN相关
查看>>
Tomcat的设置4——Tomcat的体系结构与设置基于端口号的虚拟主机
查看>>
我的友情链接
查看>>
ftp协议基础
查看>>
访问共享经常中断
查看>>