ある物理的なドライブがSSDかどうかを判別する方法は色々あり得るが、Microsoftが示したアルゴリズム(
Windows 7 Disk Defragmenter User Interface Overview)にしたがってNyaRuRuさんがC++のコードを書かれていたので(
SSDなら動作を変えるアプリケーションを作る)、同じことをC#からWin32を使って行うコードを書いてみた。
内容的にはほぼNyaRuRuさんのコードのままで、以下の方法の2本立て。
- Windows 7以降で使えて、管理者権限が不要な「no seek penalty」を利用する。
- それ以前のOSでも使えるが、管理者権限が必要な「nominal media rotation rate」を利用する。
まずWin32の宣言は以下のとおり。使用するWin32の関数はCreateFileとDeviceIoControlで、後はそれに必要な定数と構造体。
// For CreateFile to get handle to drive
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;
private const uint OPEN_EXISTING = 3;
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
// CreateFile to get handle to drive
[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFileW(
[MarshalAs(UnmanagedType.LPWStr)]
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
// For control codes
private const uint FILE_DEVICE_MASS_STORAGE = 0x0000002d;
private const uint IOCTL_STORAGE_BASE = FILE_DEVICE_MASS_STORAGE;
private const uint FILE_DEVICE_CONTROLLER = 0x00000004;
private const uint IOCTL_SCSI_BASE = FILE_DEVICE_CONTROLLER;
private const uint METHOD_BUFFERED = 0;
private const uint FILE_ANY_ACCESS = 0;
private const uint FILE_READ_ACCESS = 0x00000001;
private const uint FILE_WRITE_ACCESS = 0x00000002;
private static uint CTL_CODE(uint DeviceType, uint Function,
uint Method, uint Access)
{
return ((DeviceType << 16) | (Access << 14) |
(Function << 2) | Method);
}
// For DeviceIoControl to check no seek penalty
private const uint StorageDeviceSeekPenaltyProperty = 7;
private const uint PropertyStandardQuery = 0;
[StructLayout(LayoutKind.Sequential)]
private struct STORAGE_PROPERTY_QUERY
{
public uint PropertyId;
public uint QueryType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte[] AdditionalParameters;
}
[StructLayout(LayoutKind.Sequential)]
private struct DEVICE_SEEK_PENALTY_DESCRIPTOR
{
public uint Version;
public uint Size;
[MarshalAs(UnmanagedType.U1)]
public bool IncursSeekPenalty;
}
// DeviceIoControl to check no seek penalty
[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl",
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint dwIoControlCode,
ref STORAGE_PROPERTY_QUERY lpInBuffer,
uint nInBufferSize,
ref DEVICE_SEEK_PENALTY_DESCRIPTOR lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
// For DeviceIoControl to check nominal media rotation rate
private const uint ATA_FLAGS_DATA_IN = 0x02;
[StructLayout(LayoutKind.Sequential)]
private struct ATA_PASS_THROUGH_EX
{
public ushort Length;
public ushort AtaFlags;
public byte PathId;
public byte TargetId;
public byte Lun;
public byte ReservedAsUchar;
public uint DataTransferLength;
public uint TimeOutValue;
public uint ReservedAsUlong;
public IntPtr DataBufferOffset;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] PreviousTaskFile;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] CurrentTaskFile;
}
[StructLayout(LayoutKind.Sequential)]
private struct ATAIdentifyDeviceQuery
{
public ATA_PASS_THROUGH_EX header;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public ushort[] data;
}
// DeviceIoControl to check nominal media rotation rate
[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl",
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint dwIoControlCode,
ref ATAIdentifyDeviceQuery lpInBuffer,
uint nInBufferSize,
ref ATAIdentifyDeviceQuery lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
これを受けた1つ目のメソッドが「no seek penalty」を利用する「HasNoSeekPenalty」で、物理ドライブ名を「\\\\.\\PhysicalDrive0」の形にした文字列を引数に取る。QA@ITで質問させていただいたもの(
SSDかどうかをC#から判別する方法)と基本的に同じ。あまり似た例がなかったので、ほぼスクラッチ。
// Method for no seek penalty
private static void HasNoSeekPenalty(string sDrive)
{
SafeFileHandle hDrive = CreateFileW(
sDrive,
0, // No access to drive
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if (hDrive == null || hDrive.IsInvalid)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("CreateFile failed. " + message);
}
uint IOCTL_STORAGE_QUERY_PROPERTY = CTL_CODE(
IOCTL_STORAGE_BASE, 0x500,
METHOD_BUFFERED, FILE_ANY_ACCESS); // From winioctl.h
STORAGE_PROPERTY_QUERY query_seek_penalty =
new STORAGE_PROPERTY_QUERY();
query_seek_penalty.PropertyId = StorageDeviceSeekPenaltyProperty;
query_seek_penalty.QueryType = PropertyStandardQuery;
DEVICE_SEEK_PENALTY_DESCRIPTOR query_seek_penalty_desc =
new DEVICE_SEEK_PENALTY_DESCRIPTOR();
uint returned_query_seek_penalty_size;
bool query_seek_penalty_result = DeviceIoControl(
hDrive,
IOCTL_STORAGE_QUERY_PROPERTY,
ref query_seek_penalty,
(uint)Marshal.SizeOf(query_seek_penalty),
ref query_seek_penalty_desc,
(uint)Marshal.SizeOf(query_seek_penalty_desc),
out returned_query_seek_penalty_size,
IntPtr.Zero);
hDrive.Close();
if (query_seek_penalty_result == false)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("DeviceIoControl failed. " + message);
}
else
{
if (query_seek_penalty_desc.IncursSeekPenalty == false)
{
Console.WriteLine("This drive has NO SEEK penalty.");
}
else
{
Console.WriteLine("This drive has SEEK penalty.");
}
}
}
(注)GetErrorMessageはエラーメッセージを得るためのメソッド。以下同じ。
2つ目のメソッドが「nominal media rotation rate」を利用する「HasNominalMediaRotationRate」で、引数は同じ。同じようにしてドライブの情報を取得する例は結構あったので(例えば、
Getting Hard disk drive info with DeviceIOControl)、かなり参考にさせていただいた。
// Method for nominal media rotation rate
// (Administrative privilege is required)
private static void HasNominalMediaRotationRate(string sDrive)
{
SafeFileHandle hDrive = CreateFileW(
sDrive,
GENERIC_READ | GENERIC_WRITE, // Administrative privilege is required
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if (hDrive == null || hDrive.IsInvalid)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("CreateFile failed. " + message);
}
uint IOCTL_ATA_PASS_THROUGH = CTL_CODE(
IOCTL_SCSI_BASE, 0x040b, METHOD_BUFFERED,
FILE_READ_ACCESS | FILE_WRITE_ACCESS); // From ntddscsi.h
ATAIdentifyDeviceQuery id_query = new ATAIdentifyDeviceQuery();
id_query.data = new ushort[256];
id_query.header.Length = (ushort)Marshal.SizeOf(id_query.header);
id_query.header.AtaFlags = (ushort)ATA_FLAGS_DATA_IN;
id_query.header.DataTransferLength =
(uint)(id_query.data.Length * 2); // Size of "data" in bytes
id_query.header.TimeOutValue = 3; // Sec
id_query.header.DataBufferOffset = (IntPtr)Marshal.OffsetOf(
typeof(ATAIdentifyDeviceQuery), "data");
id_query.header.PreviousTaskFile = new byte[8];
id_query.header.CurrentTaskFile = new byte[8];
id_query.header.CurrentTaskFile[6] = 0xec; // ATA IDENTIFY DEVICE
uint retval_size;
bool result = DeviceIoControl(
hDrive,
IOCTL_ATA_PASS_THROUGH,
ref id_query,
(uint)Marshal.SizeOf(id_query),
ref id_query,
(uint)Marshal.SizeOf(id_query),
out retval_size,
IntPtr.Zero);
hDrive.Close();
if (result == false)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("DeviceIoControl failed. " + message);
}
else
{
// Word index of nominal media rotation rate
// (1 means non-rotate device)
const int kNominalMediaRotRateWordIndex = 217;
if (id_query.data[kNominalMediaRotRateWordIndex] == 1)
{
Console.WriteLine("This drive is NON-ROTATE device.");
}
else
{
Console.WriteLine("This drive is ROTATE device.");
}
}
}
全体を通したコードは
こちらに載せた。 Visual Studio 2012で.NET Framework 4.0を対象として作成し、ThinkPad X61sにインストールしたWindows 8 ProとWindows 7上で、SSD(Intel X25-M G2)とHDDを判別できることを確認している。 まあC++のライブラリを作ってそれを利用する形でもいいが、.NETから直接使える方法があってもいいかな、ということで。
[追記] 物理ドライブ名の取得
論理ドライブ名から物理ドライブ名を取得するコードも書いてみた。WMIを使う方法もあるが、折角なのでWin32で。 まずWin32の宣言。
// For CreateFile to get handle to drive
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;
private const uint OPEN_EXISTING = 3;
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
// CreateFile to get handle to drive
[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFileW(
[MarshalAs(UnmanagedType.LPWStr)]
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
// For control codes
private const uint IOCTL_VOLUME_BASE = 0x00000056;
private const uint METHOD_BUFFERED = 0;
private const uint FILE_ANY_ACCESS = 0;
private static uint CTL_CODE(uint DeviceType, uint Function,
uint Method, uint Access)
{
return ((DeviceType << 16) | (Access << 14) |
(Function << 2) | Method);
}
// For DeviceIoControl to get disk extents
[StructLayout(LayoutKind.Sequential)]
private struct DISK_EXTENT
{
public uint DiskNumber;
public long StartingOffset;
public long ExtentLength;
}
[StructLayout(LayoutKind.Sequential)]
private struct VOLUME_DISK_EXTENTS
{
public uint NumberOfDiskExtents;
[MarshalAs(UnmanagedType.ByValArray)]
public DISK_EXTENT[] Extents;
}
// DeviceIoControl to get disk extents
[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl",
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
ref VOLUME_DISK_EXTENTS lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
これを受けたメソッド。論理ドライブ名の文字を引数とし、始めにリムーバブルディスクなどではない固定ディスクかどうかのチェックを通す。
// Method for disk extents
private static void GetDiskExtents(char cDrive)
{
DriveInfo di = new DriveInfo(cDrive.ToString());
if (di.DriveType != DriveType.Fixed)
{
Console.WriteLine("This drive is not fixed drive.");
}
string sDrive = "\\\\.\\" + cDrive.ToString() + ":";
SafeFileHandle hDrive = CreateFileW(
sDrive,
0, // No access to drive
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if (hDrive == null || hDrive.IsInvalid)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("CreateFile failed. " + message);
}
uint IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = CTL_CODE(
IOCTL_VOLUME_BASE, 0,
METHOD_BUFFERED, FILE_ANY_ACCESS); // From winioctl.h
VOLUME_DISK_EXTENTS query_disk_extents =
new VOLUME_DISK_EXTENTS();
uint returned_query_disk_extents_size;
bool query_disk_extents_result = DeviceIoControl(
hDrive,
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
IntPtr.Zero,
0,
ref query_disk_extents,
(uint)Marshal.SizeOf(query_disk_extents),
out returned_query_disk_extents_size,
IntPtr.Zero);
hDrive.Close();
if (query_disk_extents_result == false ||
query_disk_extents.Extents.Length != 1)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("DeviceIoControl failed. " + message);
}
else
{
Console.WriteLine("The physical drive number is: " +
query_disk_extents.Extents[0].DiskNumber);
}
}
[修正] SafeFileHandleの解放
ドライブへのハンドルの解放にCloseHandle関数を使っていたが、SafeFileHandleにCloseHandleを使うと、その後にガベージコレクタがメモリを解放するときにエラーを起こすらしいと気づいたので、SafeHandle.Closeメソッドを使うように修正した。
この問題は外部コンポーネントで不規則に起こるので、なかなか原因を掴めなかったが、Stack Overflowにこれに触れたコメントがあった(
How to resolve SEHException, VB .NET 2010)。MSDNなどでソースは見つけられなかったが、問題の状況によく合うし、修正してからは起こらなくなったので、当たっていたのだと思う。