How to get the Device Number in File System Driver RRS feed

  • Question

  • I have created a sample MFC application to find the device number
    following code block used
    hDeviceHandle = CreateFile(strDrivePath,0,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);
    if (hDeviceHandle != (HANDLE)-1)
    DWORD returned;
    if (DeviceIoControl( hDeviceHandle,IOCTL_STORAGE_GET_DEVICE_NUMBER,NULL,0,&sdn,sizeof(sdn),&returned,NULL));
    printf("\tDevice type: %d number: %d partition: %d\n",sdn.DeviceType, sdn.DeviceNumber, sdn.PartitionNumber);

    I Need to find out the Device number in file system driver.Is it Possible find the device number in kernel mode .

    Is there any code sample available  .
    Friday, January 11, 2019 8:30 AM


All replies

  • The first question is what are you trying to with the number?   I notice you are also asked a question about USB serial numbers.   You should be aware that the device number you get is only windows construct that can change with a reboot, not something like the SCSI or ATA ID.   I ask this first since I have had clients who were looking for a hardware ID and thought IOCTL_STORAGE_GET_DEVICE_NUMBER would give it to them.

    Don Burn Windows Driver Consulting Website:

    Friday, January 11, 2019 1:06 PM
  • As I stated in your question about USB serial numbers: if you are in a minifilter you need to use FltGetDiskDeviceObject() to get the device object to issue the call to.   Trying to just call the volume will not connect to physical device.

    Don Burn Windows Driver Consulting Website:

    Friday, January 11, 2019 2:47 PM
  • thank you @Don Burn

    Actually i need to find the serial number of device to set individual permission for devce

    Example Allowing a particular usb device only

    But i fail to find out the serial number for some device in kernal mode .

    so now i trying to map the serial number in user mode  with the device number in kernal mode.

    how can i find the device number in file system driver .Please help

    Monday, January 14, 2019 9:23 AM
  • First, there is not a generic serial number for storage devices.   USB, SCSI and ATA all have different approaches, and in some cases don't have a number.   Then throw in other device types, and things like drive arrays and this becomes a major challenge.   I've mainly done this for SCSI and ATA.    For USB, I recommend you go through the USBview sources to see how it gets the serial number.


    Don Burn Windows Driver Consulting Website:

    Monday, January 14, 2019 12:26 PM