How to correct write/read error with deviceIoControl from different processes?

Good afternoon
I have a kmdf driver that can read and write to a file.
Through a custom application I can send IOCTL requests. If I send a request from one process to write, from another process to read, everything is fine. But if I send a read request and then a write request, both requests do not complete and seem to hang.
This is the loop I have in my custom application

do
    {
        printf("Enter command (read/write): ");
        scanf("%s", command);

        bufLength = 25;

        if (strcmp(command, "read") == 0)
        {
            if (!DoFileRead(hDevice, bufLength))
            {
                break;
            }
        }
        else if (strcmp(command, "write") == 0)
        {
            if (!DoFileWrite(hDevice, bufLength))
            {
                break;
            }
        }
    }
    WHILE(TRUE);

Handler code in the driver

VOID
FileEvtIoDeviceControl(
    IN WDFQUEUE         Queue,
    IN WDFREQUEST       Request,
    IN size_t           OutputBufferLength,
    IN size_t           InputBufferLength,
    IN ULONG            IoControlCode
    )
{
    NTSTATUS            status = STATUS_SUCCESS;
    NTSTATUS            rstatus = STATUS_SUCCESS;
    IO_STATUS_BLOCK             ioStatus;
    PCHAR inBuf = NULL;
    PCHAR outBuf = NULL;
    PCHAR               data = "this String is from Device Driver !!!";
    ULONG               datalen = (ULONG) strlen(data)+1;
    PCHAR               buffer = NULL;
    PREQUEST_CONTEXT    reqContext = NULL;
    size_t               bufSize;
    PCONTROL_DEVICE_EXTENSION   devExt;
    FILE_POSITION_INFORMATION   position;
    ULONG_PTR                   bytesWritten = 0;
    ULONG_PTR                   bytesRead = 0;
    WDFDEVICE device;
    UNREFERENCED_PARAMETER( Queue );
    PAGED_CODE();
    switch (IoControlCode)
    {
        case IOCTL_WRITE:
        status = WdfRequestRetrieveInputBuffer(Request, 0, &inBuf, &bufSize);
        if(!NT_SUCCESS(status)) {
            status = STATUS_INSUFFICIENT_RESOURCES;
            break;
        }
        devExt = ControlGetData(WdfIoQueueGetDevice(Queue));
        if(devExt->FileHandle) {
            position.CurrentByteOffset.QuadPart = 0;

            status = ZwSetInformationFile(devExt->FileHandle,
                                    &ioStatus,
                                    &position,
                                    sizeof(FILE_POSITION_INFORMATION),
                                    FilePositionInformation);
            if (NT_SUCCESS(status))
            {

                status = ZwWriteFile(devExt->FileHandle, NULL,
                                    NULL, NULL, &ioStatus, inBuf,
                                    (ULONG)InputBufferLength, 0, NULL);
                if (!NT_SUCCESS(status))
                {
                    //...
                }
                status = ioStatus.Status;
                bytesWritten =  ioStatus.Information;
            }
        }
        WdfRequestCompleteWithInformation(Request, status, bytesWritten);
        if (devExt->PendingRequest) {
            rstatus = WdfRequestRetrieveOutputBuffer(devExt->PendingRequest, 0, &outBuf, &bufSize);
            if (!NT_SUCCESS(rstatus))
            {
                WdfRequestComplete(devExt->PendingRequest, rstatus);
                return;
            }
            position.CurrentByteOffset.QuadPart = 0;
            rstatus = ZwSetInformationFile(devExt->FileHandle,
                                        &ioStatus,
                                        &position,
                                        sizeof(FILE_POSITION_INFORMATION),
                                        FilePositionInformation);
            if (NT_SUCCESS(rstatus))
            {
                rstatus = ZwReadFile(devExt->FileHandle,NULL,NULL,NULL,
                                    &ioStatus,outBuf,(ULONG)OutputBufferLength,
                                    0,NULL
                );
                if (!NT_SUCCESS(rstatus))
                {
                      //...
                }
                else
                {
                    WdfRequestCompleteWithInformation(devExt->PendingRequest, rstatus, ioStatus.Information);
                    devExt->isDataAdded = FALSE;
                    devExt->PendingRequest = NULL;
                }
            }
        } else {
            devExt->isDataAdded = TRUE;
        }
        return;
        break;
    case IOCTL_READ:
        status = WdfRequestRetrieveOutputBuffer(Request, 0, &outBuf, &bufSize);
        if(!NT_SUCCESS(status)) {
            WdfRequestComplete(Request, status);
            return;
        }
        devExt = ControlGetData(WdfIoQueueGetDevice(Queue));
        if(devExt->FileHandle) 
        {
            if (!devExt->isDataAdded)
            {
                if (!devExt->PendingRequest)
                {
                    KdPrint(("FileEvtIoRead, data not available...n"));
                    devExt->PendingRequest = Request;
                }
                else
                {
                    WdfRequestComplete(Request, STATUS_UNSUCCESSFUL);
                }
                return;
            }
            position.CurrentByteOffset.QuadPart = 0;
            status = ZwSetInformationFile(devExt->FileHandle,
                                &ioStatus,
                                &position,
                                sizeof(FILE_POSITION_INFORMATION),
                                FilePositionInformation);
            if (NT_SUCCESS(status)) {
                status = ZwReadFile (devExt->FileHandle,NULL,NULL,NULL,
                                    &ioStatus,outBuf,(ULONG)OutputBufferLength,
                                    0,NULL
                                    );
                if (!NT_SUCCESS(status)) {
                    //...
                }
                else
                {
                    devExt->isDataAdded = FALSE;
                }
                status = ioStatus.Status;
                bytesRead = ioStatus.Information;
            }
        }
        WdfRequestSetInformation(Request, bytesRead);
        break;
    default:
        status = STATUS_INVALID_DEVICE_REQUEST;
        break;
    }
    WdfRequestComplete( Request, status);

}

Tried changing the end of the queries, but nothing works

New contributor

Артём Варнавский is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật