📄 async.c
字号:
//
// (C) Copyright 1997 OSR Open Systems Resources, Inc.
// All Rights Reserved
//
#include <ntddk.h>
//
// Forward declaration
//
NTSTATUS AsyncCompletion(PDEVICE_OBJECT, PIRP, PVOID);
//
// This demonstrates how to build an asynchronous I/O request. Note
// that the caller (in this case) does NOT need to process the request.
//
BOOLEAN SendAsyncIrp(PDEVICE_OBJECT DeviceObject,
ULONG MajorFunction, // read, write, flush, etc.
PFILE_OBJECT FileObject,
PVOID Buffer,
ULONG BufferLength,
ULONG Offset,
PIO_STATUS_BLOCK IoStatusBlock)
{
PIRP Irp;
LARGE_INTEGER StartingOffset;
//
// The starting offset is a quad. For this example we restricted to ULONG
// so we do the conversion here. For YOUR work, you can use a quad if that's
// what's needed.
//
StartingOffset.QuadPart = (LONGLONG) Offset;
//
// We're going to build the I/O request. Note that this is an ASYNCHRONOUS
// request, so we don't provide it with an Event to set when the I/O itself
// is done.
//
Irp = IoBuildAsynchronousFsdRequest(MajorFunction,
DeviceObject,
Buffer,
BufferLength,
&StartingOffset,
IoStatusBlock);
if (!Irp) {
//
// Creation of the IRP failed. This may happen when there is insufficient memory to
// create the IRP (non-paged pool exhaustion.)
//
return FALSE;
}
//
// If you are going to call your own driver with this IRP, you should advance
// the I/O stack location (c.f. floppy.c for an example of this) using the
// IoSetNextIrpStackLocation() call.
//
// IoSetNextIrpStackLocation(Irp);
//
// Set a completion routine for processing this IRP after it has completed. This
// will allow us to free the IRP and avoid completion processing.
//
IoSetCompletionRoutine(Irp, AsyncCompletion, NULL, TRUE, TRUE, TRUE);
//
// Pass along to the underlying device
//
IoCallDriver(DeviceObject, Irp);
//
// Done!
//
return TRUE;
}
//
// AsyncCompletion
//
NTSTATUS AsyncCompletion(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
{
//
// For the asynchronous example, cleanup of the MDLs attached to the IRP
// are done here in the completion routine. This allows us to free the
// IRP below.
//
if (Irp->MdlAddress) {
MmUnmapLockedPages(MmGetSystemAddressForMdl(Irp->MdlAddress), Irp->MdlAddress);
MmUnlockPages(Irp->MdlAddress);
IoFreeMdl(Irp->MdlAddress);
}
IoFreeIrp(Irp);
//
// This advises the I/O Manager to STOP PROCESSING this request - that the
// DRIVER is going to perform (and may, in fact, have already performed)
// additional processing on the I/O request.
//
// In fact, in this case, note that we've already freed the IRP - it might be
// a VERY BAD idea for the I/O Manager to do ANYTHING with that IRP at this
// point (since it isn't even an IRP anymore...)
//
return STATUS_MORE_PROCESSING_REQUIRED;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -