Software & Application Miscellaneous: ZwReadFile returns STATUS_INVALID_HANDLE in DriverIOControl

  • anonymous / 205 / Sun, 31 Jan 2010 18:15:00 GMT / Comments (3)
  • I have written a device driver and I have a problem with ZwCreateFile and ZwReadFile.
    ZwReadFile always returns STATUS_INVALID_HANDLE in DriverIOControl.

    The DriveEntry opens a file with ZwCreateFile.

    #pragma alloc_text(INIT,DriverEntry)
    #pragma alloc_text(PAGE,DriverCreateClose)
    #pragma alloc_text(PAGE,DriverIOControl)
    #pragma alloc_text(PAGE,DriverUnload)

    typedef struct _DEVICE_EXTENSION {
    HANDLE filehandle;
    } DEVICE_EXTENSION, *PDEVICE_EXTENSION;

    NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath)
    {
    ...
    NTSTATUS status;
    IO_STATUS_BLOCK info;
    UNICODE_STRING ucfilename;
    OBJECT_ATTRIBUTES objectattributes;
    WCHAR filename[]=L"\\'\\L:\\test.dat";

    DbgPrint("DriveEntry:\n");
    ...

    status=IoCreateDevice(DriverObject,sizeof(DEVICE_EXTENSION),&ucdevicename,FI LE_DEVICE_UNKNOWN,0,false,&deviceobject);
    ...
    status=IoCreateSymbolicLink(&ucdevicelink,&ucdevicename);
    ...
    deviceextension=(PDEVICE_EXTENSION) deviceobject->DeviceExtension;
    deviceextension->filehandle=NULL;

    deviceobject->Flags |= DO_DIRECT_IO;
    ...
    RtlInitUnicodeString(&ucfilename,filename);

    InitializeObjectAttributes(&objectattributes,&ucfilename,OBJ_CASE_INSENSITIV E,NULL,NULL);

    status=ZwCreateFile(&deviceextension->filehandle,GENERIC_READ,&objectattribu tes,&info, NULL, 0, FILE_SHARE_READ, FILE_OPEN,FILE_SYNCHRONOUS_IO_NONALERT,NULL,0);
    ...
    DriverObject->MajorFunction[IRP_MJ_CREATE] = DriverCreateClose;
    DriverObject->MajorFunction[IRP_MJ_CLOSE] = DriverCreateClose;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverIOControl;
    DriverObject->DriverUnload = DriverUnload;

    return STATUS_SUCCESS;
    }

    NTSTATUS DriverCreateClose(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
    {
    DbgPrint("DriveCreateClose:\n");

    Irp->IoStatus.Status=STATUS_SUCCESS;
    Irp->IoStatus.Information=0;
    IoCompleteRequest(Irp,IO_NO_INCREMENT);

    return STATUS_SUCCESS;
    }

    VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
    {
    PDEVICE_EXTENSION deviceextension;
    PDEVICE_OBJECT deviceobject;

    DbgPrint("DriveUnload:\n");

    deviceobject=DriverObject->DeviceObject;
    deviceextension=(PDEVICE_EXTENSION) deviceobject->DeviceExtension; ZwCloseHandle(deviceextension->filehandle);

    return;
    }

    In DriverIOControl I have programmed a new IoControlCode.

    #define FILE_DEVICE_FILE 0x8000
    #define IOCTL_FILE_READ CTL_CODE(FILE_DEVICE_FILE,0x800,METHOD_BUFFERED, FILE_READ_ACCESS|FILE_WRITE_ACCESS)

    NTSTATUS DriverIOControl(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
    {
    PDEVICE_EXTENSION deviceextension;
    PIO_STACK_LOCATION iostack;
    IO_STATUS_BLOCK stat;
    LARGE_INTEGER ofs;
    NTSTATUS status;

    char rbuf[20];

    DbgPrint("DriverIOControl:\n");

    deviceextension=(PDEVICE_EXTENSION) DeviceObject->DeviceExtension; iostack=IoGetCurrentIrpStackLocation(Irp);

    switch (iostack->Parameters.DeviceIoControl.IoControlCode)
    {

    case IOCTL_FILE_READ:
    {
    ofs.QuadPart=0;

    status=ZwReadFile(deviceextension->filehandle,NULL,NULL,NULL, &stat,&rbuf[0],10,&ofs,NULL);
    if (status==STATUS_SUCCESS) {
    DbgPrint(" INFO: data readed\n");

    status=STATUS_SUCCESS;

    Irp->IoStatus.Information=0;

    }
    else if (status==STATUS_INVALID_HANDLE)
    DbgPrint(" ERROR: status_invalid_handle\n");
    else DbgPrint(" ERROR: other error\n");
    }
    break;
    default:
    {
    DbgPrint(" ERROR: invalid device request\n");
    status=INVALID_DEVICE_REQUEST;
    }
    break;
    }

    Irp->IoStatus.Status=status;
    Irp->IoStatus.Information=0;
    IoCompleteRequest(Irp,IO_NO_INCREMENT);

    return status;
    }

    In my user program I use CreateFile to connect to the driver and DeviceIoControl to send a IOCTL message.

    HANDLE driver;
    DWORD dwSize=0;

    ...
    driver=CreateFile("\\\\.\\mydriver",GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
    OPEN_EXISTING,
    FILE_FLAG_NO_BUFFERING, NULL);
    ...
    ret=DeviceIoControl(driver,IOCTL_VDISK_READ,NULL,0,NULL,dwSize,&dwSize,NULL)
    ;
    ...
    CloseHandle(driver);

    ZwReadFile returns STATUS_INVALID_HANDLE all the time. But the filehandle of the driverfile (L:\test.dat) is not closed. The driverfile is big enough (10MB).

    The program Handle shows me that the driverfilehandle is allocated to the system.

    Handle v2.10
    Copyright (C) 1997-2003 Mark Russinovich
    Sysinternals - www.sysinternals.com
    System pid: 4 L:\test.dat
    If I open a file in DriverIOControl the file will allocate to the user
    program.

    Handle v2.10
    Copyright (C) 1997-2003 Mark Russinovich
    Sysinternals - www.sysinternals.com

    control.exe pid: 252 L:\test.dat

    This handle is only available for this user program. After a new start of the program I have to create a new handle.

    It seems that I have no access to the system handle in DriverIOControl.

    How can I fix this problem without open the driverfile every IOCTL_VDISK_READ message?
  • Keywords:

    zwreadfile, returns, status_invalid_handle, driveriocontrol, software, application

  • http://software.itags.org/software-application/240751/«« Last Thread - Next Thread »»
    1. How many times has the question been asked and answered? Care to try
      for a guess? Read about 'context'.

      Why use an Ioctl to do a read? There are read and write requests
      defined already. They have specific access checks with standard
      implementation rules and trying to reinvent the wheel is not a good use
      of time and resources. I don't see a buffer for the Ioctl read to
      provide data.

      "Steffen" <anonym...anonymnospam.com> wrote in message
      news:%2308C%23Pj0DHA.1576...TK2MSFTNGP11.phx.gbl...
      > I have written a device driver and I have a problem with ZwCreateFile
      and
      > ZwReadFile.
      > ZwReadFile always returns STATUS_INVALID_HANDLE in DriverIOControl.
      >
      > The DriveEntry opens a file with ZwCreateFile.
      > #pragma alloc_text(INIT,DriverEntry)
      > #pragma alloc_text(PAGE,DriverCreateClose)
      > #pragma alloc_text(PAGE,DriverIOControl)
      > #pragma alloc_text(PAGE,DriverUnload)
      >
      > typedef struct _DEVICE_EXTENSION {
      > HANDLE filehandle;
      > } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
      >
      > NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING
      > RegistryPath)
      > {
      > ...
      > NTSTATUS status;
      > IO_STATUS_BLOCK info;
      > UNICODE_STRING ucfilename;
      > OBJECT_ATTRIBUTES objectattributes;
      > WCHAR filename[]=L"\\'\\L:\\test.dat";
      > DbgPrint("DriveEntry:\n");
      > ...
      >
      status=IoCreateDevice(DriverObject,sizeof(DEVICE_EXTENSION),&ucdevicenam
      e,FI
      > LE_DEVICE_UNKNOWN,0,false,&deviceobject);
      > ...
      > status=IoCreateSymbolicLink(&ucdevicelink,&ucdevicename);
      > ...
      > deviceextension=(PDEVICE_EXTENSION) deviceobject->DeviceExtension;
      > deviceextension->filehandle=NULL;
      > deviceobject->Flags |= DO_DIRECT_IO;
      > ...
      > RtlInitUnicodeString(&ucfilename,filename);
      >
      InitializeObjectAttributes(&objectattributes,&ucfilename,OBJ_CASE_INSENS
      ITIV
      > E,NULL,NULL);
      >
      status=ZwCreateFile(&deviceextension->filehandle,GENERIC_READ,&objectatt
      ribu
      > tes,&info,
      > NULL, 0, FILE_SHARE_READ,
      > FILE_OPEN,FILE_SYNCHRONOUS_IO_NONALERT,NULL,0);
      > ...
      > DriverObject->MajorFunction[IRP_MJ_CREATE] =DriverCreateClose;
      > DriverObject->MajorFunction[IRP_MJ_CLOSE] =DriverCreateClose;
      > DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverIOControl;
      > DriverObject->DriverUnload = DriverUnload;
      > return STATUS_SUCCESS;
      > }
      > NTSTATUS DriverCreateClose(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
      > {
      > DbgPrint("DriveCreateClose:\n");
      > Irp->IoStatus.Status=STATUS_SUCCESS;
      > Irp->IoStatus.Information=0;
      > IoCompleteRequest(Irp,IO_NO_INCREMENT);
      > return STATUS_SUCCESS;
      > }
      > VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
      > {
      > PDEVICE_EXTENSION deviceextension;
      > PDEVICE_OBJECT deviceobject;
      > DbgPrint("DriveUnload:\n");
      > deviceobject=DriverObject->DeviceObject;
      > deviceextension=(PDEVICE_EXTENSION) deviceobject->DeviceExtension;
      > ZwCloseHandle(deviceextension->filehandle);
      > return;
      > }
      >
      > In DriverIOControl I have programmed a new IoControlCode.
      > #define FILE_DEVICE_FILE 0x8000
      > #define IOCTL_FILE_READ
      CTL_CODE(FILE_DEVICE_FILE,0x800,METHOD_BUFFERED,
      > FILE_READ_ACCESS|FILE_WRITE_ACCESS)
      > NTSTATUS DriverIOControl(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
      > {
      > PDEVICE_EXTENSION deviceextension;
      > PIO_STACK_LOCATION iostack;
      > IO_STATUS_BLOCK stat;
      > LARGE_INTEGER ofs;
      > NTSTATUS status;
      > char rbuf[20];
      >
      > DbgPrint("DriverIOControl:\n");
      > deviceextension=(PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
      > iostack=IoGetCurrentIrpStackLocation(Irp);
      > switch (iostack->Parameters.DeviceIoControl.IoControlCode)
      > {
      > case IOCTL_FILE_READ:
      > {
      > ofs.QuadPart=0;
      > status=ZwReadFile(deviceextension->filehandle,NULL,NULL,NULL,
      > &stat,&rbuf[0],10,&ofs,NULL);
      > if (status==STATUS_SUCCESS) {
      > DbgPrint(" INFO: data readed\n");
      > status=STATUS_SUCCESS;
      > Irp->IoStatus.Information=0;
      > }
      > else if (status==STATUS_INVALID_HANDLE)
      > DbgPrint(" ERROR: status_invalid_handle\n");
      > else DbgPrint(" ERROR: other
      error\n");
      > }
      > break;
      > default:
      > {
      > DbgPrint(" ERROR: invalid device
      request\n");
      > status=INVALID_DEVICE_REQUEST;
      > }
      > break;
      > }
      >
      > Irp->IoStatus.Status=status;
      > Irp->IoStatus.Information=0;
      > IoCompleteRequest(Irp,IO_NO_INCREMENT);
      > return status;
      > }
      >
      > In my user program I use CreateFile to connect to the driver and
      > DeviceIoControl
      > to send a IOCTL message.
      > HANDLE driver;
      > DWORD dwSize=0;
      > ...
      > driver=CreateFile("\\\\.\\mydriver",GENERIC_READ | GENERIC_WRITE,
      > FILE_SHARE_READ | FILE_SHARE_WRITE,
      NULL,
      > OPEN_EXISTING,
      > FILE_FLAG_NO_BUFFERING, NULL);
      > ...
      >
      ret=DeviceIoControl(driver,IOCTL_VDISK_READ,NULL,0,NULL,dwSize,&dwSize,N
      ULL)
      > ;
      > ...
      > CloseHandle(driver);
      >
      > ZwReadFile returns STATUS_INVALID_HANDLE all the time. But the
      filehandle of
      > the
      > driverfile (L:\test.dat) is not closed. The driverfile is big enough
      (10MB).
      >
      > The program Handle shows me that the driverfilehandle is allocated to
      the
      > system.
      > Handle v2.10
      > Copyright (C) 1997-2003 Mark Russinovich
      > Sysinternals - www.sysinternals.com
      > System pid: 4 L:\test.dat
      >
      > If I open a file in DriverIOControl the file will allocate to the user
      > program.
      > Handle v2.10
      > Copyright (C) 1997-2003 Mark Russinovich
      > Sysinternals - www.sysinternals.com
      > control.exe pid: 252 L:\test.dat
      > This handle is only available for this user program. After a new start
      of
      > the program
      > I have to create a new handle.
      >
      > It seems that I have no access to the system handle in
      DriverIOControl.
      >
      > How can I fix this problem without open the driverfile every
      > IOCTL_VDISK_READ message?
      >

      david | Tues, 20 May 2008 08:09:00 GMT |

    2. Use OBJ_KERNEL_HANDLE flag (read InitizlizeObjectAttributes documentation).

      "Steffen" <anonym...anonymnospam.com> wrote in message
      news:%2308C%23Pj0DHA.1576...TK2MSFTNGP11.phx.gbl...
      > I have written a device driver and I have a problem with ZwCreateFile and
      > ZwReadFile.
      > ZwReadFile always returns STATUS_INVALID_HANDLE in DriverIOControl.
      >
      > The DriveEntry opens a file with ZwCreateFile.
      > #pragma alloc_text(INIT,DriverEntry)
      > #pragma alloc_text(PAGE,DriverCreateClose)
      > #pragma alloc_text(PAGE,DriverIOControl)
      > #pragma alloc_text(PAGE,DriverUnload)
      >
      > typedef struct _DEVICE_EXTENSION {
      > HANDLE filehandle;
      > } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
      >
      > NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING
      > RegistryPath)
      > {
      > ...
      > NTSTATUS status;
      > IO_STATUS_BLOCK info;
      > UNICODE_STRING ucfilename;
      > OBJECT_ATTRIBUTES objectattributes;
      > WCHAR filename[]=L"\\'\\L:\\test.dat";
      > DbgPrint("DriveEntry:\n");
      > ...
      >
      status=IoCreateDevice(DriverObject,sizeof(DEVICE_EXTENSION),&ucdevicename,FI
      > LE_DEVICE_UNKNOWN,0,false,&deviceobject);
      > ...
      > status=IoCreateSymbolicLink(&ucdevicelink,&ucdevicename);
      > ...
      > deviceextension=(PDEVICE_EXTENSION) deviceobject->DeviceExtension;
      > deviceextension->filehandle=NULL;
      > deviceobject->Flags |= DO_DIRECT_IO;
      > ...
      > RtlInitUnicodeString(&ucfilename,filename);
      >
      InitializeObjectAttributes(&objectattributes,&ucfilename,OBJ_CASE_INSENSITIV
      > E,NULL,NULL);
      >
      status=ZwCreateFile(&deviceextension->filehandle,GENERIC_READ,&objectattribu
      > tes,&info,
      > NULL, 0, FILE_SHARE_READ,
      > FILE_OPEN,FILE_SYNCHRONOUS_IO_NONALERT,NULL,0);
      > ...
      > DriverObject->MajorFunction[IRP_MJ_CREATE] = DriverCreateClose;
      > DriverObject->MajorFunction[IRP_MJ_CLOSE] = DriverCreateClose;
      > DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverIOControl;
      > DriverObject->DriverUnload = DriverUnload;
      > return STATUS_SUCCESS;
      > }
      > NTSTATUS DriverCreateClose(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
      > {
      > DbgPrint("DriveCreateClose:\n");
      > Irp->IoStatus.Status=STATUS_SUCCESS;
      > Irp->IoStatus.Information=0;
      > IoCompleteRequest(Irp,IO_NO_INCREMENT);
      > return STATUS_SUCCESS;
      > }
      > VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
      > {
      > PDEVICE_EXTENSION deviceextension;
      > PDEVICE_OBJECT deviceobject;
      > DbgPrint("DriveUnload:\n");
      > deviceobject=DriverObject->DeviceObject;
      > deviceextension=(PDEVICE_EXTENSION) deviceobject->DeviceExtension;
      > ZwCloseHandle(deviceextension->filehandle);
      > return;
      > }
      >
      > In DriverIOControl I have programmed a new IoControlCode.
      > #define FILE_DEVICE_FILE 0x8000
      > #define IOCTL_FILE_READ CTL_CODE(FILE_DEVICE_FILE,0x800,METHOD_BUFFERED,
      > FILE_READ_ACCESS|FILE_WRITE_ACCESS)
      > NTSTATUS DriverIOControl(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
      > {
      > PDEVICE_EXTENSION deviceextension;
      > PIO_STACK_LOCATION iostack;
      > IO_STATUS_BLOCK stat;
      > LARGE_INTEGER ofs;
      > NTSTATUS status;
      > char rbuf[20];
      >
      > DbgPrint("DriverIOControl:\n");
      > deviceextension=(PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
      > iostack=IoGetCurrentIrpStackLocation(Irp);
      > switch (iostack->Parameters.DeviceIoControl.IoControlCode)
      > {
      > case IOCTL_FILE_READ:
      > {
      > ofs.QuadPart=0;
      > status=ZwReadFile(deviceextension->filehandle,NULL,NULL,NULL,
      > &stat,&rbuf[0],10,&ofs,NULL);
      > if (status==STATUS_SUCCESS) {
      > DbgPrint(" INFO: data readed\n");
      > status=STATUS_SUCCESS;
      > Irp->IoStatus.Information=0;
      > }
      > else if (status==STATUS_INVALID_HANDLE)
      > DbgPrint(" ERROR: status_invalid_handle\n");
      > else DbgPrint(" ERROR: other
      error\n");
      > }
      > break;
      > default:
      > {
      > DbgPrint(" ERROR: invalid device request\n");
      > status=INVALID_DEVICE_REQUEST;
      > }
      > break;
      > }
      >
      > Irp->IoStatus.Status=status;
      > Irp->IoStatus.Information=0;
      > IoCompleteRequest(Irp,IO_NO_INCREMENT);
      > return status;
      > }
      >
      > In my user program I use CreateFile to connect to the driver and
      > DeviceIoControl
      > to send a IOCTL message.
      > HANDLE driver;
      > DWORD dwSize=0;
      > ...
      > driver=CreateFile("\\\\.\\mydriver",GENERIC_READ | GENERIC_WRITE,
      > FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
      > OPEN_EXISTING,
      > FILE_FLAG_NO_BUFFERING, NULL);
      > ...
      >
      ret=DeviceIoControl(driver,IOCTL_VDISK_READ,NULL,0,NULL,dwSize,&dwSize,NULL)
      > ;
      > ...
      > CloseHandle(driver);
      >
      > ZwReadFile returns STATUS_INVALID_HANDLE all the time. But the filehandle
      of
      > the
      > driverfile (L:\test.dat) is not closed. The driverfile is big enough
      (10MB).
      >
      > The program Handle shows me that the driverfilehandle is allocated to the
      > system.
      > Handle v2.10
      > Copyright (C) 1997-2003 Mark Russinovich
      > Sysinternals - www.sysinternals.com
      > System pid: 4 L:\test.dat
      >
      > If I open a file in DriverIOControl the file will allocate to the user
      > program.
      > Handle v2.10
      > Copyright (C) 1997-2003 Mark Russinovich
      > Sysinternals - www.sysinternals.com
      > control.exe pid: 252 L:\test.dat
      > This handle is only available for this user program. After a new start of
      > the program
      > I have to create a new handle.
      >
      > It seems that I have no access to the system handle in DriverIOControl.
      >
      > How can I fix this problem without open the driverfile every
      > IOCTL_VDISK_READ message?
      >

      alexander | Tues, 20 May 2008 08:10:00 GMT |

    3. File handle must be used within the same process context as it was openned
      in. If you are creating a file handle inside system process, but using it
      in DeviceIoCtrl that is running in a user application process context, then
      this handle value has no meaning inside user process. :(

      Cheers,

      "Steffen" <anonym...anonymnospam.com> wrote in message
      news:%2308C%23Pj0DHA.1576...TK2MSFTNGP11.phx.gbl...
      > I have written a device driver and I have a problem with ZwCreateFile and
      > ZwReadFile.
      > ZwReadFile always returns STATUS_INVALID_HANDLE in DriverIOControl.
      >
      > The DriveEntry opens a file with ZwCreateFile.
      > #pragma alloc_text(INIT,DriverEntry)
      > #pragma alloc_text(PAGE,DriverCreateClose)
      > #pragma alloc_text(PAGE,DriverIOControl)
      > #pragma alloc_text(PAGE,DriverUnload)
      >
      > typedef struct _DEVICE_EXTENSION {
      > HANDLE filehandle;
      > } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
      >
      > NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING
      > RegistryPath)
      > {
      > ...
      > NTSTATUS status;
      > IO_STATUS_BLOCK info;
      > UNICODE_STRING ucfilename;
      > OBJECT_ATTRIBUTES objectattributes;
      > WCHAR filename[]=L"\\'\\L:\\test.dat";
      > DbgPrint("DriveEntry:\n");
      > ...
      >
      status=IoCreateDevice(DriverObject,sizeof(DEVICE_EXTENSION),&ucdevicename,FI
      > LE_DEVICE_UNKNOWN,0,false,&deviceobject);
      > ...
      > status=IoCreateSymbolicLink(&ucdevicelink,&ucdevicename);
      > ...
      > deviceextension=(PDEVICE_EXTENSION) deviceobject->DeviceExtension;
      > deviceextension->filehandle=NULL;
      > deviceobject->Flags |= DO_DIRECT_IO;
      > ...
      > RtlInitUnicodeString(&ucfilename,filename);
      >
      InitializeObjectAttributes(&objectattributes,&ucfilename,OBJ_CASE_INSENSITIV
      > E,NULL,NULL);
      >
      status=ZwCreateFile(&deviceextension->filehandle,GENERIC_READ,&objectattribu
      > tes,&info,
      > NULL, 0, FILE_SHARE_READ,
      > FILE_OPEN,FILE_SYNCHRONOUS_IO_NONALERT,NULL,0);
      > ...
      > DriverObject->MajorFunction[IRP_MJ_CREATE] = DriverCreateClose;
      > DriverObject->MajorFunction[IRP_MJ_CLOSE] = DriverCreateClose;
      > DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverIOControl;
      > DriverObject->DriverUnload = DriverUnload;
      > return STATUS_SUCCESS;
      > }
      > NTSTATUS DriverCreateClose(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
      > {
      > DbgPrint("DriveCreateClose:\n");
      > Irp->IoStatus.Status=STATUS_SUCCESS;
      > Irp->IoStatus.Information=0;
      > IoCompleteRequest(Irp,IO_NO_INCREMENT);
      > return STATUS_SUCCESS;
      > }
      > VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
      > {
      > PDEVICE_EXTENSION deviceextension;
      > PDEVICE_OBJECT deviceobject;
      > DbgPrint("DriveUnload:\n");
      > deviceobject=DriverObject->DeviceObject;
      > deviceextension=(PDEVICE_EXTENSION) deviceobject->DeviceExtension;
      > ZwCloseHandle(deviceextension->filehandle);
      > return;
      > }
      >
      > In DriverIOControl I have programmed a new IoControlCode.
      > #define FILE_DEVICE_FILE 0x8000
      > #define IOCTL_FILE_READ CTL_CODE(FILE_DEVICE_FILE,0x800,METHOD_BUFFERED,
      > FILE_READ_ACCESS|FILE_WRITE_ACCESS)
      > NTSTATUS DriverIOControl(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
      > {
      > PDEVICE_EXTENSION deviceextension;
      > PIO_STACK_LOCATION iostack;
      > IO_STATUS_BLOCK stat;
      > LARGE_INTEGER ofs;
      > NTSTATUS status;
      > char rbuf[20];
      >
      > DbgPrint("DriverIOControl:\n");
      > deviceextension=(PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
      > iostack=IoGetCurrentIrpStackLocation(Irp);
      > switch (iostack->Parameters.DeviceIoControl.IoControlCode)
      > {
      > case IOCTL_FILE_READ:
      > {
      > ofs.QuadPart=0;
      > status=ZwReadFile(deviceextension->filehandle,NULL,NULL,NULL,
      > &stat,&rbuf[0],10,&ofs,NULL);
      > if (status==STATUS_SUCCESS) {
      > DbgPrint(" INFO: data readed\n");
      > status=STATUS_SUCCESS;
      > Irp->IoStatus.Information=0;
      > }
      > else if (status==STATUS_INVALID_HANDLE)
      > DbgPrint(" ERROR: status_invalid_handle\n");
      > else DbgPrint(" ERROR: other
      error\n");
      > }
      > break;
      > default:
      > {
      > DbgPrint(" ERROR: invalid device request\n");
      > status=INVALID_DEVICE_REQUEST;
      > }
      > break;
      > }
      >
      > Irp->IoStatus.Status=status;
      > Irp->IoStatus.Information=0;
      > IoCompleteRequest(Irp,IO_NO_INCREMENT);
      > return status;
      > }
      >
      > In my user program I use CreateFile to connect to the driver and
      > DeviceIoControl
      > to send a IOCTL message.
      > HANDLE driver;
      > DWORD dwSize=0;
      > ...
      > driver=CreateFile("\\\\.\\mydriver",GENERIC_READ | GENERIC_WRITE,
      > FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
      > OPEN_EXISTING,
      > FILE_FLAG_NO_BUFFERING, NULL);
      > ...
      >
      ret=DeviceIoControl(driver,IOCTL_VDISK_READ,NULL,0,NULL,dwSize,&dwSize,NULL)
      > ;
      > ...
      > CloseHandle(driver);
      >
      > ZwReadFile returns STATUS_INVALID_HANDLE all the time. But the filehandle
      of
      > the
      > driverfile (L:\test.dat) is not closed. The driverfile is big enough
      (10MB).
      >
      > The program Handle shows me that the driverfilehandle is allocated to the
      > system.
      > Handle v2.10
      > Copyright (C) 1997-2003 Mark Russinovich
      > Sysinternals - www.sysinternals.com
      > System pid: 4 L:\test.dat
      >
      > If I open a file in DriverIOControl the file will allocate to the user
      > program.
      > Handle v2.10
      > Copyright (C) 1997-2003 Mark Russinovich
      > Sysinternals - www.sysinternals.com
      > control.exe pid: 252 L:\test.dat
      > This handle is only available for this user program. After a new start of
      > the program
      > I have to create a new handle.
      >
      > It seems that I have no access to the system handle in DriverIOControl.
      >
      > How can I fix this problem without open the driverfile every
      > IOCTL_VDISK_READ message?
      >

      paul | Tues, 20 May 2008 08:11:00 GMT |

  • Software & Application Miscellaneous Questions

    • Zwiki 0.41 released, news for april

      Seems I'm never going to finish this just *post* it!*What is it ?*Zwiki is a Zope product for b...

      By simonmichael

    • How can I do?

      Thomas Bohl schrieb:> I have the impression that since some time (since Vers. 5.0?) N[expr] does&...

      By peter_pein

    • ZwCreateFile failing

      Hi, I have a routine which open the file handle for me. It works fine as long as no one touches the ...

      By anonymous

    • How can I do?

      Hello Bruce,Thanks a lot for your explanation! I got it.I should have read the known BUGs before:)Ma...

      By wyel_en

    • "virtual" serial port driver on Windows XP

      For our own remote serial server equipment which is remotely connected via Ethernet TCP/IP, we need ...

      By anonymous, 11 Comments

    • ZwMapViewOfSection

      InitializeObjectAttributes(&ObjAttributes,NULL,OBJ_KERNEL_HANDLE,NULL,NULL); NtStatus = ZwCreateSect...

      By anonymous, 2 Comments

    • "Pop-ups" Anyone?

      My problem lies on these dang "Pop-ups", lately as soon as we open our browser "IE 6", we are swampe...

      By wogster, 12 Comments

    • "Autorun" when inserting SD card

      Hello, Is there any "autorun" mechanism when inserting a SD Card ? I would like to launch an applica...

      By anonymous, 6 Comments

    • "Search" won't work in "Start"

      When I click on Search in the Start menu, I am unable to do a search. A window pops up with a toolba...

      By etabe, 2 Comments