windows driver 定时器的使用
阅读原文时间:2023年07月12日阅读:1

#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;  

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章