bool IsSmartEnabled(IntPtr hDevice, byte ucDriveIndex, ref ST_DRIVE_INFO m_stDrivesInfo) { SENDCMDINPARAMS stCIP = new SENDCMDINPARAMS(); SENDCMDOUTPARAMS stCOP = new SENDCMDOUTPARAMS(); uint dwRet = 0; bool bRet = false; stCIP.cBufferSize = 0; stCIP.bDriveNumber = ucDriveIndex; stCIP.irDriveRegs.bFeaturesReg = C_SMART_SUBREGISTER.ENABLE_SMART; stCIP.irDriveRegs.bSectorCountReg = 1; stCIP.irDriveRegs.bSectorNumberReg = 1; stCIP.irDriveRegs.bCylLowReg = SMART_CYL_LOW; stCIP.irDriveRegs.bCylHighReg = SMART_CYL_HI; stCIP.irDriveRegs.bDriveHeadReg = DRIVE_HEAD_REG; stCIP.irDriveRegs.bCommandReg = SMART_CMD; object OutObj = stCOP; bRet = kernel32.DeviceIoControl(hDevice, SMART_SEND_DRIVE_COMMAND, stCIP, ref OutObj, ref dwRet); if (bRet) { } else { dwRet = (uint)Marshal.GetLastWin32Error(); m_stDrivesInfo.m_csErrorString = "Error " + dwRet + " in reading SMART Enabled flag"; } return(bRet); }
public ST_DRIVE_INFO[] GetSMARTForAllDrives() { System.IO.DriveInfo[] allDrives = System.IO.DriveInfo.GetDrives(); ST_DRIVE_INFO[] SMARTValues = new ST_DRIVE_INFO[allDrives.Length]; int Count = 0; for (byte i = 0; i < allDrives.Length; i++) { if (allDrives[i].DriveType == System.IO.DriveType.Fixed) { if (GetSMART(i, ref SMARTValues[i])) { Count++; } } } ST_DRIVE_INFO[] res = new ST_DRIVE_INFO[Count]; Array.Copy(SMARTValues, res, Count); return(res); }
bool CollectDriveInfo(IntPtr hDevice, byte ucDriveIndex, ref ST_DRIVE_INFO m_stDrivesInfo) { bool bRet = false; SENDCMDINPARAMS stCIP = new SENDCMDINPARAMS(); uint dwRet = 0; const int OUT_BUFFER_SIZE = IDENTIFY_BUFFER_SIZE + 16; char[] szOutput = new char[OUT_BUFFER_SIZE]; stCIP.cBufferSize = IDENTIFY_BUFFER_SIZE; stCIP.bDriveNumber = ucDriveIndex; stCIP.irDriveRegs.bFeaturesReg = 0; stCIP.irDriveRegs.bSectorCountReg = 1; stCIP.irDriveRegs.bSectorNumberReg = 1; stCIP.irDriveRegs.bCylLowReg = 0; stCIP.irDriveRegs.bCylHighReg = 0; stCIP.irDriveRegs.bDriveHeadReg = DRIVE_HEAD_REG; stCIP.irDriveRegs.bCommandReg = ID_CMD; object TempO = szOutput; bRet = kernel32.DeviceIoControl(hDevice, SMART_RCV_DRIVE_DATA, stCIP, ref TempO, ref dwRet); szOutput = (char[])TempO; if (bRet) { m_stDrivesInfo.m_stInfo = (ST_IDSECTOR)ArrayToStructure(szOutput, m_stDrivesInfo.m_stInfo.GetType(), 16); ConvertString(ref m_stDrivesInfo.m_stInfo.sModelNumber); ConvertString(ref m_stDrivesInfo.m_stInfo.sSerialNumber); ConvertString(ref m_stDrivesInfo.m_stInfo.sFirmwareRev); } else { dwRet = (uint)Marshal.GetLastWin32Error(); } return(bRet); }
public bool GetSMART(byte ucDriveIndex, ref ST_DRIVE_INFO m_stDrivesInfo) { uint dwRet = 0; string DriveName = "\\\\.\\PHYSICALDRIVE" + ucDriveIndex; IntPtr hDevice = kernel32.CreateFile(DriveName, F_GENERIC_.READ_WRITE, F_FILE_SHARE_.READ_WRITE, IntPtr.Zero, F_Creation.OPEN_EXISTING, F_ATTRIBUTE_.SYSTEM, IntPtr.Zero); if (hDevice == (new IntPtr(-1))) { dwRet = (uint)Marshal.GetLastWin32Error(); return(false); } m_stDrivesInfo.m_ucDriveIndex = ucDriveIndex; object TempObj = m_stDrivesInfo.m_stGVIP; bool bRet = kernel32.DeviceIoControl(hDevice, SMART_GET_VERSION, null, ref TempObj, ref dwRet); m_stDrivesInfo.m_stGVIP = (GETVERSIONINPARAMS)TempObj; if (bRet) { if ((m_stDrivesInfo.m_stGVIP.fCapabilities & CAP_SMART_CMD) == CAP_SMART_CMD) { if (IsSmartEnabled(hDevice, ucDriveIndex, ref m_stDrivesInfo)) { m_stDrivesInfo.m_SmartEnabled = true; bRet = CollectDriveInfo(hDevice, ucDriveIndex, ref m_stDrivesInfo); bRet = ReadSMARTAttributes(hDevice, ucDriveIndex, ref m_stDrivesInfo); } else { m_stDrivesInfo.m_SmartEnabled = false; } } } kernel32.CloseHandle(hDevice); return(true); }
bool ReadSMARTAttributes(IntPtr hDevice, byte ucDriveIndex, ref ST_DRIVE_INFO m_stDrivesInfo) { SENDCMDINPARAMS stCIP = new SENDCMDINPARAMS(); uint dwRet = 0; bool bRet = false; char[] szAttributes = new char[Marshal.SizeOf(typeof(ST_ATAOUTPARAM)) + READ_ATTRIBUTE_BUFFER_SIZE - 1]; m_stDrivesInfo.m_stSmartInfo = new ST_SMART_INFO[256]; stCIP.cBufferSize = READ_ATTRIBUTE_BUFFER_SIZE; stCIP.bDriveNumber = ucDriveIndex; stCIP.irDriveRegs.bFeaturesReg = C_SMART_SUBREGISTER.READ_ATTRIBUTES; stCIP.irDriveRegs.bSectorCountReg = 1; stCIP.irDriveRegs.bSectorNumberReg = 1; stCIP.irDriveRegs.bCylLowReg = SMART_CYL_LOW; stCIP.irDriveRegs.bCylHighReg = SMART_CYL_HI; stCIP.irDriveRegs.bDriveHeadReg = DRIVE_HEAD_REG; stCIP.irDriveRegs.bCommandReg = SMART_CMD; object TempO = szAttributes; bRet = kernel32.DeviceIoControl(hDevice, SMART_RCV_DRIVE_DATA, stCIP, ref TempO, ref dwRet); szAttributes = (char[])TempO; if (bRet) { m_stDrivesInfo.m_ucSmartValues = 0; m_stDrivesInfo.m_ucDriveIndex = ucDriveIndex; int pT1 = Marshal.SizeOf(typeof(ST_DRIVERSTAT)) + 4; // buffer int pT3, pT2; for (int ucT1 = 0; ucT1 < 30; ++ucT1) { pT3 = pT1 + 2 + ucT1 * 12; pT2 = pT3 + C_SMART_ATTRIB_INDEX.RAW; for (int i = 2; i < 7; i++) { szAttributes[pT3 + C_SMART_ATTRIB_INDEX.RAW + i] = (char)0; } if (szAttributes[pT3 + C_SMART_ATTRIB_INDEX.INDEX] != 0) { m_stDrivesInfo.m_stSmartInfo[m_stDrivesInfo.m_ucSmartValues] = new ST_SMART_INFO(); m_stDrivesInfo.m_stSmartInfo[m_stDrivesInfo.m_ucSmartValues].m_ucAttribIndex = (byte)szAttributes[pT3 + C_SMART_ATTRIB_INDEX.INDEX]; m_stDrivesInfo.m_stSmartInfo[m_stDrivesInfo.m_ucSmartValues].m_ucValue = (byte)szAttributes[pT3 + C_SMART_ATTRIB_INDEX.VALUE]; m_stDrivesInfo.m_stSmartInfo[m_stDrivesInfo.m_ucSmartValues].m_ucWorst = (byte)szAttributes[pT3 + C_SMART_ATTRIB_INDEX.WORST]; m_stDrivesInfo.m_stSmartInfo[m_stDrivesInfo.m_ucSmartValues].m_dwAttribValue = ReadUint(szAttributes, pT2); m_stDrivesInfo.m_stSmartInfo[m_stDrivesInfo.m_ucSmartValues].m_dwThreshold = uint.MaxValue; m_stDrivesInfo.m_stSmartInfo[m_stDrivesInfo.m_ucSmartValues].Details = GetAttrDesc(m_stDrivesInfo.m_stSmartInfo[m_stDrivesInfo.m_ucSmartValues].m_ucAttribIndex); m_stDrivesInfo.m_ucSmartValues++; } } } else { dwRet = (uint)Marshal.GetLastWin32Error(); } stCIP.irDriveRegs.bFeaturesReg = C_SMART_SUBREGISTER.READ_THRESHOLDS; stCIP.cBufferSize = READ_THRESHOLD_BUFFER_SIZE; // Is same as attrib size TempO = szAttributes; bRet = kernel32.DeviceIoControl(hDevice, SMART_RCV_DRIVE_DATA, stCIP, ref TempO, ref dwRet); szAttributes = (char[])TempO; if (bRet) { int pT1 = Marshal.SizeOf(typeof(ST_DRIVERSTAT)) + 4; // buffer int pT3, pT2; for (int ucT1 = 0; ucT1 < 30; ++ucT1) { pT2 = pT1 + 2 + ucT1 * 12 + 5; pT3 = pT1 + 2 + ucT1 * 12; for (int i = 2; i < 7; i++) { szAttributes[pT3 + C_SMART_ATTRIB_INDEX.RAW + i] = (char)0; } if (szAttributes[pT3] != 0) { for (int i = 0; i < m_stDrivesInfo.m_ucSmartValues; i++) { if (m_stDrivesInfo.m_stSmartInfo[i].m_ucAttribIndex == szAttributes[pT3]) { m_stDrivesInfo.m_stSmartInfo[i].m_dwThreshold = szAttributes[pT2]; break; } } } } } return(bRet); }