Example #1
0
        public StorageDeviceNumber GetDeviceNumber(SafeHandle hDevice)
        {
            StorageDeviceNumber deviceNumber = new StorageDeviceNumber();
            SafeAllocHandle <STORAGE_DEVICE_NUMBER> storageDeviceNumberPtr = null;

            try {
                storageDeviceNumberPtr = new SafeAllocHandle <STORAGE_DEVICE_NUMBER>();
                bool success = DeviceIoControl(hDevice, IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0,
                                               storageDeviceNumberPtr, storageDeviceNumberPtr.SizeOf, out uint bytesReturns, IntPtr.Zero);
                if (!success || bytesReturns == 0)
                {
                    m_Win32Error = Marshal.GetLastWin32Error();
                    return(null);
                }
                STORAGE_DEVICE_NUMBER storageDeviceNumber = storageDeviceNumberPtr.ToStructure();
                deviceNumber.DeviceType      = storageDeviceNumber.DeviceType;
                deviceNumber.DeviceNumber    = storageDeviceNumber.DeviceNumber;
                deviceNumber.PartitionNumber = storageDeviceNumber.PartitionNumber;
            } finally {
                if (storageDeviceNumberPtr != null)
                {
                    storageDeviceNumberPtr.Close();
                }
            }
            return(deviceNumber);
        }
        /// <summary>
        /// Determins the device number
        /// </summary>
        /// <param name="devicePath">Device path</param>
        /// <returns>The Device number</returns>
        private static int GetDeviceNumber(string devicePath)
        {
            int    ans = -1;
            IntPtr h   = CreateFile(devicePath, 0, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

            if (h.ToInt32() != INVALID_HANDLE_VALUE)
            {
                int requiredSize;
                STORAGE_DEVICE_NUMBER Sdn = new STORAGE_DEVICE_NUMBER();
                int    nBytes             = Marshal.SizeOf(Sdn);
                IntPtr ptrSdn             = Marshal.AllocHGlobal(nBytes);

                if (DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, null, 0, ptrSdn, nBytes, out requiredSize,
                                    IntPtr.Zero))
                {
                    Sdn = (STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(ptrSdn, typeof(STORAGE_DEVICE_NUMBER));
                    // just my way of combining the relevant parts of the
                    // STORAGE_DEVICE_NUMBER into a single number
                    ans = (Sdn.DeviceType << 8) + Sdn.DeviceNumber;
                }
                Marshal.FreeHGlobal(ptrSdn);
                CloseHandle(h);
            }
            return(ans);
        }
Example #3
0
        static long GetDeviceNumber(IntPtr handle)
        {
            // get the volume's device number
            long   DeviceNumber  = -1;
            int    size          = 0x400; // some big size
            IntPtr buffer        = Marshal.AllocHGlobal(size);
            int    bytesReturned = 0;

            try
            {
                DeviceIoControl(handle, IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, buffer, size, out bytesReturned, IntPtr.Zero);
            }
            finally
            {
                CloseHandle(handle);
            }

            if (bytesReturned > 0)
            {
                STORAGE_DEVICE_NUMBER sdn = (STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(buffer, typeof(STORAGE_DEVICE_NUMBER));
                DeviceNumber = sdn.DeviceNumber;
            }
            Marshal.FreeHGlobal(buffer);

            return(DeviceNumber);
        }
Example #4
0
        public static string GetRemovableDriveDeviceId(DriveType driveType, STORAGE_DEVICE_NUMBER deviceNumber)
        {
            string deviceId = "";

            var foundItem = GetRemovableDriveDeviceIds(driveType, deviceNumber.DeviceType).Find(x => x.StorageDeviceNumber.DeviceNumber == deviceNumber.DeviceNumber);

            if (!default(StorageDeviceInfo).Equals(foundItem))
            {
                deviceId = foundItem.InstanceID;
            }

            return(deviceId);
        }
Example #5
0
        public static (uint DeviceType, uint DeviceNumber) QueryDeviceNumber(SafeHandle handle)
        {
            STORAGE_DEVICE_NUMBER deviceNumber = default;
            uint bytesReturned = 0;

            if (NativeMethods.DeviceIoControl(handle, StorageCommand.IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, ref deviceNumber,
                                              Marshal.SizeOf <STORAGE_DEVICE_NUMBER>(), out bytesReturned, IntPtr.Zero))
            {
                return(deviceNumber.DeviceType, deviceNumber.DeviceNumber);
            }

            return(UInt32.MaxValue, UInt32.MaxValue);
        }
Example #6
0
        public static uint GetDiskDeviceNumber(SafeFileHandle deviceHandle)
        {
            var storageDeviceNumber = new STORAGE_DEVICE_NUMBER();

            if (!IoctlStorageGetDeviceNumber(deviceHandle, storageDeviceNumber))
            {
                throw new Win32Exception();
            }

            if (storageDeviceNumber.deviceType != DeviceType.FILE_DEVICE_DISK)
            {
                throw new Exception($"Expected FILE_DEVICE_DISK. Got '{storageDeviceNumber.deviceType}'.");
            }

            return(storageDeviceNumber.deviceNumber);
        }
Example #7
0
        public static string GetDeviceFromDrive(System.IO.DriveInfo driveInfo)
        {
            IntPtr         pStorageDeviceNumber = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(STORAGE_DEVICE_NUMBER)));
            SafeFileHandle hDrive = null;

            try {
                string driveName = "\\\\.\\" + driveInfo.ToString().Substring(0, 2);
                hDrive = ApiFunctions.CreateFile(driveName, AccessMask.GENERIC_READ,
                                                 System.IO.FileShare.ReadWrite, 0, System.IO.FileMode.Open, 0, IntPtr.Zero);

                if (hDrive.IsInvalid)
                {
                    throw new FileLoadException("Drive handle invalid");
                }

                bool status;
                int  retByte;
                System.Threading.NativeOverlapped nativeOverlap = new System.Threading.NativeOverlapped();
                status = ApiFunctions.DeviceIoControl(hDrive, DeviceIOControlCode.StorageGetDeviceNumber, IntPtr.Zero, 0,
                                                      pStorageDeviceNumber, Marshal.SizeOf(typeof(STORAGE_DEVICE_NUMBER)), out retByte, ref nativeOverlap);

                if (!status)
                {
                    throw new FileLoadException("DeviceIoControl error");
                }

                STORAGE_DEVICE_NUMBER storDevNum = (STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(pStorageDeviceNumber, typeof(STORAGE_DEVICE_NUMBER));

                return("\\\\.\\PhysicalDrive" + storDevNum.DeviceNumber);
            }
            finally {
                Marshal.FreeHGlobal(pStorageDeviceNumber);
                if (hDrive != null)
                {
                    hDrive.Close();
                }
            }
        }
    /// <summary>
    /// Determins the device number 
    /// </summary>
    /// <param name="devicePath">Device path</param>
    /// <returns>The Device number</returns>
    private static int GetDeviceNumber(string devicePath)
    {
      int ans = -1;
      IntPtr h = CreateFile(devicePath, 0, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
      if (h.ToInt32() != INVALID_HANDLE_VALUE)
      {
        int requiredSize;
        STORAGE_DEVICE_NUMBER Sdn = new STORAGE_DEVICE_NUMBER();
        int nBytes = Marshal.SizeOf(Sdn);
        IntPtr ptrSdn = Marshal.AllocHGlobal(nBytes);

        if (DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, null, 0, ptrSdn, nBytes, out requiredSize,
                            IntPtr.Zero))
        {
          Sdn = (STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(ptrSdn, typeof (STORAGE_DEVICE_NUMBER));
          // just my way of combining the relevant parts of the
          // STORAGE_DEVICE_NUMBER into a single number
          ans = (Sdn.DeviceType << 8) + Sdn.DeviceNumber;
        }
        Marshal.FreeHGlobal(ptrSdn);
        CloseHandle(h);
      }
      return ans;
    }
Example #9
0
 public static extern bool DeviceIoControl(
     SafeHandle handle, StorageCommand command,
     IntPtr inData, int querySize,
     ref STORAGE_DEVICE_NUMBER outData, int outDataSize,
     out uint bytesReturned, IntPtr overlapped);
Example #10
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="rootPath">The volume's drive letter (for example, X:\) or the path of a mounted folder
 /// that is associated with the volume.</param>
 /// <param name="label">The label of a file system volume.</param>
 /// <param name="deviceInfo">Information about the device.</param>
 public VolumeInfo(string rootPath, string label, STORAGE_DEVICE_NUMBER deviceInfo)
 {
     RootPath = rootPath;
     Label = label;
     DeviceInfo = deviceInfo;
 }
Example #11
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="rootPath">The volume's drive letter (for example, X:\) or the path of a mounted folder
 /// that is associated with the volume.</param>
 /// <param name="label">The label of a file system volume.</param>
 /// <param name="deviceInfo">Information about the device.</param>
 public VolumeInfo(string rootPath, string label, STORAGE_DEVICE_NUMBER deviceInfo)
 {
     RootPath   = rootPath;
     Label      = label;
     DeviceInfo = deviceInfo;
 }
Example #12
0
 public static extern bool DeviceIoControl(IntPtr deviceHandle, IO_CONTROL_CODE controlCode, IntPtr inBuffer, uint inBufferSize, ref STORAGE_DEVICE_NUMBER outBuffer, uint outBufferSize, ref uint bytesReturned, IntPtr overlapped);
Example #13
0
 public static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, ref STORAGE_DEVICE_NUMBER lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped);
Example #14
0
 static extern bool IoctlStorageGetDeviceNumber(SafeFileHandle handle, STORAGE_DEVICE_NUMBER storageDeviceNumber);