Hardware/HDD/SMART.cs
changeset 167 b7cc9d09aefe
parent 165 813d8bc3192f
child 188 6ce1c13899e1
     1.1 --- a/Hardware/HDD/SMART.cs	Thu Aug 12 20:53:27 2010 +0000
     1.2 +++ b/Hardware/HDD/SMART.cs	Sun Aug 15 14:46:58 2010 +0000
     1.3 @@ -232,37 +232,10 @@
     1.4  
     1.5      private const int MAX_DRIVE_ATTRIBUTES = 512;
     1.6  
     1.7 -    private const string KERNEL = "kernel32.dll";
     1.8 -
     1.9 -    [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
    1.10 -    private static extern IntPtr CreateFile(string fileName, 
    1.11 -      AccessMode desiredAccess, ShareMode shareMode, IntPtr securityAttributes,
    1.12 -      CreationMode creationDisposition, FileAttribute flagsAndAttributes, 
    1.13 -      IntPtr templateFilehandle);
    1.14 -
    1.15 -    [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
    1.16 -    public static extern int CloseHandle(IntPtr handle);
    1.17 -
    1.18 -    [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
    1.19 -    private static extern bool DeviceIoControl(IntPtr handle,
    1.20 -      DriveCommand command, ref DriveCommandParameter parameter,
    1.21 -      int parameterSize, out DriveSmartReadResult result, int resultSize, 
    1.22 -      out uint bytesReturned, IntPtr overlapped);
    1.23 -
    1.24 -    [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
    1.25 -    private static extern bool DeviceIoControl(IntPtr handle,
    1.26 -      DriveCommand command, ref DriveCommandParameter parameter,
    1.27 -      int parameterSize, out DriveCommandResult result, int resultSize,
    1.28 -      out uint bytesReturned, IntPtr overlapped);
    1.29 -
    1.30 -    [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
    1.31 -    private static extern bool DeviceIoControl(IntPtr handle,
    1.32 -      DriveCommand command, ref DriveCommandParameter parameter,
    1.33 -      int parameterSize, out DriveIdentifyResult result, int resultSize,
    1.34 -      out uint bytesReturned, IntPtr overlapped);
    1.35 +    private SMART() { }
    1.36  
    1.37      public static IntPtr OpenPhysicalDrive(int driveNumber) {
    1.38 -      return CreateFile(@"\\.\PhysicalDrive" + driveNumber,
    1.39 +      return NativeMethods.CreateFile(@"\\.\PhysicalDrive" + driveNumber,
    1.40          AccessMode.Read | AccessMode.Write, ShareMode.Read | ShareMode.Write,
    1.41          IntPtr.Zero, CreationMode.OpenExisting, FileAttribute.Device,
    1.42          IntPtr.Zero);
    1.43 @@ -279,8 +252,8 @@
    1.44        parameter.Registers.LBAHigh = SMART_LBA_HI;
    1.45        parameter.Registers.Command = SMART_CMD;
    1.46  
    1.47 -      return DeviceIoControl(handle, DriveCommand.SendDriveCommand, 
    1.48 -        ref parameter, Marshal.SizeOf(parameter), out result,
    1.49 +      return NativeMethods.DeviceIoControl(handle, DriveCommand.SendDriveCommand, 
    1.50 +        ref parameter, Marshal.SizeOf(typeof(DriveCommandParameter)), out result,
    1.51          Marshal.SizeOf(typeof(DriveCommandResult)), out bytesReturned, 
    1.52          IntPtr.Zero);
    1.53      }
    1.54 @@ -296,10 +269,10 @@
    1.55        parameter.Registers.LBAHigh = SMART_LBA_HI;
    1.56        parameter.Registers.Command = SMART_CMD;
    1.57  
    1.58 -      bool valid = DeviceIoControl(handle, DriveCommand.ReceiveDriveData,
    1.59 -        ref parameter, Marshal.SizeOf(parameter), out result,
    1.60 -        Marshal.SizeOf(typeof(DriveSmartReadResult)), out bytesReturned,
    1.61 -        IntPtr.Zero);
    1.62 +      bool valid = NativeMethods.DeviceIoControl(handle, 
    1.63 +        DriveCommand.ReceiveDriveData, ref parameter, Marshal.SizeOf(parameter), 
    1.64 +        out result, Marshal.SizeOf(typeof(DriveSmartReadResult)), 
    1.65 +        out bytesReturned, IntPtr.Zero);
    1.66  
    1.67        if (!valid)
    1.68          return null;
    1.69 @@ -315,10 +288,10 @@
    1.70        parameter.DriveNumber = (byte)driveNumber;
    1.71        parameter.Registers.Command = ID_CMD;
    1.72  
    1.73 -      bool valid = DeviceIoControl(handle, DriveCommand.ReceiveDriveData,
    1.74 -        ref parameter, Marshal.SizeOf(parameter), out result,
    1.75 -        Marshal.SizeOf(typeof(DriveIdentifyResult)), out bytesReturned,
    1.76 -        IntPtr.Zero);
    1.77 +      bool valid = NativeMethods.DeviceIoControl(handle, 
    1.78 +        DriveCommand.ReceiveDriveData, ref parameter, Marshal.SizeOf(parameter), 
    1.79 +        out result, Marshal.SizeOf(typeof(DriveIdentifyResult)), 
    1.80 +        out bytesReturned, IntPtr.Zero);
    1.81  
    1.82        if (!valid)
    1.83          return null;
    1.84 @@ -335,5 +308,43 @@
    1.85        }
    1.86      }
    1.87  
    1.88 +    public static int CloseHandle(IntPtr handle) {
    1.89 +      return NativeMethods.CloseHandle(handle);
    1.90 +    }
    1.91 +
    1.92 +    private static class NativeMethods {
    1.93 +      private const string KERNEL = "kernel32.dll";
    1.94 +
    1.95 +      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi,
    1.96 +        CharSet = CharSet.Unicode)]
    1.97 +      public static extern IntPtr CreateFile(string fileName,
    1.98 +        AccessMode desiredAccess, ShareMode shareMode, IntPtr securityAttributes,
    1.99 +        CreationMode creationDisposition, FileAttribute flagsAndAttributes,
   1.100 +        IntPtr templateFilehandle);
   1.101 +
   1.102 +      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
   1.103 +      public static extern int CloseHandle(IntPtr handle);
   1.104 +
   1.105 +      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
   1.106 +      [return: MarshalAsAttribute(UnmanagedType.Bool)]
   1.107 +      public static extern bool DeviceIoControl(IntPtr handle,
   1.108 +        DriveCommand command, ref DriveCommandParameter parameter,
   1.109 +        int parameterSize, out DriveSmartReadResult result, int resultSize,
   1.110 +        out uint bytesReturned, IntPtr overlapped);
   1.111 +
   1.112 +      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
   1.113 +      [return: MarshalAsAttribute(UnmanagedType.Bool)]
   1.114 +      public static extern bool DeviceIoControl(IntPtr handle,
   1.115 +        DriveCommand command, ref DriveCommandParameter parameter,
   1.116 +        int parameterSize, out DriveCommandResult result, int resultSize,
   1.117 +        out uint bytesReturned, IntPtr overlapped);
   1.118 +
   1.119 +      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
   1.120 +      [return: MarshalAsAttribute(UnmanagedType.Bool)]
   1.121 +      public static extern bool DeviceIoControl(IntPtr handle,
   1.122 +        DriveCommand command, ref DriveCommandParameter parameter,
   1.123 +        int parameterSize, out DriveIdentifyResult result, int resultSize,
   1.124 +        out uint bytesReturned, IntPtr overlapped);
   1.125 +    }    
   1.126    }
   1.127  }