IRP_MJ_CREATE标识CreateFile句柄FsContext
2022-06-01
81
0
驱动程序可以通过CreateFile打开设备,不过有一种情况我们需要对不同的CreateFile句柄在驱动中标识不同的CONTEXT.
这个CONTEXT可以存储在rpSp->FileObject->FsContext中。
即每个CreateFile实例句柄都在驱动中有惟一对应的MY_CONTEXT
CreateFile
NTSTATUS IrpMajorCreate(PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
{
PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
ASSERT(irpSp->Parameters.Create.SecurityContext);
DBGASSERT((irpSp->Parameters.Create.SecurityContext->DesiredAccess & (FILE_READ_DATA|FILE_WRITE_DATA)),
("Neither FILE_READ_DATA|FILE_WRITE_DATA requested in HidpIrpMajorCreate. DesiredAccess = %xh.", (ULONG)irpSp->Parameters.Create.SecurityContext->DesiredAccess),
FALSE)
//irpSp->Parameters.Create.ShareAccess == 0
//irpSp->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA
//irpSp->Parameters.Create.SecurityContext->DesiredAccess & FILE_READ_DATA
//irpSp->Parameters.Create.ShareAccess & FILE_SHARE_READ
//irpSp->Parameters.Create.ShareAccess & FILE_SHARE_WRITE
/
/Attempt to open this device as a directory
//irpSp->Parameters.Create.Options & FILE_DIRECTORY_FILE
irpSp->FileObject->FsContext = ExAllocatePool(NonPagedPool, sizeof(MY_CONTEXT));
...
}
CloseHandle
NTSTATUS IrpMajorClose(PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
{
PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
irpSp = IoGetCurrentIrpStackLocation( Irp );
MY_CONTEXT* fileExtension = (MY_CONTEXT*)fileObject->FsContext;
...
ExFreePool( fileExtension );
}