static extern int DeviceIoControl( IntPtr hDevice, uint dwIoControlCode, ref SendCmdInParams lpInBuffer, uint nInBufferSize, ref SendCmdOutParams lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned, [Out] IntPtr lpOverlapped);
private static HardDiskInfo GetHddInfoNT(byte driveIndex) { GetVersionOutParams vers = new GetVersionOutParams(); SendCmdInParams inParam = new SendCmdInParams(); SendCmdOutParams outParam = new SendCmdOutParams(); uint bytesReturned = 0; // We start in NT/Win2000 IntPtr hDevice = CreateFile( string.Format(@"//./PhysicalDrive{0}", driveIndex), 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."); } if (0 == DeviceIoControl( hDevice, DFP_GET_VERSION, IntPtr.Zero, 0, ref vers, (uint)Marshal.SizeOf(vers), ref bytesReturned, IntPtr.Zero)) { CloseHandle(hDevice); throw new Exception(string.Format("磁盘 {0} 可能不存在.", driveIndex + 1)); } // If IDE identify command not supported, fails if (0 == (vers.fCapabilities & 1)) { CloseHandle(hDevice); throw new Exception("错误: IDE 查询命令不支持"); } // Identify the IDE drives if (0 != (driveIndex & 1)) { inParam.irDriveRegs.bDriveHeadReg = 0xb0; } else { inParam.irDriveRegs.bDriveHeadReg = 0xa0; } if (0 != (vers.fCapabilities & (16 >> driveIndex))) { // We don't detect a ATAPI device. CloseHandle(hDevice); throw new Exception(string.Format("磁盘 {0} 是 ATAPI 设备, 不支持.", driveIndex + 1)); } else { 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("设备控制器失败: DFP_RECEIVE_DRIVE_DATA"); } CloseHandle(hDevice); return(GetHardDiskInfo(outParam.bBuffer)); }
private static HardDiskInfo GetHddInfo9x(byte driveIndex) { GetVersionOutParams vers = new GetVersionOutParams(); SendCmdInParams inParam = new SendCmdInParams(); SendCmdOutParams outParam = new SendCmdOutParams(); uint bytesReturned = 0; IntPtr hDevice = CreateFile( @"//./Smartvsd", 0, 0, IntPtr.Zero, CREATE_NEW, 0, IntPtr.Zero); if (hDevice == IntPtr.Zero) { throw new Exception("打开 smartvsd.vxd 失败."); } if (0 == DeviceIoControl( hDevice, DFP_GET_VERSION, IntPtr.Zero, 0, ref vers, (uint)Marshal.SizeOf(vers), ref bytesReturned, IntPtr.Zero)) { CloseHandle(hDevice); throw new Exception("设备控制器失败:DFP_GET_VERSION"); } // If IDE identify command not supported, fails if (0 == (vers.fCapabilities & 1)) { CloseHandle(hDevice); throw new Exception("错误: IDE 查询命令不支持"); } if (0 != (driveIndex & 1)) { inParam.irDriveRegs.bDriveHeadReg = 0xb0; } else { inParam.irDriveRegs.bDriveHeadReg = 0xa0; } if (0 != (vers.fCapabilities & (16 >> driveIndex))) { // We don't detect a ATAPI device. CloseHandle(hDevice); throw new Exception(string.Format("设备 {0} 是 ATAPI 设备, 不支持", driveIndex + 1)); } else { 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("设备控制器失败: DFP_RECEIVE_DRIVE_DATA"); } CloseHandle(hDevice); return(GetHardDiskInfo(outParam.bBuffer)); }