//**************************************************************************** // CheckSMARTEnable - Check if SMART enable // FUNCTION: Send a SMART_ENABLE_SMART_OPERATIONS command to the drive // bDriveNum = 0-3 //*************************************************************************** private static bool CheckSMARTEnable(int hDrive, IDE_DRIVE_NUMBER DriveNum) { //Set up data structures for Enable SMART Command. SENDCMDINPARAMS SCIP = null; SENDCMDOUTPARAMS SCOP = null; int lpcbBytesReturned = 0; { SCIP.cBufferSize = 0; { SCIP.irDriveRegs.bFeaturesReg = SMART_ENABLE_SMART_OPERATIONS; SCIP.irDriveRegs.bCylLowReg = SMART_CYL_LOW; SCIP.irDriveRegs.bCylHighReg = SMART_CYL_HI; //Compute the drive number. SCIP.irDriveRegs.bDriveHeadReg = 0xa0; // Or (DriveNum And 1) * 16 SCIP.irDriveRegs.bCommandReg = IDE_EXECUTE_SMART_FUNCTION; } SCIP.bDriveNumber = DriveNum; } return(Convert.ToBoolean(DeviceIoControl(hDrive, DFP_SEND_DRIVE_COMMAND, ref SCIP, Marshal.SizeOf(SCIP) - 4, ref SCOP, Marshal.SizeOf(SCOP) - 4, ref lpcbBytesReturned, 0L))); }
//*************************************************************************** // ReadAttributesCmd // FUNCTION: Send a SMART_READ_ATTRIBUTE_VALUES command to the drive // bDriveNum = 0-3 //*************************************************************************** public static string GetDriveInfo(IDE_DRIVE_NUMBER DriveNum) { string functionReturnValue = null; int hDrive = 0; hDrive = OpenSmart(DriveNum); if (hDrive == INVALID_HANDLE_VALUE) { return(null); } if (CheckSMARTEnable(hDrive, DriveNum)) { functionReturnValue = IdentifyDrive(hDrive, IDE_ID_FUNCTION); } else { functionReturnValue = null; } CloseHandle(hDrive); return(functionReturnValue); }
//*************************************************************************** // Open SMART to allow DeviceIoControl communications. Return SMART handle //*************************************************************************** private static int OpenSmart(IDE_DRIVE_NUMBER drv_num) { return(CreateFile(string.Format("\\\\.\\PhysicalDrive{0}", Convert.ToInt32(drv_num)), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0L, OPEN_EXISTING, 0, 0)); }