定时器2...DPC

刚才测试了 计时器  ...现在用的PDC

注意 KeInitializeDpc 和 IoInitializeTimer 的区别...前缀

代码很多相同,就不一一贴出了..可以参看上一篇



typedef struct _DEVICE_EXTENSION{	
	UNICODE_STRING ustrDeviceName;
	UNICODE_STRING ustrDeviceSymlink;

	KDPC dpc;
	KTIMER timer;
	LARGE_INTEGER liInterval;
}DEVICE_EXTENSION;
	KeInitializeTimer(&pDex->timer);
	KeInitializeDpc(&pDex->dpc,CustomDpc,pDex);
	pDex->liInterval=RtlConvertLongToLargeInteger(2 * -10000000);
	//从1961年1月1日到现在时刻,单位100ns.如果负数表示间隔时长
	//1s = 1000 ms = 10^6 us = 10^9 ns
NTSTATUS DRIVER5_DispatchDeviceControl(
    IN PDEVICE_OBJECT		DeviceObject,
    IN PIRP					Irp
    )
{
	NTSTATUS status = STATUS_SUCCESS;
	PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);

	DEVICE_EXTENSION *pDex=(DEVICE_EXTENSION*)DeviceObject->DeviceExtension;

	switch(irpSp->Parameters.DeviceIoControl.IoControlCode)
	{
	case IOCTL_DRIVER5_OPERATION:
		// status = SomeHandlerFunction(irpSp);
		break;
	case IOCOL_START:		
		KdPrint(("IOCOL_START"));
		KeSetTimer(&pDex->timer,pDex->liInterval,&pDex->dpc);
		break;
	case IOCOL_STOP:
		KdPrint(("IOCOL_STOP"));
		KeCancelTimer(&pDex->timer);
		break;
	}
	Irp->IoStatus.Status = STATUS_SUCCESS;
	Irp->IoStatus.Information = 0;
	IoCompleteRequest(Irp, IO_NO_INCREMENT);
	return STATUS_SUCCESS;
}
VOID
CustomDpc(
		  IN struct _KDPC  *Dpc,
		  IN PVOID  DeferredContext,
		  IN PVOID  SystemArgument1,
		  IN PVOID  SystemArgument2
		  ){

			  KdPrint(("CustomDpc"));

			  DEVICE_EXTENSION* pDex=(DEVICE_EXTENSION*)DeferredContext;
			  KeSetTimer(&pDex->timer,pDex->liInterval,&pDex->dpc);//如果要循环调用需要重设KeSetTimer
}


评论
文章信息
作者: admin
目录: driver
创建: 2014-03-26
更新: 2014-03-26
如果文章对你有帮助,请博主喝杯咖啡:-)