标签:
1 #include <wdm.h> 2 3 typedef struct { 4 UNICODE_STRING devName; 5 UNICODE_STRING linkName; 6 PDEVICE_OBJECT fdo; 7 PDEVICE_OBJECT nextStackDevice; 8 }DEVICE_EXTENSION, *PDEVICE_EXTENSION; 9 10 #pragma PAGEDCODE 11 void dump(PDEVICE_OBJECT pdo) 12 { 13 PAGED_CODE(); 14 //显示调试信息 15 KdPrint(("-------------------------------------\n")); 16 KdPrint(("Begin dump device stack\n")); 17 for (int i = 0; NULL != pdo; pdo = pdo->AttachedDevice, i++) 18 { 19 KdPrint(("the %d device in device stack\n", i)); 20 KdPrint(("device attcheddevice:%#010x", pdo->AttachedDevice)); 21 KdPrint(("device nextdevice:%#010x", pdo->NextDevice)); 22 KdPrint(("device stacksize:%d\n", pdo->StackSize)); 23 KdPrint(("device‘s driverobject:%#010x", pdo->DriverObject)); 24 } 25 KdPrint(("Dump over\n")); 26 KdPrint(("-------------------------------------\n")); 27 } 28 29 #pragma PAGEDCODE 30 NTSTATUS AddDevice(PDRIVER_OBJECT pDriverObject, PDEVICE_OBJECT PhysicalDeviceObject) //形参不知道 31 { 32 PAGED_CODE(); 33 34 KdPrint(("Enter AddDevice\n")); 35 36 NTSTATUS status; 37 PDEVICE_OBJECT pDeviceObject; 38 UNICODE_STRING devName, linkName; 39 PDEVICE_EXTENSION pDevExt; 40 41 //创建设备对象 42 RtlInitUnicodeString(&devName, L"\\Device\\WDM_ChenJiaqi"); 43 status = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENSION), &devName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDeviceObject); 44 if (!NT_SUCCESS(status)) 45 { 46 return status; 47 } 48 //创建符号链接 49 RtlInitUnicodeString(&linkName, L"\\??\\WDM_ChenJiaqi"); 50 status = IoCreateSymbolicLink(&linkName, &devName); 51 if (!NT_SUCCESS(status)) 52 { 53 IoDeleteDevice(pDeviceObject); 54 return status; 55 } 56 //将FDO附加到PDO之上 57 PDEVICE_OBJECT pNextStackDevice = IoAttachDeviceToDeviceStack(pDeviceObject, PhysicalDeviceObject); 58 //设置设备扩展 59 pDevExt = reinterpret_cast<PDEVICE_EXTENSION>(pDeviceObject->DeviceExtension); 60 pDevExt->devName = devName; 61 pDevExt->linkName = linkName; 62 pDevExt->fdo = pDeviceObject; 63 pDevExt->nextStackDevice = pNextStackDevice; 64 //设备对象Flags 65 pDeviceObject->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE; 66 pDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING; 67 68 dump(pNextStackDevice); 69 KdPrint(("Leave AddDevice\n")); 70 return STATUS_SUCCESS; 71 } 72 73 #pragma PAGEDCODE 74 VOID DriverUnLoad(PDRIVER_OBJECT pDriverObject) 75 { 76 PAGED_CODE(); 77 //释放内存等相关操作 78 pDriverObject; 79 KdPrint(("Enter DriverUnLoad\n")); 80 KdPrint(("Leave DriverUnLoad\n")); 81 } 82 83 #pragma PAGEDCODE 84 NTSTATUS DefaultPnpHandler(PDEVICE_EXTENSION pdx, PIRP pIrp) 85 { 86 PAGED_CODE(); 87 KdPrint(("Enter DefaultPnpHandler\n")); 88 // 89 IoSkipCurrentIrpStackLocation(pIrp); 90 KdPrint(("Leave DefaultPnpHandler\n")); 91 // 92 return IoCallDriver(pdx->nextStackDevice, pIrp); 93 } 94 95 //对IRP_MN_REMOVE_DEVICE的处理,从DriverUnLoad的功能分离出来 96 #pragma PAGEDCODE 97 NTSTATUS HandleRemoveDevice(PDEVICE_EXTENSION pDeviceExtension, PIRP pIrp) 98 { 99 PAGED_CODE(); 100 101 KdPrint(("Enter HandleRemoveDevice\n")); 102 //设置IRP的完成状态 103 pIrp->IoStatus.Status = STATUS_SUCCESS; 104 pIrp->IoStatus.Information = 0; //读写字节数 105 //将IRP请求向底层驱动转发 106 NTSTATUS status = DefaultPnpHandler(pDeviceExtension, pIrp); //这个函数由我们自己编写 107 //删除符号链接 108 IoDeleteSymbolicLink(&pDeviceExtension->linkName); 109 //调用IoDetechDevice()把FDO从设备栈脱离开 110 IoDetachDevice(pDeviceExtension->nextStackDevice); 111 //删除FDO 112 IoDeleteDevice(pDeviceExtension->fdo); 113 KdPrint(("Leave HandleRemoveDevice\n")); 114 return status; 115 } 116 117 #pragma PAGEDCODE 118 NTSTATUS DefaultDispatchRoutinue(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) 119 { 120 //对IRP的处理 121 PAGED_CODE(); 122 KdPrint(("Enter DefaultDispatchRoutinue\n")); 123 pDeviceObject; 124 //设置IRP返回情况 125 pIrp->IoStatus.Status = STATUS_SUCCESS; 126 pIrp->IoStatus.Information = 0; // no bytes xfered 127 //完成对IRP的处理 128 IoCompleteRequest(pIrp, IO_NO_INCREMENT); 129 KdPrint(("Leave DefaultDispatchRoutinue\n")); 130 return STATUS_SUCCESS; 131 } 132 133 #pragma PAGEDCODE 134 NTSTATUS WDMPnp(IN PDEVICE_OBJECT fdo,IN PIRP Irp) 135 { 136 PAGED_CODE(); 137 138 KdPrint(("Enter HelloWDMPnp\n")); 139 NTSTATUS status = STATUS_SUCCESS; 140 //得到设备拓展 141 PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)fdo->DeviceExtension; 142 //得到当前IRP堆栈 143 PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp); 144 //定义函数指针列表 145 static NTSTATUS(*fcntab[])(PDEVICE_EXTENSION pdx, PIRP Irp) = 146 { 147 DefaultPnpHandler, // IRP_MN_START_DEVICE 148 DefaultPnpHandler, // IRP_MN_QUERY_REMOVE_DEVICE 149 HandleRemoveDevice, // IRP_MN_REMOVE_DEVICE 150 DefaultPnpHandler, // IRP_MN_CANCEL_REMOVE_DEVICE 151 DefaultPnpHandler, // IRP_MN_STOP_DEVICE 152 DefaultPnpHandler, // IRP_MN_QUERY_STOP_DEVICE 153 DefaultPnpHandler, // IRP_MN_CANCEL_STOP_DEVICE 154 DefaultPnpHandler, // IRP_MN_QUERY_DEVICE_RELATIONS 155 DefaultPnpHandler, // IRP_MN_QUERY_INTERFACE 156 DefaultPnpHandler, // IRP_MN_QUERY_CAPABILITIES 157 DefaultPnpHandler, // IRP_MN_QUERY_RESOURCES 158 DefaultPnpHandler, // IRP_MN_QUERY_RESOURCE_REQUIREMENTS 159 DefaultPnpHandler, // IRP_MN_QUERY_DEVICE_TEXT 160 DefaultPnpHandler, // IRP_MN_FILTER_RESOURCE_REQUIREMENTS 161 DefaultPnpHandler, // 162 DefaultPnpHandler, // IRP_MN_READ_CONFIG 163 DefaultPnpHandler, // IRP_MN_WRITE_CONFIG 164 DefaultPnpHandler, // IRP_MN_EJECT 165 DefaultPnpHandler, // IRP_MN_SET_LOCK 166 DefaultPnpHandler, // IRP_MN_QUERY_ID 167 DefaultPnpHandler, // IRP_MN_QUERY_PNP_DEVICE_STATE 168 DefaultPnpHandler, // IRP_MN_QUERY_BUS_INFORMATION 169 DefaultPnpHandler, // IRP_MN_DEVICE_USAGE_NOTIFICATION 170 DefaultPnpHandler, // IRP_MN_SURPRISE_REMOVAL 171 }; 172 //得到IRP的Minor编号,了解它是哪种类型的PNP请求 173 ULONG fcn = stack->MinorFunction; 174 if (fcn >= sizeof(fcntab) / sizeof(fcntab[0])) 175 { // 未知的子功能代码 176 status = DefaultPnpHandler(pdx, Irp); // some function we don‘t know about 177 return status; 178 } 179 //PNP请求名列表 180 static char* fcnname[] = 181 { 182 "IRP_MN_START_DEVICE", 183 "IRP_MN_QUERY_REMOVE_DEVICE", 184 "IRP_MN_REMOVE_DEVICE", 185 "IRP_MN_CANCEL_REMOVE_DEVICE", 186 "IRP_MN_STOP_DEVICE", 187 "IRP_MN_QUERY_STOP_DEVICE", 188 "IRP_MN_CANCEL_STOP_DEVICE", 189 "IRP_MN_QUERY_DEVICE_RELATIONS", 190 "IRP_MN_QUERY_INTERFACE", 191 "IRP_MN_QUERY_CAPABILITIES", 192 "IRP_MN_QUERY_RESOURCES", 193 "IRP_MN_QUERY_RESOURCE_REQUIREMENTS", 194 "IRP_MN_QUERY_DEVICE_TEXT", 195 "IRP_MN_FILTER_RESOURCE_REQUIREMENTS", 196 "", 197 "IRP_MN_READ_CONFIG", 198 "IRP_MN_WRITE_CONFIG", 199 "IRP_MN_EJECT", 200 "IRP_MN_SET_LOCK", 201 "IRP_MN_QUERY_ID", 202 "IRP_MN_QUERY_PNP_DEVICE_STATE", 203 "IRP_MN_QUERY_BUS_INFORMATION", 204 "IRP_MN_DEVICE_USAGE_NOTIFICATION", 205 "IRP_MN_SURPRISE_REMOVAL", 206 }; 207 208 KdPrint(("PNP Request (%s)\n", fcnname[fcn])); 209 //!!!根据IRP的MINOR编号,调用相应的请求处理函数 210 status = (*fcntab[fcn])(pdx, Irp); 211 KdPrint(("Leave HelloWDMPnp\n")); 212 return status; 213 } 214 215 #pragma INITCODE 216 extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegisterPath) 217 { 218 //设置pDriverObject结构体,注册AddDevice和相关PNP函数,包括IRP_MJ_PNP,IRP_MJ_CREATE等 219 KdPrint(("Enter DriverEntry\n")); 220 pRegisterPath; 221 //设置AddDevice函数 222 pDriverObject->DriverExtension->AddDevice = AddDevice; 223 //注册PNP 224 pDriverObject->MajorFunction[IRP_MJ_PNP] = WDMPnp; 225 pDriverObject->MajorFunction[IRP_MJ_CREATE] = 226 pDriverObject->MajorFunction[IRP_MJ_WRITE] = 227 pDriverObject->MajorFunction[IRP_MJ_READ] = 228 pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DefaultDispatchRoutinue; 229 KdPrint(("Leave DriverEntry\n")); 230 return STATUS_SUCCESS; 231 }
4 inf文件
;;WDM_Driver inf文件--2015年8月14日 [Version] Signature = "$Windows NT$" Class = WDM_Driver ClassGUID = {EF2962F0-0D55-4bff-B8AA-2221EE8A79B0} Provider = cposture DriverVer = CatalogFile=WDM_Driver.cat CatalogFile.ntamd64=WDM_Driver.cat CatalogFile.nt=WDM_Driver.cat [ClassInstall32.NTamd64] AddReg=Class_AddReg [Class_AddReg] HKR,,,,%DeviceClassName% HKR,,Icon,,"-5" [SourceDisksNames] 1 = %DiskName%,, [SourceDisksFiles] WDM_Driver层次结构.sys = 1 WDM_Driver层次结构.sys = 1 [DestinationDirs] DefaultDestDir = 12 my_files_driver = 12 [my_files_driver] WDM_Driver层次结构.sys [my_files_driver64] WDM_Driver层次结构.sys [Manufacturer] %MfgName%=Mfg0,NT,NTamd64 [Mfg0.NT] %DeviceDesc%=InstallLauncher, PCI\VEN_9999&DEV_9999 [Mfg0.NTamd64] %DeviceDesc%=InstallLauncher, PCI\VEN_9999&DEV_9999 ;---------- DDInstall Sections ----------------------------------------------- [InstallLauncher.NT] CopyFiles=my_files_driver AddReg=Install_NT_AddReg [InstallLauncher.NT.Services] Addservice = WDM_Driver, 0x00000002, Sys_AddService [Sys_AddService] DisplayName = %DeviceDesc% ServiceType = 1 StartType = 3 ErrorControl = 1 ServiceBinary = %12%\WDM_Driver层次结构.sys [Install_NT_AddReg] HKLM, "System\CurrentControlSet\Services\WDM_Driver\Parameters","BreakOnEntry", 0x00010001, 0 ;---------------------64位支持--------------- [InstallLauncher.NTamd64] CopyFiles=my_files_driver64 AddReg=Install_NT_AddReg64 [InstallLauncher.NTamd64.Services] Addservice = WDM_Driver, 0x00000002, Sys_AddService64 [Sys_AddService64] DisplayName = %DeviceDesc% ServiceType = 1 StartType = 3 ErrorControl = 1 ServiceBinary = %12%\WDM_Driver层次结构.sys [Install_NT_AddReg64] HKLM, "System\CurrentControlSet\Services\WDM_Driver\Parameters","BreakOnEntry", 0x00010001, 0 [Strings] MfgName="cpostute" DeviceDesc="WDM_Driver" DiskName="WDM_Driver Source Disk" DeviceClassName = WDM_Driver
本文链接:http://www.cnblogs.com/cposture/p/4734288.html
标签:
原文地址:http://www.cnblogs.com/cposture/p/4734288.html