我的项目环境是vs2013和wdk8.1. vs2013不需要任何的设置,编写好文件后,点击F7编译生成,就会自动生成一个inf文件和sys文件.
vs2013和WDK8.1可在微软官网下载,下载WDK8.1后安装,这时在vs2013的Vc中会出现:
我们选择WDM进行建立方案.方案名为WDMDriver.
HelloWDM项目的建立如下:
注意inf会自动生成一个,但是节不全,需要自己添加哦!
WDMDriver.inf文件代码如下:- <STRONG><SPAN style="FONT-SIZE: 18px">;; The Win2K DDK documentation contains an excellent INF reference.
- ;--------- Version Section ---------------------------------------------------
- [Version]
- Signature="$CHICAGO$"
- Provider=jinzihan
- DriverVer=12/12/2013,15.18.43.886
- CatalogFile=WDMDriver.cat;生成的inf中没有填写,此处需要自己添加:名字.cat.此处重点
- ; 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=QT;随便改个就行
- ClassGUID={EF2962F0-0D55-4bff-B8AA-2221EE8A79AB};随便改个就行,不要与已有的冲突class和calssguid都有个对应关系.网上可查
- ;--------- 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 = %DiskName%,,,""
- [SourceDisksFiles]
- WDMDriver.sys = 1,,
- ;--------- 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,,WDMDriver.sys
- HKR, "Parameters", "BreakOnEntry", 0x00010001, 0
- ; --------- Windows NT -----------------
- [YouMark_DDI.NT]
- CopyFiles=YouMark_Files_Driver
- AddReg=YouMark_NT_AddReg
- [YouMark_DDI.NT.Services]
- Addservice = WDMDriver, 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\WDMDriver.sys
- [YouMark_NT_AddReg]
- HKLM, "System\CurrentControlSet\Services\WDMDriver\Parameters",\
- "BreakOnEntry", 0x00010001, 0
- ; --------- Files (common) -------------
- [YouMark_Files_Driver]
- WDMDriver.sys
- ;--------- Strings Section ---------------------------------------------------
- [Strings]
- ProviderName="jinzihan"
- MfgName="Zhangfan Soft"
- DeviceDesc="QQ761153454";设备管理器中的显示的实例的名字.在类的下边显示
- DeviceClassName="QT218967017";此为显示在设备管理器中的类的名字.在实例的上边显示.修改一次类的名字,就需要更改一次classguid的值.
- SvcDesc="jinzihan"
- ;inf结束inf中没有的节,可以参照我的inf自己添加.</SPAN></STRONG>
;; The Win2K DDK documentation contains an excellent INF reference.
;--------- Version Section ---------------------------------------------------
[Version]
Signature="$CHICAGO$"
Provider=jinzihan
DriverVer=12/12/2013,15.18.43.886
CatalogFile=WDMDriver.cat;生成的inf中没有填写,此处需要自己添加:名字.cat.此处重点
; 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=QT;随便改个就行
ClassGUID={EF2962F0-0D55-4bff-B8AA-2221EE8A79AB};随便改个就行,不要与已有的冲突class和calssguid都有个对应关系.网上可查
;--------- 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 = %DiskName%,,,""
[SourceDisksFiles]
WDMDriver.sys = 1,,
;--------- 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,,WDMDriver.sys
HKR, "Parameters", "BreakOnEntry", 0x00010001, 0
; --------- Windows NT -----------------
[YouMark_DDI.NT]
CopyFiles=YouMark_Files_Driver
AddReg=YouMark_NT_AddReg
[YouMark_DDI.NT.Services]
Addservice = WDMDriver, 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\WDMDriver.sys
[YouMark_NT_AddReg]
HKLM, "System\CurrentControlSet\Services\WDMDriver\Parameters",\
"BreakOnEntry", 0x00010001, 0
; --------- Files (common) -------------
[YouMark_Files_Driver]
WDMDriver.sys
;--------- Strings Section ---------------------------------------------------
[Strings]
ProviderName="jinzihan"
MfgName="Zhangfan Soft"
DeviceDesc="QQ761153454";设备管理器中的显示的实例的名字.在类的下边显示
DeviceClassName="QT218967017";此为显示在设备管理器中的类的名字.在实例的上边显示.修改一次类的名字,就需要更改一次classguid的值.
SvcDesc="jinzihan"
;inf结束inf中没有的节,可以参照我的inf自己添加.
驱动主体HelloWDM.cpp文件:
- <STRONG><SPAN style="FONT-SIZE: 18px">/************************************************************************
- * 文件名称:HelloWDM.cpp
- * 作 者:张帆
- * 完成日期:2007-11-1
- *************************************************************************/
- #include "HelloWDM.h"
- /************************************************************************
- * 函数名称:DriverEntry
- * 功能描述:初始化驱动程序,定位和申请硬件资源,创建内核对象
- * 参数列表:
- pDriverObject:从I/O管理器中传进来的驱动对象
- pRegistryPath:驱动程序在注册表的中的路径
- * 返回 值:返回初始化驱动状态
- *************************************************************************/
- #pragma INITCODE
- extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject,
- IN PUNICODE_STRING pRegistryPath)
- {
- KdPrint(("jinzihan Enter DriverEntry\n"));
- pDriverObject->DriverExtension->AddDevice = HelloWDMAddDevice;
- pDriverObject->MajorFunction[IRP_MJ_PNP] = HelloWDMPnp;
- pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =
- pDriverObject->MajorFunction[IRP_MJ_CREATE] =
- pDriverObject->MajorFunction[IRP_MJ_READ] =
- pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloWDMDispatchRoutine;
- pDriverObject->DriverUnload = HelloWDMUnload;
- KdPrint(("Leave DriverEntry\n"));
- return STATUS_SUCCESS;
- }
- /************************************************************************
- * 函数名称:HelloWDMAddDevice
- * 功能描述:添加新设备
- * 参数列表:
- DriverObject:从I/O管理器中传进来的驱动对象
- PhysicalDeviceObject:从I/O管理器中传进来的物理设备对象
- * 返回 值:返回添加新设备状态
- *************************************************************************/
- #pragma PAGEDCODE
- NTSTATUS HelloWDMAddDevice(IN PDRIVER_OBJECT DriverObject,
- IN PDEVICE_OBJECT PhysicalDeviceObject)
- {
- PAGED_CODE();
- KdPrint(("Enter HelloWDMAddDevice123\n"));
- NTSTATUS status;
- PDEVICE_OBJECT fdo;
- UNICODE_STRING devName;
- RtlInitUnicodeString(&devName, L"\\Device\\MyWDMDevice");
- status = IoCreateDevice(
- DriverObject,
- sizeof(DEVICE_EXTENSION),
- &(UNICODE_STRING)devName,
- FILE_DEVICE_UNKNOWN,
- 0,
- FALSE,
- &fdo);
- if (!NT_SUCCESS(status))
- return status;
- PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)fdo->DeviceExtension;
- pdx->fdo = fdo;
- pdx->NextStackDevice = IoAttachDeviceToDeviceStack(fdo, PhysicalDeviceObject);
- UNICODE_STRING symLinkName;
- RtlInitUnicodeString(&symLinkName, L"\\DosDevices\\HelloWDM");
- pdx->ustrDeviceName = devName;
- pdx->ustrSymLinkName = symLinkName;
- status = IoCreateSymbolicLink(&(UNICODE_STRING)symLinkName, &(UNICODE_STRING)devName);
- if (!NT_SUCCESS(status))
- {
- IoDeleteSymbolicLink(&pdx->ustrSymLinkName);
- status = IoCreateSymbolicLink(&symLinkName, &devName);
- if (!NT_SUCCESS(status))
- {
- return status;
- }
- }
- fdo->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE;
- fdo->Flags &= ~DO_DEVICE_INITIALIZING;
- KdPrint(("Leave HelloWDMAddDevice\n"));
- return STATUS_SUCCESS;
- }
- /************************************************************************
- * 函数名称:DefaultPnpHandler
- * 功能描述:对PNP IRP进行缺省处理
- * 参数列表:
- pdx:设备对象的扩展
- Irp:从IO请求包
- * 返回 值:返回状态
- *************************************************************************/
- #pragma PAGEDCODE
- NTSTATUS DefaultPnpHandler(PDEVICE_EXTENSION pdx, PIRP Irp)
- {
- PAGED_CODE();
- KdPrint(("Enter DefaultPnpHandler\n"));
- IoSkipCurrentIrpStackLocation(Irp);
- KdPrint(("Leave DefaultPnpHandler\n"));
- return IoCallDriver(pdx->NextStackDevice, Irp);
- }
- /************************************************************************
- * 函数名称:HandleRemoveDevice
- * 功能描述:对IRP_MN_REMOVE_DEVICE IRP进行处理
- * 参数列表:
- fdo:功能设备对象
- Irp:从IO请求包
- * 返回 值:返回状态
- *************************************************************************/
- #pragma PAGEDCODE
- NTSTATUS HandleRemoveDevice(PDEVICE_EXTENSION pdx, PIRP Irp)
- {
- PAGED_CODE();
- KdPrint(("Enter HandleRemoveDevice\n"));
- Irp->IoStatus.Status = STATUS_SUCCESS;
- NTSTATUS status = DefaultPnpHandler(pdx, Irp);
- IoDeleteSymbolicLink(&(UNICODE_STRING)pdx->ustrSymLinkName);
- //调用IoDetachDevice()把fdo从设备栈中脱开:
- if (pdx->NextStackDevice)
- IoDetachDevice(pdx->NextStackDevice);
- //删除fdo:
- IoDeleteDevice(pdx->fdo);
- KdPrint(("Leave HandleRemoveDevice\n"));
- return status;
- }
- /************************************************************************
- * 函数名称:HelloWDMPnp
- * 功能描述:对即插即用IRP进行处理
- * 参数列表:
- fdo:功能设备对象
- Irp:从IO请求包
- * 返回 值:返回状态
- *************************************************************************/
- #pragma PAGEDCODE
- NTSTATUS HelloWDMPnp(IN PDEVICE_OBJECT fdo,
- IN PIRP Irp)
- {
- PAGED_CODE();
- KdPrint(("Enter HelloWDMPnp\n"));
- NTSTATUS status = STATUS_SUCCESS;
- PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)fdo->DeviceExtension;
- PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
- static NTSTATUS(*fcntab[])(PDEVICE_EXTENSION pdx, PIRP Irp) =
- {
- DefaultPnpHandler, // IRP_MN_START_DEVICE
- DefaultPnpHandler, // IRP_MN_QUERY_REMOVE_DEVICE
- HandleRemoveDevice, // IRP_MN_REMOVE_DEVICE
- DefaultPnpHandler, // IRP_MN_CANCEL_REMOVE_DEVICE
- DefaultPnpHandler, // IRP_MN_STOP_DEVICE
- DefaultPnpHandler, // IRP_MN_QUERY_STOP_DEVICE
- DefaultPnpHandler, // IRP_MN_CANCEL_STOP_DEVICE
- DefaultPnpHandler, // IRP_MN_QUERY_DEVICE_RELATIONS
- DefaultPnpHandler, // IRP_MN_QUERY_INTERFACE
- DefaultPnpHandler, // IRP_MN_QUERY_CAPABILITIES
- DefaultPnpHandler, // IRP_MN_QUERY_RESOURCES
- DefaultPnpHandler, // IRP_MN_QUERY_RESOURCE_REQUIREMENTS
- DefaultPnpHandler, // IRP_MN_QUERY_DEVICE_TEXT
- DefaultPnpHandler, // IRP_MN_FILTER_RESOURCE_REQUIREMENTS
- DefaultPnpHandler, //
- DefaultPnpHandler, // IRP_MN_READ_CONFIG
- DefaultPnpHandler, // IRP_MN_WRITE_CONFIG
- DefaultPnpHandler, // IRP_MN_EJECT
- DefaultPnpHandler, // IRP_MN_SET_LOCK
- DefaultPnpHandler, // IRP_MN_QUERY_ID
- DefaultPnpHandler, // IRP_MN_QUERY_PNP_DEVICE_STATE
- DefaultPnpHandler, // IRP_MN_QUERY_BUS_INFORMATION
- DefaultPnpHandler, // IRP_MN_DEVICE_USAGE_NOTIFICATION
- DefaultPnpHandler, // IRP_MN_SURPRISE_REMOVAL
- };
- ULONG fcn = stack->MinorFunction;
- if (fcn >= arraysize(fcntab))
- { // unknown function
- status = DefaultPnpHandler(pdx, Irp); // some function we don't know about
- return status;
- } // unknown function
- #if DBG
- static char* fcnname[] =
- {
- "IRP_MN_START_DEVICE",
- "IRP_MN_QUERY_REMOVE_DEVICE",
- "IRP_MN_REMOVE_DEVICE",
- "IRP_MN_CANCEL_REMOVE_DEVICE",
- "IRP_MN_STOP_DEVICE",
- "IRP_MN_QUERY_STOP_DEVICE",
- "IRP_MN_CANCEL_STOP_DEVICE",
- "IRP_MN_QUERY_DEVICE_RELATIONS",
- "IRP_MN_QUERY_INTERFACE",
- "IRP_MN_QUERY_CAPABILITIES",
- "IRP_MN_QUERY_RESOURCES",
- "IRP_MN_QUERY_RESOURCE_REQUIREMENTS",
- "IRP_MN_QUERY_DEVICE_TEXT",
- "IRP_MN_FILTER_RESOURCE_REQUIREMENTS",
- "",
- "IRP_MN_READ_CONFIG",
- "IRP_MN_WRITE_CONFIG",
- "IRP_MN_EJECT",
- "IRP_MN_SET_LOCK",
- "IRP_MN_QUERY_ID",
- "IRP_MN_QUERY_PNP_DEVICE_STATE",
- "IRP_MN_QUERY_BUS_INFORMATION",
- "IRP_MN_DEVICE_USAGE_NOTIFICATION",
- "IRP_MN_SURPRISE_REMOVAL",
- };
- KdPrint(("PNP Request (%s)\n", fcnname[fcn]));
- #endif // DBG
- status = (*fcntab[fcn])(pdx, Irp);
- KdPrint(("Leave HelloWDMPnp\n"));
- return status;
- }
- /************************************************************************
- * 函数名称:HelloWDMDispatchRoutine
- * 功能描述:对缺省IRP进行处理
- * 参数列表:
- fdo:功能设备对象
- Irp:从IO请求包
- * 返回 值:返回状态
- *************************************************************************/
- #pragma PAGEDCODE
- NTSTATUS HelloWDMDispatchRoutine(IN PDEVICE_OBJECT fdo,
- IN PIRP Irp)
- {
- PAGED_CODE();
- KdPrint(("Enter HelloWDMDispatchRoutine\n"));
- Irp->IoStatus.Status = STATUS_SUCCESS;
- Irp->IoStatus.Information = 0; // no bytes xfered
- IoCompleteRequest(Irp, IO_NO_INCREMENT);
- KdPrint(("Leave HelloWDMDispatchRoutine\n"));
- return STATUS_SUCCESS;
- }
- /************************************************************************
- * 函数名称:HelloWDMUnload
- * 功能描述:负责驱动程序的卸载操作
- * 参数列表:
- DriverObject:驱动对象
- * 返回 值:返回状态
- *************************************************************************/
- #pragma PAGEDCODE
- void HelloWDMUnload(IN PDRIVER_OBJECT DriverObject)
- {
- PAGED_CODE();
- KdPrint(("Enter HelloWDMUnload\n"));
- KdPrint(("Leave HelloWDMUnload\n"));
- }</SPAN></STRONG>
/************************************************************************
* 文件名称:HelloWDM.cpp
* 作 者:张帆
* 完成日期:2007-11-1
*************************************************************************/
#include "HelloWDM.h"
/************************************************************************
* 函数名称:DriverEntry
* 功能描述:初始化驱动程序,定位和申请硬件资源,创建内核对象
* 参数列表:
pDriverObject:从I/O管理器中传进来的驱动对象
pRegistryPath:驱动程序在注册表的中的路径
* 返回 值:返回初始化驱动状态
*************************************************************************/
#pragma INITCODE
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING pRegistryPath)
{
KdPrint(("jinzihan Enter DriverEntry\n"));
pDriverObject->DriverExtension->AddDevice = HelloWDMAddDevice;
pDriverObject->MajorFunction[IRP_MJ_PNP] = HelloWDMPnp;
pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =
pDriverObject->MajorFunction[IRP_MJ_CREATE] =
pDriverObject->MajorFunction[IRP_MJ_READ] =
pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloWDMDispatchRoutine;
pDriverObject->DriverUnload = HelloWDMUnload;
KdPrint(("Leave DriverEntry\n"));
return STATUS_SUCCESS;
}
/************************************************************************
* 函数名称:HelloWDMAddDevice
* 功能描述:添加新设备
* 参数列表:
DriverObject:从I/O管理器中传进来的驱动对象
PhysicalDeviceObject:从I/O管理器中传进来的物理设备对象
* 返回 值:返回添加新设备状态
*************************************************************************/
#pragma PAGEDCODE
NTSTATUS HelloWDMAddDevice(IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT PhysicalDeviceObject)
{
PAGED_CODE();
KdPrint(("Enter HelloWDMAddDevice123\n"));
NTSTATUS status;
PDEVICE_OBJECT fdo;
UNICODE_STRING devName;
RtlInitUnicodeString(&devName, L"\\Device\\MyWDMDevice");
status = IoCreateDevice(
DriverObject,
sizeof(DEVICE_EXTENSION),
&(UNICODE_STRING)devName,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
&fdo);
if (!NT_SUCCESS(status))
return status;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)fdo->DeviceExtension;
pdx->fdo = fdo;
pdx->NextStackDevice = IoAttachDeviceToDeviceStack(fdo, PhysicalDeviceObject);
UNICODE_STRING symLinkName;
RtlInitUnicodeString(&symLinkName, L"\\DosDevices\\HelloWDM");
pdx->ustrDeviceName = devName;
pdx->ustrSymLinkName = symLinkName;
status = IoCreateSymbolicLink(&(UNICODE_STRING)symLinkName, &(UNICODE_STRING)devName);
if (!NT_SUCCESS(status))
{
IoDeleteSymbolicLink(&pdx->ustrSymLinkName);
status = IoCreateSymbolicLink(&symLinkName, &devName);
if (!NT_SUCCESS(status))
{
return status;
}
}
fdo->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE;
fdo->Flags &= ~DO_DEVICE_INITIALIZING;
KdPrint(("Leave HelloWDMAddDevice\n"));
return STATUS_SUCCESS;
}
/************************************************************************
* 函数名称:DefaultPnpHandler
* 功能描述:对PNP IRP进行缺省处理
* 参数列表:
pdx:设备对象的扩展
Irp:从IO请求包
* 返回 值:返回状态
*************************************************************************/
#pragma PAGEDCODE
NTSTATUS DefaultPnpHandler(PDEVICE_EXTENSION pdx, PIRP Irp)
{
PAGED_CODE();
KdPrint(("Enter DefaultPnpHandler\n"));
IoSkipCurrentIrpStackLocation(Irp);
KdPrint(("Leave DefaultPnpHandler\n"));
return IoCallDriver(pdx->NextStackDevice, Irp);
}
/************************************************************************
* 函数名称:HandleRemoveDevice
* 功能描述:对IRP_MN_REMOVE_DEVICE IRP进行处理
* 参数列表:
fdo:功能设备对象
Irp:从IO请求包
* 返回 值:返回状态
*************************************************************************/
#pragma PAGEDCODE
NTSTATUS HandleRemoveDevice(PDEVICE_EXTENSION pdx, PIRP Irp)
{
PAGED_CODE();
KdPrint(("Enter HandleRemoveDevice\n"));
Irp->IoStatus.Status = STATUS_SUCCESS;
NTSTATUS status = DefaultPnpHandler(pdx, Irp);
IoDeleteSymbolicLink(&(UNICODE_STRING)pdx->ustrSymLinkName);
//调用IoDetachDevice()把fdo从设备栈中脱开:
if (pdx->NextStackDevice)
IoDetachDevice(pdx->NextStackDevice);
//删除fdo:
IoDeleteDevice(pdx->fdo);
KdPrint(("Leave HandleRemoveDevice\n"));
return status;
}
/************************************************************************
* 函数名称:HelloWDMPnp
* 功能描述:对即插即用IRP进行处理
* 参数列表:
fdo:功能设备对象
Irp:从IO请求包
* 返回 值:返回状态
*************************************************************************/
#pragma PAGEDCODE
NTSTATUS HelloWDMPnp(IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
PAGED_CODE();
KdPrint(("Enter HelloWDMPnp\n"));
NTSTATUS status = STATUS_SUCCESS;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)fdo->DeviceExtension;
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
static NTSTATUS(*fcntab[])(PDEVICE_EXTENSION pdx, PIRP Irp) =
{
DefaultPnpHandler, // IRP_MN_START_DEVICE
DefaultPnpHandler, // IRP_MN_QUERY_REMOVE_DEVICE
HandleRemoveDevice, // IRP_MN_REMOVE_DEVICE
DefaultPnpHandler, // IRP_MN_CANCEL_REMOVE_DEVICE
DefaultPnpHandler, // IRP_MN_STOP_DEVICE
DefaultPnpHandler, // IRP_MN_QUERY_STOP_DEVICE
DefaultPnpHandler, // IRP_MN_CANCEL_STOP_DEVICE
DefaultPnpHandler, // IRP_MN_QUERY_DEVICE_RELATIONS
DefaultPnpHandler, // IRP_MN_QUERY_INTERFACE
DefaultPnpHandler, // IRP_MN_QUERY_CAPABILITIES
DefaultPnpHandler, // IRP_MN_QUERY_RESOURCES
DefaultPnpHandler, // IRP_MN_QUERY_RESOURCE_REQUIREMENTS
DefaultPnpHandler, // IRP_MN_QUERY_DEVICE_TEXT
DefaultPnpHandler, // IRP_MN_FILTER_RESOURCE_REQUIREMENTS
DefaultPnpHandler, //
DefaultPnpHandler, // IRP_MN_READ_CONFIG
DefaultPnpHandler, // IRP_MN_WRITE_CONFIG
DefaultPnpHandler, // IRP_MN_EJECT
DefaultPnpHandler, // IRP_MN_SET_LOCK
DefaultPnpHandler, // IRP_MN_QUERY_ID
DefaultPnpHandler, // IRP_MN_QUERY_PNP_DEVICE_STATE
DefaultPnpHandler, // IRP_MN_QUERY_BUS_INFORMATION
DefaultPnpHandler, // IRP_MN_DEVICE_USAGE_NOTIFICATION
DefaultPnpHandler, // IRP_MN_SURPRISE_REMOVAL
};
ULONG fcn = stack->MinorFunction;
if (fcn >= arraysize(fcntab))
{ // unknown function
status = DefaultPnpHandler(pdx, Irp); // some function we don't know about
return status;
} // unknown function
#if DBG
static char* fcnname[] =
{
"IRP_MN_START_DEVICE",
"IRP_MN_QUERY_REMOVE_DEVICE",
"IRP_MN_REMOVE_DEVICE",
"IRP_MN_CANCEL_REMOVE_DEVICE",
"IRP_MN_STOP_DEVICE",
"IRP_MN_QUERY_STOP_DEVICE",
"IRP_MN_CANCEL_STOP_DEVICE",
"IRP_MN_QUERY_DEVICE_RELATIONS",
"IRP_MN_QUERY_INTERFACE",
"IRP_MN_QUERY_CAPABILITIES",
"IRP_MN_QUERY_RESOURCES",
"IRP_MN_QUERY_RESOURCE_REQUIREMENTS",
"IRP_MN_QUERY_DEVICE_TEXT",
"IRP_MN_FILTER_RESOURCE_REQUIREMENTS",
"",
"IRP_MN_READ_CONFIG",
"IRP_MN_WRITE_CONFIG",
"IRP_MN_EJECT",
"IRP_MN_SET_LOCK",
"IRP_MN_QUERY_ID",
"IRP_MN_QUERY_PNP_DEVICE_STATE",
"IRP_MN_QUERY_BUS_INFORMATION",
"IRP_MN_DEVICE_USAGE_NOTIFICATION",
"IRP_MN_SURPRISE_REMOVAL",
};
KdPrint(("PNP Request (%s)\n", fcnname[fcn]));
#endif // DBG
status = (*fcntab[fcn])(pdx, Irp);
KdPrint(("Leave HelloWDMPnp\n"));
return status;
}
/************************************************************************
* 函数名称:HelloWDMDispatchRoutine
* 功能描述:对缺省IRP进行处理
* 参数列表:
fdo:功能设备对象
Irp:从IO请求包
* 返回 值:返回状态
*************************************************************************/
#pragma PAGEDCODE
NTSTATUS HelloWDMDispatchRoutine(IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
PAGED_CODE();
KdPrint(("Enter HelloWDMDispatchRoutine\n"));
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0; // no bytes xfered
IoCompleteRequest(Irp, IO_NO_INCREMENT);
KdPrint(("Leave HelloWDMDispatchRoutine\n"));
return STATUS_SUCCESS;
}
/************************************************************************
* 函数名称:HelloWDMUnload
* 功能描述:负责驱动程序的卸载操作
* 参数列表:
DriverObject:驱动对象
* 返回 值:返回状态
*************************************************************************/
#pragma PAGEDCODE
void HelloWDMUnload(IN PDRIVER_OBJECT DriverObject)
{
PAGED_CODE();
KdPrint(("Enter HelloWDMUnload\n"));
KdPrint(("Leave HelloWDMUnload\n"));
}
头文件HelloWDM.h如下:
- <STRONG><SPAN style="FONT-SIZE: 18px">/************************************************************************
- * 文件名称:HelloWDM.h
- * 作 者:张帆
- * 完成日期:2007-11-1
- *************************************************************************/
- #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);</SPAN></STRONG>
/************************************************************************
* 文件名称:HelloWDM.h
* 作 者:张帆
* 完成日期:2007-11-1
*************************************************************************/
#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);