???? ID: 320275 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

IRPs - cheat ????? (1 ?? ?? ??? 2) ???????? ?? ???? ?????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

?????

Windows ??????? ???? (WDM) ??????? ??? ?????? ????? ??????? ??? ?? ?? ?? ????? ?????/?????? ?????? ????? (IRPs) ?? ??????? ?? ?? ???? ??????? ?? ???? ???? ??????? ?? ?? ????? IRP ????? ?? ?? ??? ???? ????? ??????? ????? ??, ?? ??????? ?? ??? ?? ??????? ?? ?? ???? ???? ??????? ?? ????? ???? IRPs ???????? ???? ???

?? ???? ????? ???? ?? ?? ??? ???? ????? ?? ???? ??????? ???????? ????? ??? ?? ??? ??? ??????? ?? ???? ?? ??? IRPs ??? ???? ???? ??????, ?? ???? ?? ??????? ????? ?? ???? ??? ??? ?? ???????? ??? ?? ?? ?? ???? ?? ???? ?? ???? ???? ?? ???? ???????? IRP ?????? ???? ???????? ?? ???????

??? 1 ?? ???? ?? ?? IRP ???? ?? ??? ???? ???? ??????? ?? ?????? ????? ?? ???????? ???? ?? ??? ???? ???? ?? ???? ??? 5 ???????? ?? ?????? ??, ?? (2 ?? ?? ??? ?? ???? ??? ????????) ??? 7 ???????? ???? IRP ????? ?? ??? ???? ???? ??????? ?? ????? ?? ??? ????? ?? ????? ????? ?? ???? ?? ??? 2 ????? ???????? ???? ??? ????? ??:
326315  (http://support.microsoft.com/kb/326315/ ) IRPs - cheat ????? (2 ?? ?? ??? 2) ???????? ?? ???? ?????
???? ???? ?? ?? ??????? ?????????? ?? ??????? ????? routines ?????? ??? ?? ?????? ?? ???? ??? ????? ????? ???:
??? IRP ??????? ????? STATUS_MORE_PROCESSING_REQUIRED ?? STATUS_SUCCESS ?? ???? ?? ???? ????
I/O ??????? ????? ?????? ?? ?? ?????? ????????? ?? ????? ???? ??:
  • ??? ?? ?????? ?? STATUS_MORE_PROCESSING_REQUIRED, ?????, IRP ????? ?? ??? ?? ?????????? ?????? ????? ????? ?? ?????? ?? ???? ???
  • ??? ?????? STATUS_MORE_PROCESSING_REQUIRED ?? ????? ??? ?? ??, ?? ???? upward IRP ????? ?? ??? ???
I/O ??????? ????? ??? ?? ???-?? ???-STATUS_MORE_PROCESSING_REQUIRED ??? ????? ???? ?? ??? ???? ??, ??????? ????? STATUS_SUCCESS (??????? ??? 0 ?? ??????? ???????? architectures ?????? loadable ??)?

??????? ?? ???, Windows XP SP1 ?? Windows ??? ????? ???? ?? ??? XP .NET ??????? ????????? ??? Ntddk.h ?? wdm.h ????? ??? ??????? ???? ?? ????# ????????? ?????? ??? STATUS_CONTINUE_COMPLETION, ?? ????? ??? ??? ?????? ?????? STATUS_SUCCESS ???? ?? ??? aliased ??:
// 
// This value should be returned from completion routines to continue
// completing the IRP upwards. Otherwise, STATUS_MORE_PROCESSING_REQUIRED
// should be returned.
// 
#define STATUS_CONTINUE_COMPLETION      STATUS_SUCCESS
// 
// Completion routines can also use this enumeration instead of status codes.
// 
typedef enum _IO_COMPLETION_ROUTINE_RESULT {
    
    ContinueCompletion = STATUS_CONTINUE_COMPLETION,
    StopCompletion = STATUS_MORE_PROCESSING_REQUIRED

} IO_COMPLETION_ROUTINE_RESULT, *PIO_COMPLETION_ROUTINE_RESULT;
				

???? ???????

???????? 1: ???????? ?? ??? ????

??? ???? IRP ???????? ???? ?? ??? ???????? ?????? ???? ?? ??? ??? ??????? ??? ????? ??, ?? ????? ??? ?? ????? ????? ?? ?????? ??? ??? ????? ????? ?? ??? ???? ?? ??? ??????? ???? ??? ??? ??????? ?? ?? ???? ??? ??????? ??, IRP ???? ???? ?? ?? ???? synchronously ?? asynchronously, ?? ??????? ?????? ??? ?? ?????? ?? ???? ???
NTSTATUS
DispatchRoutine_1(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    // 
    // You are not setting a completion routine, so just skip the stack
    // location because it provides better performance.
    // 
    IoSkipCurrentIrpStackLocation (Irp);
    return IoCallDriver(TopOfDeviceStack, Irp);
} 

???????? 2: ???????? ?? ?? ????????? ????

??? ??????? ?? ???? ?? ??? IRP ???????? ???? ?? ?? ?? ??? IRP ????????? ?? ???? ??? ?? ???? ???? ?? ??? ????????? ???? ?? ??? ?? ??????? ????? ??, ?? ????? ??? ?? ????? ????? ?? ?? PNP IRPs ???????? ??????? ?? ??? ??? ?????? ?? ???, ?? ?? ???? IRP_MN_START_DEVICE IRP ??????? ???? ??, ???? ?????? ?? ??????? ?? ??? ???? IRP ???????? ?? ?? ??? ???? ?????? ?? ??????? ???? ?? ???? ???? ???? ?? ????????? ????? Windows XP ?????? ??? ??? ??? ?????? ?? ??? ??IoForwardIrpSynchronously?? ???? ?? ???????? ?? ????? ?? ???? ?? ????? ?? ???? ???
NTSTATUS
DispatchRoutine_2(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    KEVENT   event;
    NTSTATUS status;

    KeInitializeEvent(&event, NotificationEvent, FALSE);

    // 
    // You are setting completion routine, so you must copy
    // current stack location to the next. You cannot skip a location
    // here.
    // 
    IoCopyCurrentIrpStackLocationToNext(Irp);

    IoSetCompletionRoutine(Irp,
                           CompletionRoutine_2,
                           &event,
                           TRUE,
                           TRUE,
                           TRUE
                           );

    status = IoCallDriver(TopOfDeviceStack, Irp);

    if (status == STATUS_PENDING) {
        
       KeWaitForSingleObject(&event,
                             Executive, // WaitReason
                             KernelMode, // must be Kernelmode to prevent the stack getting paged out
                             FALSE,
                             NULL // indefinite wait
                             );
       status = Irp->IoStatus.Status;
    }
    
    // <---- Do your own work here.


    // 
    // Because you stopped the completion of the IRP in the CompletionRoutine
    // by returning STATUS_MORE_PROCESSING_REQUIRED, you must call
    // IoCompleteRequest here.
    // 
    IoCompleteRequest (Irp, IO_NO_INCREMENT);
    return status;

}
NTSTATUS
CompletionRoutine_2(
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp,
    IN PVOID            Context
    )
{ 
  if (Irp->PendingReturned == TRUE) {
    // 
    // You will set the event only if the lower driver has returned
    // STATUS_PENDING earlier. This optimization removes the need to
    // call KeSetEvent unnecessarily and improves performance because the
    // system does not have to acquire an internal lock.  
    // 
    KeSetEvent ((PKEVENT) Context, IO_NO_INCREMENT, FALSE);
  }
  // This is the only status you can return. 
  return STATUS_MORE_PROCESSING_REQUIRED;  
} 
				

?????? 3: ??? ????? ????? ?????????

?? ?????? ???, ??????? ??? ????? ????? ??? ???? ??, ???? IRP ???????? ???? ??, ?? ???? ??? ????? ??????? ?? ???? ?? ?????? ???? ??? ????? ????? ?????? ?? ???????? IRP ?? ???? ???? ??? ?? ??????? ?? ??????? ???? ?? ??? ???
NTSTATUS
DispathRoutine_3(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    NTSTATUS status;

    // 
    // Because you are setting completion routine, you must copy the
    // current stack location to the next. You cannot skip a location
    // here.
    // 
    IoCopyCurrentIrpStackLocationToNext(Irp); 

    IoSetCompletionRoutine(Irp,
                           CompletionRoutine_31,// or CompletionRoutine_32
                           NULL,
                           TRUE,
                           TRUE,
                           TRUE
                           );
    
    return IoCallDriver(TopOfDeviceStack, Irp);

} 
??? ?? ???? ?????? ????? ?? ?? ??????? ?? ?????? ????:
  • ?? ??? ????? ????? IRP ?? ?????? ???? ????????? ???? ?????? ??? ?? ????????? ???? ?? IRP IoStatus ??? ?????? ??? ??? ???? ?? ??? ?? ??? (-> IoStatus.Status Irp) ?? ??? ??? ?? ????????? ?? ????? ?????? ???? ????
  • -> PendingReturned Irp ?????? ?????? ?? ??? ??? ????? IRP ?? ?????? ????????? ???? ?????? ???
  • ?? IRP ?? synchronicity ???? ????????? ???? ?????? ???
????????????, ??? ???? ?? ?????? ????? (31 ?? 32), ?? ?????? ??? ????? ??????? 2:
 
NTSTATUS
CompletionRoutine_31 (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp,
    IN PVOID            Context
    )
{   

    // 
    // Because the dispatch routine is returning the status of lower driver
    // as is, you must do the following:
    // 
    if (Irp->PendingReturned) {
        
        IoMarkIrpPending( Irp );
    }
    
    return STATUS_CONTINUE_COMPLETION ; // Make sure of same synchronicity 
}

NTSTATUS
CompletionRoutine_32 (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp,
    IN PVOID            Context
    )
{   
    // 
    // Because the dispatch routine is returning the status of lower driver
    // as is, you must do the following:
    // 
    if (Irp->PendingReturned) {
        
        IoMarkIrpPending( Irp );
    }
    
    //    
    // To make sure of the same synchronicity, complete the IRP here.
    // You cannot complete the IRP later in another thread because the 
    // the dispatch routine is returning the status returned by the lower
    // driver as is.
    // 
    IoCompleteRequest( Irp,  IO_NO_INCREMENT);  

    // 
    // Although this is an unusual completion routine that you rarely see,
    // it is discussed here to address all possible ways to handle IRPs.  
    // 
    return STATUS_MORE_PROCESSING_REQUIRED; 
} 

				

??? ???, ?? ??? ?????????? ???????? 4: ?? ???????? ???? ?? ???: ?????

??? ?????? ?? ???? ??????? ?? ?? ?? IRP ?????????? ?? ??? ??? ??????? ???? ?? IRP ????? ??????? ?? ??? ???????? ???? ?? ???? IRP ?? ???? ???? ?? ???? ?? ??? ?? ?? ??????? ?????? ?? ??? ????? ???? ?? ??? ????? ??? ????? ??? snippet ?? ????? ????? ?????? ????? ????? IRP ??????? ???? ??, ?? ??? ??? ?? ??? ????? ??? ????? ???? ?? ??? IRP ???? ?? ??????? STATUS_PENDING ???? ??? ????, ??????? ??????? ???? ?????? ?????? (??? contrast ????? ????????) IRP ?? ?????? ????????? ?????
NTSTATUS
DispathRoutine_4(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    NTSTATUS status;

    // 
    // You mark the IRP pending if you are intending to queue the IRP
    // and process it later. If you are intending to forward the IRP 
    // directly, use one of the methods discussed earlier in this article.
    // 
    IoMarkIrpPending( Irp );    

    // 
    // For demonstration purposes: this IRP is forwarded to the lower driver.
    // 
    IoCopyCurrentIrpStackLocationToNext(Irp); 

    IoSetCompletionRoutine(Irp,
                           CompletionRoutine_41, // or CompletionRoutine_42
                           NULL,
                           TRUE,
                           TRUE,
                           TRUE
                           ); 
    IoCallDriver(TopOfDeviceStack, Irp);

    // 
    // Because you marked the IRP pending, you must return pending,
    // regardless of the status of returned by IoCallDriver.
    // 
    return STATUS_PENDING ;

}
????? ????? STATUS_CONTINUE_COMPLETION ?? STATUS_MORE_PROCESSING_REQUIRED ?? ?? ?? ?? ???? ???? ??? ?? ???? ???? ????? ?? IRP ???: ????? ???? ?? ??? ??? ??? ????? ???? ?? ??? ???????? ???? STATUS_MORE_PROCESSING_REQUIRED ???? ???
NTSTATUS
CompletionRoutine_41(
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp,
    IN PVOID            Context
    )
{ 
    // 
    // By returning STATUS_CONTINUE_COMPLETION , you are relinquishing the 
    // ownership of the IRP. You cannot touch the IRP after this.
    // 
    return STATUS_CONTINUE_COMPLETION ; 
} 


NTSTATUS
CompletionRoutine_42 (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp,
    IN PVOID            Context
    )
{  
    // 
    // Because you are stopping the completion of the IRP by returning the
    // following status, you must complete the IRP later.
    // 
    return STATUS_MORE_PROCESSING_REQUIRED ; 
} 
				

???????? 5: ????? ?????? ????? ??? IRP

?? ???????? ??? ?????? ????? ?? IRP ?? ???? ???? ?? ??? ???? ?????? ???

???????????? ?? ???? IRP ?????? ????? ??? ???? ????, ?????? ????? ?? ????? ?????? IRP (-> IoStatus.Status Irp) ?? IoStatus ????? ??? ??? ?? ?? ??? ?? ?????? ?? ??? ??????
NTSTATUS
DispatchRoutine_5(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    // 
    // <-- Process the IRP here.
    // 
    Irp->IoStatus.Status = STATUS_XXX;
    Irp->IoStatus.Information = YYY;
    IoCompletRequest(Irp, IO_NO_INCREMENT);
    return STATUS_XXX;
} 

??????

?? ???? ?? ??? 2 ????? ???????? ???? ??? ????? ??:
326315  (http://support.microsoft.com/kb/326315/ ) IRPs - cheat ????? (2 ?? ?? ??? 2) ???????? ?? ???? ?????
  • Walter Oney ???Windows ??????? ???? ????????????????? ???????, ?????? 5?

???? ???? ???? ??:
  • Microsoft Windows XP Driver Development Kit
??????: 
kbinfo kbkmode kbwdm kbmt KB320275 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:320275  (http://support.microsoft.com/kb/320275/en-us/ )