0x00 函数
IoInitializeTimer() //将IoTimer()与设备进行关联,每个设备对象只应调用一次IoInitializeTimer,执行间隔为1秒
IoStartTimer() //重新启用计时器
IoStopTimer() //禁用计时器
IoTimer() //计时器回调,是一个DPC,注册后每秒调用一次。IRQL = DISPATCH_LEVEL
0x01 代码
#include <wdm.h>
VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{
DbgPrint("DriverUnload");
if (DriverObject->DeviceObject)
{
IoDeleteDevice(DriverObject->DeviceObject);
}
return;
}
int index = 0;
void IoTimerRoutine(PDEVICE_OBJECT DeviceObject,PVOID Context)
{
DbgPrint("IoTimerRoutine index:%d",++index);
}
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
NTSTATUS status = STATUS_SUCCESS;
PDEVICE_OBJECT DeviceObject = NULL;
LARGE_INTEGER sleep = { 0 };
sleep.QuadPart = -10 * 1000 * 1000 * 10; //5秒 -> 单位为100纳秒,*10 = 微秒,*10*1000 = 毫秒,*10*1000*1000 = 秒
DbgPrint("DriverEntry");
DriverObject->DriverUnload = DriverUnload;
//Create Device
UNICODE_STRING DeviceName = { 0 };
RtlInitUnicodeString(&DeviceName,L"\\Device\\TimerDriver");
status = IoCreateDevice(DriverObject,0,&DeviceName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN,FALSE,&DeviceObject);
if (!NT_SUCCESS(status))
{
DbgPrint("DriverEntry -> IoCreateDevice Failed");
return status;
}
//Init IoTimer
DbgPrint("DriverEntry -> IoInitializeTimer");
IoInitializeTimer(DeviceObject, IoTimerRoutine,0);
//start timer
DbgPrint("DriverEntry -> IoStartTimer");
IoStartTimer(DeviceObject);
//sleep 10s
DbgPrint("DriverEntry -> KeDelayExecutionThread 10S");
KeDelayExecutionThread(KernelMode,FALSE,&sleep);
//stop timer
DbgPrint("DriverEntry -> IoStopTimer");
IoStopTimer(DeviceObject);
DbgPrint("DriverEntry Success");
return status;
}