内核事件等待KeWaitForSingleObject超时处理和IPR的完成例程
2021-08-30
582
0
Windows内核中通过KeWaitForSingleObject等待事件完成,并且设置IPR的完成例程
NTSTATUS
IrpCompletionRoutine(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PVOID Context
)
{
UNREFERENCED_PARAMETER(Irp);
UNREFERENCED_PARAMETER(DeviceObject);
PKEVENT event = (PKEVENT)Context;
if (event != NULL)
{
KeSetEvent(event, IO_NO_INCREMENT, FALSE);
}
return STATUS_MORE_PROCESSING_REQUIRED;
}
VOID IrpCancelRoutine(PDEVICE_OBJECT devObj, PIRP Irp)
{
UNREFERENCED_PARAMETER(devObj);
IoReleaseCancelSpinLock(Irp->CancelIrql);
Irp->IoStatus.Status = STATUS_CANCELLED;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
}
void Test()
{
KEVENT event;
KeInitializeEvent(&event, NotificationEvent, FALSE);
IoCopyCurrentIrpStackLocationToNext(Irp);
IoSetCompletionRoutine(Irp,
(PIO_COMPLETION_ROUTINE)IrpCompletionRoutine,
(PVOID) & event,
TRUE,
TRUE,
TRUE);
IoSetCancelRoutine(Irp, IrpCancelRoutine);
status = IoCallDriver(DeviceObject, Irp);
if (status == STATUS_PENDING)
{
LARGE_INTEGER interval;
interval.QuadPart = 3000 * -10000;//100ns为单位,这时待待3秒种,负数
status = KeWaitForSingleObject(&event,
Executive,
KernelMode,
FALSE,
&interval);
if (status == STATUS_SUCCESS)
{
status = Irp->IoStatus.Status;
}
else
{
IoCancelIrp(Irp);
}
DbgPrint("SyncForwardIrp KeWaitForSingleObject:[%08x]\n", status);
}
else
{
DbgPrint("SyncForwardIrp IoCallDriver:[%08x]\n", status);
}
}