Buffer mode read

Source: Internet
Author: User
Tags readfile
// Driver Layer Code
#ifdef __cplusplusextern "C" {#endifNTSTATUS DriverEntry(    IN OUT PDRIVER_OBJECT   DriverObject,    IN PUNICODE_STRING      RegistryPath    ){    PDEVICE_OBJECT pdoDeviceObj = 0;    NTSTATUS status = STATUS_UNSUCCESSFUL;    pdoGlobalDrvObj = DriverObject;    // Create the device object.    if(!NT_SUCCESS(status = IoCreateDevice(        DriverObject,        0,        &usDeviceName,        FILE_DEVICE_UNKNOWN,        FILE_DEVICE_SECURE_OPEN,        FALSE,        &pdoDeviceObj        )))    {        // Bail out (implicitly forces the driver to unload).        return status;    };    // Now create the respective symbolic link object    if(!NT_SUCCESS(status = IoCreateSymbolicLink(        &usSymlinkName,        &usDeviceName        )))    {        IoDeleteDevice(pdoDeviceObj);        return status;    }pdoDeviceObj->Flags=DO_BUFFERED_IO;    // NOTE: You need not provide your own implementation for any major function that    //       you do not want to handle. I have seen code using DDKWizard that left the    //       *empty* dispatch routines intact. This is not necessary at all!    DriverObject->MajorFunction[IRP_MJ_CREATE] =IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_READ]=IRPTEST_DISPATCH_READ;DriverObject->MajorFunction[IRP_MJ_WRITE]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_CLEANUP]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_SHUTDOWN]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL]=IRPTEST_Dispatch;DriverObject->MajorFunction[IRP_MJ_CLOSE] = IRPTEST_Dispatch;    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IRPTEST_DispatchDeviceControl;    DriverObject->DriverUnload = IRPTEST_DriverUnload;    return STATUS_SUCCESS;}#ifdef __cplusplus}; // extern "C"#endif

Ntstatus irptest_dispatch_read (pdevice_object pdeviceobject, pirp) {// ensure that the flags subdomain of the device has the do_buffer_io mode kdprint ("driver_read \ r \ n ")); __try {pio_stack_location piostack = iogetcurrentirpstacklocation (pirp); ulong ulreadlen = piostack-> parameters. read. length; // number of bytes to be read in readfile if (irp_mj_read = piostack-> majorfunction) {rtlfillmemory (pir-> associatedirp. systembuffer, ulreadlen, 0xaa); pir-> iostatus. information = ulreadlen ;}__ handle T (exception_execute_handler) {pir-> iostatus. information = 0; kdprint ("read dispatch routine exception \ r \ n");} pir-> iostatus. status = STATUS_SUCCESS; iocompleterequest (pirp, io_no_increment); kdprint ("driver_read_end \ r \ n"); return STATUS_SUCCESS ;}
// Application Layer call
Int _ tmain (INT argc, _ tchar * argv []) {handle hdev = createfile (_ T ("\\\\. \ symlink_irptest "), generic_read | generic_write, 0, null, open_existing, file_attribute_normal, null); If (invalid_handle_value = hdev) {printf ("invalid device handle \ r \ n"); System ("pause"); Return-1;} uchar Buf [max_path] = {0 }; DWORD dwreaded = 0; If (readfile (hdev, Buf, 20, & dwreaded, null) {for (INT I = 0; I <dwreaded; I ++) {printf ("% 08x \ r \ n", Buf [I]) ;}} elseprintf ("data read failed \ r \ n"); closehandle (hdev ); system ("pause"); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.