示例#1
0
        internal static async Task <MorphicResult <ExtendedPInvoke.STORAGE_DEVICE_NUMBER, StorageDeviceNumberError> > GetStorageDeviceNumberAsync(string devicePath)
        {
            var deviceHandle = PInvoke.Kernel32.CreateFile(devicePath, (PInvoke.Kernel32.ACCESS_MASK) 0, PInvoke.Kernel32.FileShare.FILE_SHARE_READ, IntPtr.Zero, PInvoke.Kernel32.CreationDisposition.OPEN_EXISTING, (PInvoke.Kernel32.CreateFileFlags) 0, PInvoke.Kernel32.SafeObjectHandle.Null);

            if (deviceHandle.IsInvalid == true)
            {
                var win32ErrorCode = Marshal.GetLastWin32Error();
                return(MorphicResult.ErrorResult(StorageDeviceNumberError.Win32Error((uint)win32ErrorCode)));
            }
            //
            // get the device number for this storage device
            var storageDeviceNumber             = new ExtendedPInvoke.STORAGE_DEVICE_NUMBER();
            var storageDeviceNumberMemoryObject = new Memory <ExtendedPInvoke.STORAGE_DEVICE_NUMBER>(new ExtendedPInvoke.STORAGE_DEVICE_NUMBER[] { storageDeviceNumber });

            //
            try
            {
                uint numberOfBytes;
                try
                {
                    numberOfBytes = await PInvoke.Kernel32.DeviceIoControlAsync <byte /* for null */, ExtendedPInvoke.STORAGE_DEVICE_NUMBER>(
                        hDevice : deviceHandle,
                        dwIoControlCode : ExtendedPInvoke.IOCTL_STORAGE_GET_DEVICE_NUMBER,
                        inBuffer : null,
                        outBuffer : storageDeviceNumberMemoryObject,
                        cancellationToken : System.Threading.CancellationToken.None);
                }
                catch (PInvoke.Win32Exception ex)
                {
                    return(MorphicResult.ErrorResult(StorageDeviceNumberError.Win32Error((uint)ex.NativeErrorCode)));
                }

                var storageDeviceNumberAsArray = storageDeviceNumberMemoryObject.ToArray();
                if (storageDeviceNumberAsArray.Length != 1)
                {
                    return(MorphicResult.ErrorResult(StorageDeviceNumberError.CouldNotRetrieveStorageDeviceNumbers));
                }
                storageDeviceNumber = storageDeviceNumberAsArray[0];
            }
            finally
            {
                deviceHandle.Close();
            }

            return(MorphicResult.OkResult(storageDeviceNumber));
        }
示例#2
0
 private Disk(Device device, ExtendedPInvoke.STORAGE_DEVICE_NUMBER storageDeviceNumber)
 {
     _device = device;
     this.StorageDeviceNumber = storageDeviceNumber;
 }