IRP的非同步完成

2021-07-04 07:52:57 字數 4020 閱讀 7445

exe部分

#include #include #include #include "ioctl.h"

int main (void)

uchar buffer[10]=;

ulong ulread=0;

bool bread=readfile(hdevice,buffer,10,&ulread,&overlap1);

if (!bread && getlasterror()==error_io_pending)

else

bread=readfile(hdevice,buffer,10,&ulread,&overlap2);

if (!bread && getlasterror()==error_io_pending)

else

//迫使程式終止2秒

sleep(2000);

printf("ok3\n");

//建立irp_mj_cleanup irp

closehandle(hdevice);

getchar();

getchar();

return 0;

}

sys部分

#pragma once

#include #define countarray(array) ( sizeof(array) / sizeof(array[0]) )

#define max_file_length 1024

typedef struct _device_extension

device_extension,*pdevice_extension;

typedef struct _my_irp_entry

my_irp_entry,*pmy_irp_entry;

#ifdef __cplusplus

extern "c" ntstatus driverentry(in pdriver_object driverobject, in punicode_string registrypath);

#endif

void hellounload(in pdriver_object driverobject); //解除安裝函式

ntstatus createdevice(pdriver_object pdevobj); //建立裝置

ntstatus helloddkdispatchroutine(in pdevice_object pdevobj,in pirp pirp); //派遣函式

ntstatus helloddkcontrol(in pdevice_object pdevobj,in pirp pirp); //irp_mj_directory_control

ntstatus helloddkread(in pdevice_object pdevobj,in pirp pirp); //讀裝置

ntstatus helloddkcleanup(in pdevice_object pdevobj,in pirp pirp); //關閉

#include "hello.h"

#include "ioctl.h"

ntstatus driverentry(in pdriver_object driverobject, in punicode_string registrypath)

//driverobject->majorfunction[irp_mj_device_control]=helloddkcontrol;

driverobject->majorfunction[irp_mj_read]=helloddkread;

driverobject->majorfunction[irp_mj_cleanup]=helloddkcleanup;

#if dbg

_asm int 3

#endif

//建立裝置

createdevice(driverobject);

return status_success;

}//解除安裝函式

void hellounload(in pdriver_object driverobject)

//刪除裝置

iodeletedevice(pdevext->pdevice);

pnextobj=pnextobj->nextdevice;

}}ntstatus helloddkcontrol(in pdevice_object pdevobj,in pirp pirp)

break;

default:

status=status_invalid_variant;

} //設定irp的完成狀態

pirp->iostatus.status=status;

pirp->iostatus.information=0;

iocompleterequest(pirp,io_no_increment);

return status;

}//讀裝置irp

ntstatus helloddkread(in pdevice_object pdevobj,in pirp pirp)

ntstatus helloddkcleanup(in pdevice_object pdevobj,in pirp pirp)

//處理irp_mj_cleanup的irp

ntstatus status=status_success;

//完成irp

pirp->iostatus.status=status;

pirp->iostatus.information=0;

iocompleterequest(pirp,io_no_increment);

return status_success;

}//建立裝置

ntstatus createdevice(pdriver_object pdriver_object)

pdevobje->flags |= do_buffered_io;;

pdevext=(pdevice_extension)pdevobje->deviceextension;

pdevext->pdevice=pdevobje;

pdevext->ustrdevicename=devname;

pdevext->ustrsymlinkname=symlinkname;

pdevext->pirplinklisthead = (plist_entry)exallocatepool(pagedpool,sizeof(list_entry));

initializelisthead(pdevext->pirplinklisthead);

//建立符號連線

if (iocreatesymboliclink(&symlinkname,&devname)!=status_success )

return status_success;

}//派遣函式

ntstatus helloddkdispatchroutine(in pdevice_object pdevobj,in pirp pirp)

; uchar type = stack->majorfunction;

if (type >= countarray(irpname))

kdprint(("無效的irp型別 %x\n", type));

else

kdprint(("%s\n", irpname[type]));

pirp->iostatus.status=status_success; //設定完成狀態

pirp->iostatus.information=0; //設定操作位元組為0

iocompleterequest(pirp,io_no_increment); //結束irp派遣函式,第二個引數表示不增加優先順序

return status_success;

}

IRP請求的完成與返回

irp請求的完成與返回 每當完成了乙個以irp為代表的i o操作請求的時候,就要執行irp的善後操作iocompleterequest 這是個巨集操作,定義為函式iofcompleterequest define iocompleterequest iofcompleterequest 如前所述,以...

Windows驅動開發 IRP的完成例程

windows驅動開發技術詳解 331頁,在將irp傳送給底層驅動或其他驅動之前,可以對irp設定乙個完成例程,一旦底層驅動將irp完成後,irp完成例程立刻被處罰,通過設定完成例程可以方便地使程式設計師了解其他驅動對irp進行的處理,不管是呼叫自己的底層驅動或是呼叫其他驅動,都是使用核心函式ioc...

對IRP的理解

對irp的理解 驅動程式與i o管理器通訊,使用的是irp,即i o請求包。irp分為2部分 1 irp首部 2 irp堆疊。irp首部資訊如下 irp首部 io status block iostatus 包含i o請求的狀態 pvoid associatedirp.systembuffer 如果...