示例#1
0
        public static uint GetPhysicalDriveId(string drive)
        {
            var sdn = new StorageDeviceNumber();

            var driveHandle = CreateFile(@"\\.\" + drive, 0, 0, IntPtr.Zero, (uint)CreationDisposition.OPEN_EXISTING, 0, IntPtr.Zero);

            if (driveHandle.ToInt64() == -1)
            {
                int lastWin32ErrorCode = Marshal.GetLastWin32Error();
                throw new IOException("Failed to CreateFile for Drive : " + drive,
                                      new Win32Exception(lastWin32ErrorCode));
            }
            try
            {
                int requiredSize;
                if (DeviceIoControl(driveHandle,
                                    (int)IoControlCode.IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, new IntPtr(&sdn), sizeof(StorageDeviceNumber),
                                    out requiredSize, IntPtr.Zero) == false)
                {
                    int lastWin32ErrorCode = Marshal.GetLastWin32Error();
                    throw new IOException("Failed to DeviceIoControl for Drive : " + drive,
                                          new Win32Exception(lastWin32ErrorCode));
                }
            }
            finally
            {
                CloseHandle(driveHandle);
            }
            return((uint)((int)sdn.DeviceType << 8) + sdn.DeviceNumber);
        }
示例#2
0
        private void QueryDeviceNumber(XmlElement pathNode, IOSVolumeDeviceInfo vinfo, SafeHandle hDevice)
        {
            StorageDeviceNumber device = QueryApi(pathNode, "StorageDeviceNumber", vinfo, () => {
                return(vinfo.GetDeviceNumber(hDevice));
            }, out XmlElement deviceNode);

            if (device == null)
            {
                return;
            }
            WriteApiResult(deviceNode, "DeviceType", (int)device.DeviceType);
            WriteApiResult(deviceNode, "DeviceNumber", device.DeviceNumber);
            WriteApiResult(deviceNode, "DevicePartition", device.PartitionNumber);
        }
示例#3
0
 internal static int GetDeviceNumber(string devicePath)
 {
     var handle = GetFileHandle(devicePath);
     var ptrSdn = IntPtr.Zero;
     try {
         int requiredSize;
         var sdn = new StorageDeviceNumber();
         var nBytes = Marshal.SizeOf(sdn);
         ptrSdn = Marshal.AllocHGlobal(nBytes);
         if(DeviceIoControl(handle, (int) IOCTL.StorageGetDeviceNumber, IntPtr.Zero, 0, ptrSdn, nBytes, out requiredSize, IntPtr.Zero)) {
             sdn = (StorageDeviceNumber) Marshal.PtrToStructure(ptrSdn, typeof(StorageDeviceNumber));
             return (sdn.deviceType << 8) + sdn.deviceNumber;
         }
         return -1;
     }
     finally {
         Marshal.FreeHGlobal(ptrSdn);
         handle.Close();
     }
 }
示例#4
0
        internal static int GetDeviceNumber(string devicePath)
        {
            var handle = GetFileHandle(devicePath);
            var ptrSdn = IntPtr.Zero;

            try {
                int requiredSize;
                var sdn    = new StorageDeviceNumber();
                var nBytes = Marshal.SizeOf(sdn);
                ptrSdn = Marshal.AllocHGlobal(nBytes);
                if (DeviceIoControl(handle, (int)IOCTL.StorageGetDeviceNumber, IntPtr.Zero, 0, ptrSdn, nBytes, out requiredSize, IntPtr.Zero))
                {
                    sdn = (StorageDeviceNumber)Marshal.PtrToStructure(ptrSdn, typeof(StorageDeviceNumber));
                    return((sdn.deviceType << 8) + sdn.deviceNumber);
                }
                return(-1);
            }
            finally {
                Marshal.FreeHGlobal(ptrSdn);
                handle.Close();
            }
        }
示例#5
0
        private static int GetDeviceNumber(string devicePath)
        {
            var result = -1;

            var h = Kernel32.CreateFile(devicePath.TrimEnd('\\'), default(int), default(int), IntPtr.Zero, UsbConstants.OPEN_EXISTING, default(int), IntPtr.Zero);

            if (h.ToInt32() != UsbConstants.INVALID_HANDLE_VALUE)
            {
                var sdn    = new StorageDeviceNumber();
                var nBytes = Marshal.SizeOf(sdn);
                var ptrSdn = Marshal.AllocHGlobal(nBytes);

                if (Kernel32.DeviceIoControl(h, UsbConstants.IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, default(int), ptrSdn, nBytes, out int requiredSize, IntPtr.Zero))
                {
                    sdn    = (StorageDeviceNumber)Marshal.PtrToStructure(ptrSdn, typeof(StorageDeviceNumber));
                    result = (sdn.DeviceType << 8) + sdn.DeviceNumber;
                }
                Marshal.FreeHGlobal(ptrSdn);
                Kernel32.CloseHandle(h);
            }

            return(result);
        }