DCP是一种使用更加灵活的定时器,可以对任意间隔时间进行定时。DPC定时器的内部使用了一个定时器对象KTIMER,当你设定了定时器之后,从设定开始起经过这个时间之后操作系统会将一个DPC定时器的例程插入到DPC的队列,操作系统读取DPC队列的时候定时器例程就能够被执行。这里的DPC定时器例程就相当于一个定时器的回调函数。

在使用需要一些相关的初始化操作

1,初始化Timer

VOID KeInitializeTimer(PKTIMER Timer);
//参数Timer是定时器的指针

2,初始化DPC对象

VOID KeInitializeDpc(PRKDPC Dpc,PKDEFERRED_ROUTINE DeferredRoutine,PVOID DeferredContext);
//Dpc是输入参数DPC的对象指针
//DeferredRoutine是一个与DPC关联的定时器例程函数,当定时器被触发的时候这个例程函数就会被执行调用
//DeferredContext是一个传入定时器例程的参数

3,当初始化完成之后使用函数KeSetTimer函数开启定时器,

BOOLEAN KeSetTimer( PKTIMER Timer,LARGE_INTEGER DueTime,PKDPC Dpc);
//Timer是定时器对象的指针.
//DueTimer这个参数是设定的时间间隔.
//Dpc表示传入定时器例程的参数.
//返回值:BOOL类型的,表示成功或者失败.

4,取消定时器使用内核函数KeCancelTimer,

BOOLEAN KeCancelTimer(PKTIMER Timer);

在调用KeSetTimer函数之后,经过DueTime的时间,操作系统会调用一次定时器的例程,其中如果DueTime是一个正整数,则会代表绝对时间,也就是相对于1601年1月1日到触发DPC定时器的那个时刻,如果DueTime是负数,那它表示的是间隔多长时间,DueTime的单位是100ns

示例代码:

用户层

#define IO_CONTROL_TRANSMIT_EVENT CTL_CODE(FILE_DEVICE_UNKNOWN,0x8080,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IO_CONTROL_START_TIMER CTL_CODE(FILE_DEVICE_UNKNOWN,0x8081,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IO_CONTROL_STOP_TIMER CTL_CODE(FILE_DEVICE_UNKNOWN,0x8082,METHOD_IN_DIRECT,FILE_ANY_ACCESS) int main()
{
HANDLE hDevice = CreateFile(L"\\\\.\\MyDeviceSymbolLinkName01", GENERIC_READ | GENERIC_WRITE, , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
printf("failed to obtain file handle to device with win32 error code %d ...\n", GetLastError());
return ;
} DWORD dwOutput;
DWORD dwMircoSeconds = * * ;//100ns一个单位
//发送启动定时器命令
DeviceIoControl(hDevice, IO_CONTROL_START_TIMER, &dwMircoSeconds, sizeof(DWORD), NULL, , &dwOutput, NULL); Sleep();
//发送终止定时器命令
DeviceIoControl(hDevice, IO_CONTROL_STOP_TIMER, NULL, , NULL, , &dwOutput, NULL); CloseHandle(hDevice); return ;
}

驱动层:

