获取硬盘序列号报错,以下为方法,什么情况下会报 IDE identify command not supported。
#region GetHddInfoNT
private static HardDiskInfo GetHddInfoNT(byte driveIndex)
{
MessageBox.Show("GetHddInfoNT");
GetVersionOutParams vers = new GetVersionOutParams();
SendCmdInParams inParam = new SendCmdInParams();
SendCmdOutParams outParam = new SendCmdOutParams();
uint bytesReturned = 0;
string s = string.Format(@"\\.\PhysicalDrive{0}", driveIndex);
IntPtr hDevice = CreateFile(
s,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if (hDevice == IntPtr.Zero)
{
throw new Exception("CreateFile faild.");
}
int handel = DeviceIoControl(
hDevice,
DFP_GET_VERSION,
IntPtr.Zero,
0,
ref vers,
(uint)Marshal.SizeOf(vers),
ref bytesReturned,
IntPtr.Zero);
if (0 == handel)
{
CloseHandle(hDevice);
return new HardDiskInfo();
}
MessageBox.Show(vers.fCapabilities.ToString());
if (0 == (vers.fCapabilities & 1))
{
CloseHandle(hDevice);
throw new Exception("Error: IDE identify command not supported. GetHddInfoNT");
}
if (0 != (driveIndex & 1))
{
inParam.irDriveRegs.bDriveHeadReg = 0xb0;
}
else
{
inParam.irDriveRegs.bDriveHeadReg = 0xa0;
}
inParam.irDriveRegs.bCommandReg = 0xec;
inParam.bDriveNumber = driveIndex;
inParam.irDriveRegs.bSectorCountReg = 1;
inParam.irDriveRegs.bSectorNumberReg = 1;
inParam.cBufferSize = 512;
if (0 == DeviceIoControl(
hDevice,
DFP_RECEIVE_DRIVE_DATA,
ref inParam,
(uint)Marshal.SizeOf(inParam),
ref outParam,
(uint)Marshal.SizeOf(outParam),
ref bytesReturned,
IntPtr.Zero))
{
CloseHandle(hDevice);
throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
}
CloseHandle(hDevice);
return GetHardDiskInfo(outParam.bBuffer);
}
#endregion