public IntPtr OpenDrive(int driveNumber)
 {
     return(SystemCall.CreateFile(@"\\.\PhysicalDrive" + driveNumber,
                                  AccessMode.Read | AccessMode.Write, ShareMode.Read | ShareMode.Write,
                                  IntPtr.Zero, CreationMode.OpenExisting, FileAttribute.Device,
                                  IntPtr.Zero));
 }
        public bool ReadNameAndFirmwareRevision(IntPtr handle, int driveNumber,
                                                out string name, out string firmwareRevision)
        {
            var parameter = new DriveCommandParameter
            {
                DriveNumber = (byte)driveNumber,
                Registers   = { Command = RegisterCommand.IdCmd }
            };


            var valid = SystemCall.DeviceIoControl(handle,
                                                   DriveCommand.ReceiveDriveData, ref parameter, Marshal.SizeOf(parameter),
                                                   out DriveIdentifyResult result, Marshal.SizeOf(typeof(DriveIdentifyResult)),
                                                   out _, IntPtr.Zero);

            if (!valid)
            {
                name             = null;
                firmwareRevision = null;
                return(false);
            }

            name             = GetString(result.Identify.ModelNumber);
            firmwareRevision = GetString(result.Identify.FirmwareRevision);
            return(true);
        }
        public bool EnableSmart(IntPtr handle, int driveNumber)
        {
            var parameter = new DriveCommandParameter
            {
                DriveNumber = (byte)driveNumber,
                Registers   =
                {
                    Features = RegisterFeature.SmartEnableOperations,
                    LBAMid   = SMART_LBA_MID,
                    LBAHigh  = SMART_LBA_HI,
                    Command  = RegisterCommand.SmartCmd
                }
            };


            return(SystemCall.DeviceIoControl(handle, DriveCommand.SendDriveCommand,
                                              ref parameter, Marshal.SizeOf(typeof(DriveCommandParameter)), out DriveCommandResult _,
                                              Marshal.SizeOf(typeof(DriveCommandResult)), out _,
                                              IntPtr.Zero));
        }
        public DriveAttributeValue[] ReadSmartData(IntPtr handle, int driveNumber)
        {
            var parameter = new DriveCommandParameter
            {
                DriveNumber = (byte)driveNumber,
                Registers   =
                {
                    Features = RegisterFeature.SmartReadData,
                    LBAMid   = SMART_LBA_MID,
                    LBAHigh  = SMART_LBA_HI,
                    Command  = RegisterCommand.SmartCmd
                }
            };


            var isValid = SystemCall.DeviceIoControl(handle,
                                                     DriveCommand.ReceiveDriveData, ref parameter, Marshal.SizeOf(parameter),
                                                     out DriveSmartReadDataResult result, Marshal.SizeOf(typeof(DriveSmartReadDataResult)),
                                                     out _, IntPtr.Zero);

            return(isValid ? result.Attributes : new DriveAttributeValue[0]);
        }
 public void CloseHandle(IntPtr handle)
 {
     SystemCall.CloseHandle(handle);
 }