windows driver 定时器的使用
#include < Ntifs.h> #pragma warning(disable:4995) #pragma comment(lib, "Ntoskrnl.lib") #pragma warning(disable:4995) typedef struct _Device_Extension_ { KTIMER timer; LARGE_INTEGER liDueTime; KDPC dpc; }DevExt; VOID CustomDpc( __in struct _KDPC *Dpc, __in_opt PVOID DeferredContext, __in_opt PVOID SystemArgument1, __in_opt PVOID SystemArgument2) { DevExt *pDevExt = (DevExt*)DeferredContext; KdPrint(("定时器中..")); //重新设置定时器 KeSetTimer(&pDevExt->timer, pDevExt->liDueTime, &pDevExt->dpc); } NTSTATUS MyDispatchClose(__in PDEVICE_OBJECT DeviceObject, __in PIRP Irp) { Irp->IoStatus.Status = STATUS_SUCCESS; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, IO_NO_INCREMENT); return STATUS_SUCCESS; } VOID MyUnload(__in PDRIVER_OBJECT DriverObject) { UNICODE_STRING strSymbolLink = RTL_CONSTANT_STRING(L"\\DosDevices\\Test"); DevExt *pDevExt = (DevExt *)DriverObject->DeviceObject->DeviceExtension; KeCancelTimer(&pDevExt->timer); IoDeleteSymbolicLink(&strSymbolLink); IoDeleteDevice(DriverObject->DeviceObject); KdPrint(("卸载设备成功")); } NTSTATUS DriverEntry(__in PDRIVER_OBJECT DriverObject, __in PUNICODE_STRING RegistryPath) { PDEVICE_OBJECT DeviceObject; ULONG i = 0; UNICODE_STRING strDeviceName = RTL_CONSTANT_STRING(L"\\Device\\Test_Driver"); UNICODE_STRING strSymbolLink = RTL_CONSTANT_STRING(L"\\DosDevices\\Test"); NTSTATUS status; DevExt *pDevExt = NULL; DriverObject->MajorFunction[IRP_MJ_CREATE] = DriverObject->MajorFunction[IRP_MJ_CLOSE] = MyDispatchClose; DriverObject->DriverUnload = MyUnload; status = IoCreateDevice(DriverObject, sizeof(DevExt), &strDeviceName, FILE_DEVICE_UNKNOWN, 0, FALSE, &DeviceObject); if (status != STATUS_SUCCESS){ return status; } DeviceObject->Flags |= DO_BUFFERED_IO; IoCreateSymbolicLink(&strSymbolLink, &strDeviceName); DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING; pDevExt = (DevExt*)DeviceObject->DeviceExtension; //初始化定时器 KeInitializeTimer(&pDevExt->timer); //初始化DPC KeInitializeDpc(&pDevExt->dpc,(PKDEFERRED_ROUTINE)CustomDpc, pDevExt); //设置定时时间为3s pDevExt->liDueTime = RtlConvertLongToLargeInteger(-30000000); //启动定时器 KeSetTimer(&pDevExt->timer,pDevExt->liDueTime,&pDevExt->dpc); return STATUS_SUCCESS; }
版权声明:本文为博主原创文章,未经博主允许不得转载。