首页
社区
课程
招聘
[求助]帮忙看看 irp write file 代码
发表于: 2009-12-17 19:45 5671

[求助]帮忙看看 irp write file 代码

2009-12-17 19:45
5671
NTSTATUS
IoCompletionRoutine(
        IN PDEVICE_OBJECT  DeviceObject,
        IN PIRP  Irp,
        IN PVOID  Context
        )
{
        *Irp->UserIosb = Irp->IoStatus;

        if (Irp->UserEvent)
                KeSetEvent(Irp->UserEvent, IO_NO_INCREMENT, 0);

        if (Irp->MdlAddress)
        {
                IoFreeMdl(Irp->MdlAddress);
                Irp->MdlAddress = NULL;
        }

        IoFreeIrp(Irp);

        return STATUS_MORE_PROCESSING_REQUIRED;
}

NTSTATUS
IrpFileWrite(
        IN PDEVICE_OBJECT DeviceObject,
        IN PFILE_OBJECT FileObject,
        IN PLARGE_INTEGER ByteOffset OPTIONAL,
        IN ULONG Length,
        IN PVOID Buffer,
        OUT PIO_STATUS_BLOCK IoStatusBlock
        )
{
        NTSTATUS status;
        KEVENT event;
        PIRP irp;
        PIO_STACK_LOCATION irpSp;
        IO_STATUS_BLOCK ioStatus;

        if (ByteOffset == NULL)
        {
                if (!(FileObject->Flags & FO_SYNCHRONOUS_IO))
                        return STATUS_INVALID_PARAMETER;

                ByteOffset = &FileObject->CurrentByteOffset;
        }

        if (FileObject->Vpb == 0 || FileObject->Vpb->RealDevice == NULL)
                return STATUS_UNSUCCESSFUL;

        //deviceObject = FileObject->Vpb->DeviceObject;
        irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);

        if (irp == NULL)
                return STATUS_INSUFFICIENT_RESOURCES;

        irp->MdlAddress = IoAllocateMdl(Buffer, Length, FALSE, TRUE, NULL);

        if (irp->MdlAddress == NULL)
        {
                IoFreeIrp(irp);
                return STATUS_INSUFFICIENT_RESOURCES;;
        }

        MmBuildMdlForNonPagedPool(irp->MdlAddress);

        irp->Flags = IRP_WRITE_OPERATION;
        irp->RequestorMode = KernelMode;
        irp->UserIosb = &ioStatus;
        irp->UserEvent = &event;
        irp->Tail.Overlay.Thread = (PETHREAD)KeGetCurrentThread();
        irp->Tail.Overlay.OriginalFileObject = FileObject;

        irpSp = IoGetNextIrpStackLocation(irp);
        irpSp->MajorFunction = IRP_MJ_WRITE;
        irpSp->MinorFunction = IRP_MN_NORMAL;
        irpSp->DeviceObject = DeviceObject ;
        irpSp->FileObject = FileObject;
        irpSp->Parameters.Write.Length = Length;
        irpSp->Parameters.Write.ByteOffset = *ByteOffset;

        KeInitializeEvent(&event, SynchronizationEvent, FALSE);
        IoSetCompletionRoutine(irp, IoCompletionRoutine, NULL, TRUE, TRUE, TRUE);
        status = IoCallDriver(DeviceObject , irp);

        if (status == STATUS_PENDING)
                status = KeWaitForSingleObject(&event, Executive, KernelMode, TRUE, NULL);
       
       
        status = ioStatus.Status ;

        *IoStatusBlock = ioStatus;

        return status;
}

这段代码是从网上找的,  我用在 fs filter 里 写文件,  可是会出错, 那里不对劲呢?

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 30
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
是什么错误?
2009-12-21 11:38
0
雪    币: 203
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
ntfs.sys!XXXXXX    bugCheck  ,  我发现只有在current process = system 时 write 会出错
2009-12-21 11:39
0
雪    币: 8835
活跃值: (2404)
能力值: ( LV12,RANK:760 )
在线值:
发帖
回帖
粉丝
4
请使用非irp的微软推荐的防冲入方式。
2009-12-21 18:07
0
游客
登录 | 注册 方可回帖
返回
//