//FirstDriver.h
#pragma once
#define IO_CONTROL_TRANSMIT_EVENT CTL_CODE(FILE_DEVICE_UNKNOWN,0x8080,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IO_CONTROL_START_TIMER CTL_CODE(FILE_DEVICE_UNKNOWN,0x8081,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IO_CONTROL_STOP_TIMER CTL_CODE(FILE_DEVICE_UNKNOWN,0x8082,METHOD_IN_DIRECT,FILE_ANY_ACCESS)
typedef struct _DEVICE_EXTENSION
{
PDEVICE_OBJECT pDevObj;
UNICODE_STRING ustrDeviceName;
UNICODE_STRING ustrSymbolLinkName;
KDPC pollingDPC;
KTIMER pollingTimer;
LARGE_INTEGER pollingInterVal;
}DEVICE_EXTENSION, * PDEVICE_EXTENSION; NTSTATUS unload(PDRIVER_OBJECT driver); NTSTATUS DemoDeviceControlDispatch(PDEVICE_OBJECT pDeviceObject, PIRP pIrp); NTSTATUS DeviceCreate(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);
NTSTATUS DeviceClose(PDEVICE_OBJECT pDeviceObject, PIRP pIrp); NTSTATUS DemoCreateDevice(PDRIVER_OBJECT pDriver, PCWSTR devName, PCWSTR devSymName);
//FirstDriver.c
#include <ntddk.h>
#include "FirstDriver.h" NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
NTSTATUS ntstatus = STATUS_SUCCESS;
driver->DriverUnload = unload;
driver->MajorFunction[IRP_MJ_CREATE] = DeviceCreate;//创建
driver->MajorFunction[IRP_MJ_CLOSE] = DeviceClose;//关闭
driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DemoDeviceControlDispatch; ntstatus = DemoCreateDevice(driver, L"\\Device\\MyDevice01", L"\\??\\MyDeviceSymbolLinkName01");
if (!NT_SUCCESS(ntstatus))
{
DbgPrint("IoCreateDevice Failed");
return ntstatus;
} DbgPrint("%ws", reg_path->Buffer);
DbgPrint("driver load success...");
return STATUS_SUCCESS;
} VOID PollingTimerDPC(PKDPC pDpc, PVOID pContext, PVOID SysArg1, PVOID SysArg2)
{
PDEVICE_OBJECT pDevObj = (PDEVICE_OBJECT)pContext;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; KeSetTimer(&pdx->pollingTimer, pdx->pollingInterVal, &pdx->pollingDPC);
KdPrint(("PollingTimerDpc\n")); //检验是运行在任意线程上下文
PEPROCESS pEProcess = IoGetCurrentProcess(); PTSTR ProcessName = (PTSTR)((LONGLONG)pEProcess + 0x174); DbgPrint("多余参数 %d , %d , %d", pDpc->Number, SysArg1, SysArg2);
DbgPrint("%s\n", ProcessName);
} NTSTATUS DemoDeviceControlDispatch(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
{
NTSTATUS ntstatus = STATUS_SUCCESS;
DbgPrint("enter DemoDeviceControlDispatch ...\n"); PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pIrp);
//ULONG cbin = stack->Parameters.DeviceIoControl.InputBufferLength;
//ULONG cbout = stack->Parameters.DeviceIoControl.OutputBufferLength;
ULONG ctlCode = stack->Parameters.DeviceIoControl.IoControlCode;
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDeviceObject->DeviceExtension; ULONG info = ;
switch (ctlCode)
{
case IO_CONTROL_START_TIMER:
{ KdPrint(("IOCTL_START_TIMER!\n")); //从用户模式传进来的超时
ULONG ulMircoSeconds = *(PULONG)pIrp->AssociatedIrp.SystemBuffer; pDevExt->pollingInterVal = RtlConvertLongToLargeInteger(ulMircoSeconds * -); KeSetTimer(&pDevExt->pollingTimer, pDevExt->pollingInterVal, &pDevExt->pollingDPC); break;
}
case IO_CONTROL_STOP_TIMER:
{
KdPrint(("IOCTL_STOP_TIMER!\n")); KeCancelTimer(&pDevExt->pollingTimer); break;
}
default:
ntstatus = STATUS_INVALID_VARIANT;
break;
}
pIrp->IoStatus.Status = ntstatus;
pIrp->IoStatus.Information = info;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
DbgPrint("leave the demo device control dispatch ...");
DbgPrint("device io control test success...%d", pDeviceObject->ActiveThreadCount);
return ntstatus;
} NTSTATUS DeviceCreate(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
{
//业务代码区 //设置返回状态
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = ;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
DbgPrint("create device success...%d", pDeviceObject->ActiveThreadCount);
return STATUS_SUCCESS;
} NTSTATUS DeviceClose(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
{
//业务代码区 //设置返回状态
pIrp->IoStatus.Status = STATUS_SUCCESS;//getLastError()得到的值
pIrp->IoStatus.Information = ; //返回给3环多少数据,没有填0
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
DbgPrint("close device success...%d", pDeviceObject->ActiveThreadCount);
return STATUS_SUCCESS;
} NTSTATUS DemoCreateDevice(PDRIVER_OBJECT pDriver, PCWSTR devName, PCWSTR devSymName)
{
PDEVICE_OBJECT pDevice;
PDEVICE_EXTENSION pDevExt; UNICODE_STRING DeviceName;
UNICODE_STRING SymbolLinkName; RtlInitUnicodeString(&DeviceName, devName);
RtlInitUnicodeString(&SymbolLinkName, devSymName); NTSTATUS ntstatus = STATUS_SUCCESS;
ntstatus = IoCreateDevice(pDriver, sizeof(DEVICE_EXTENSION), &DeviceName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &pDevice);
if (!NT_SUCCESS(ntstatus))
{
DbgPrint("IoCreateDevice Failed");
return ntstatus;
} ntstatus = IoCreateSymbolicLink(&SymbolLinkName, &DeviceName);
if (!NT_SUCCESS(ntstatus))
{
DbgPrint("IoCreateSymbolicLink Failed");
IoDeleteDevice(pDevice);
return ntstatus;
} pDevice->Flags |= DO_BUFFERED_IO; pDevExt = (PDEVICE_EXTENSION)pDevice->DeviceExtension;
pDevExt->pDevObj = pDevice;
pDevExt->ustrDeviceName = DeviceName;
pDevExt->ustrSymbolLinkName = SymbolLinkName; KeInitializeTimer(&pDevExt->pollingTimer);
KeInitializeDpc(&pDevExt->pollingDPC, PollingTimerDPC, (PVOID)pDevice);
return ntstatus;
} NTSTATUS unload(PDRIVER_OBJECT driver)
{
PDEVICE_OBJECT pDev;
DbgPrint("driver :%ws unload", driver->DriverName.Buffer);
pDev = driver->DeviceObject;
while (pDev != NULL)
{
PDEVICE_EXTENSION pDevExt = { };
pDevExt = (PDEVICE_EXTENSION)pDev->DeviceExtension; //删除符号链接
UNICODE_STRING pLinkName = pDevExt->ustrSymbolLinkName;
DbgPrint("this is the divice name : %ws", pLinkName.Buffer);
IoDeleteSymbolicLink(&pLinkName);
pDev = pDev->NextDevice;
IoDeleteDevice(pDevExt->pDevObj);
}
DbgPrint("driver unload success...");
return STATUS_SUCCESS;
}

最新文章

  1. 代码的坏味道(14)——重复代码(Duplicate Code)
  2. Android编程容易犯的错误之一
  3. Linux使用mount挂载samba共享
  4. 【linux】学习1
  5. display &amp; visibility区别
  6. ubuntu下如何安装wxpython
  7. Informix如何释放异常的锁资源
  8. getdata
  9. FOJ 1962 新击鼓传花游戏 线段树
  10. [Android]自定义dialog
  11. Windows10 删除已经保存的WIFI热点
  12. 利用eclipse新建一个maven项目步骤:
  13. Flex动态获取数据,服务中断报错
  14. Docker常见仓库MongoDB
  15. 【嵌入式开发】 Bootloader 详解 ( 代码环境 | ARM 启动流程 | uboot 工作流程 | 架构设计)
  16. Java序列化Serializable和Externalizable
  17. js导出excel表格并生成多sheet
  18. [ZZ] 多领域视觉数据的转换、关联与自适应学习
  19. 【转】从此以后谁也别说我不懂LDO了!
  20. A1009. Product of Polynomials

热门文章

  1. 【C语言】 删除一个字符串中重复的字符
  2. Python环境搭建后,多种方式的使用进行程序的执行。
  3. Hadoop3.1.1源码Client详解 : Packet入队后消息系统运作之ResponseProcessor(ACK接收)
  4. 深入delphi编程理解之消息(一)WINDOWS原生窗口编写及消息处理过程
  5. VS常用快捷键(2012)
  6. 最新版的 vscode 怎么配置 Python?
  7. python 在linux上面安装beautifulsoup4(bs4) No module named &#39;bs4&#39;
  8. Celery异步框架
  9. 洛谷 P5018 对称二叉树(搜索)
  10. windows jdk8