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); }
private static extern int DeviceIoControl(uint hDevice, uint dwIoControlCode, ref SENDCMDINPARAMS lpInBuffer, int nInBufferSize, ref SENDCMDOUTPARAMS lpOutBuffer, int nOutBufferSize, ref uint lpbytesReturned, int lpOverlapped);
internal static extern bool DeviceIoControl ( SafeHandle hDevice, DFP dwIoControlCode, ref SENDCMDINPARAMS lpInBuffer, int nInBufferSize, out SENDCMDOUTPARAMS lpOutBuffer, int nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped);
public DriveInfo(int driveNumber) { int handle; this._driveType = DriveTypes.Unknown; SENDCMDINPARAMS sci = new SENDCMDINPARAMS(); SENDCMDOUTPARAMS sco = new SENDCMDOUTPARAMS(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { string file = @"\\.\PhysicalDrive" + driveNumber.ToString(); handle = CreateFile(file, -1073741824, 3, 0, 3, 0, 0); } else { string _Vb_t_string_0 = @"\\.\Smartvsd"; handle = CreateFile(_Vb_t_string_0, 0, 0, 0, 1, 0, 0); } if (handle != -1) { int returnSize; sci.DriveNumber = (byte)driveNumber; sci.BufferSize = Marshal.SizeOf(sco); sci.DriveRegs.DriveHead = (byte)(160 | (driveNumber << 4)); sci.DriveRegs.Command = 0xec; sci.DriveRegs.SectorCount = 1; sci.DriveRegs.SectorNumber = 1; returnSize = 0; if (DeviceIoControl(handle, 0x7c088, sci, Marshal.SizeOf(sci), sco, Marshal.SizeOf(sco), ref returnSize, 0) != 0) { this._serialNumber = SwapChars(sco.IDS.SerialNumber); this._model = SwapChars(sco.IDS.ModelNumber); this._firmware = SwapChars(sco.IDS.FirmwareRevision); this._numberCylinders = sco.IDS.NumberCylinders; this._numberHeads = sco.IDS.NumberHeads; this._sectorsPerTrack = sco.IDS.SectorsPerTrack; this._bufferSize = sco.IDS.BufferSize * 0x200; if ((sco.IDS.GenConfig & 0x80) == 0x80) { this._driveType = DriveTypes.Removable; } else if ((sco.IDS.GenConfig & 0x40) == 0x40) { this._driveType = DriveTypes.Fixed; } else { this._driveType = DriveTypes.Unknown; } } CloseHandle(handle); } }
//**************************************************************************** // 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))); }
private static int DeviceIoControl(int hDevice, int dwIoControlCode, ref SENDCMDINPARAMS lpInBuffer, int nInBufferSize, ref SENDCMDOUTPARAMS lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, int ByVallpOverlapped) { // }
public static string Read(byte drive) { OperatingSystem os = Environment.OSVersion; if (os.Platform != PlatformID.Win32NT) { throw new NotSupportedException("仅支持WindowsNT/2000/XP"); } //我没有NT4,请哪位大大测试一下NT4下能不能用 //if (os.Version.Major < 5) throw new NotSupportedException("仅支持WindowsNT/2000/XP"); string driveName = "\\\\.\\PhysicalDrive" + drive.ToString(); uint device = CreateFile(driveName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); if (device == INVALID_HANDLE_VALUE) { return(""); } GETVERSIONOUTPARAMS verPara = new GETVERSIONOUTPARAMS(); uint bytRv = 0; if (0 != DeviceIoControl(device, DFP_GET_VERSION, 0, 0, ref verPara, Marshal.SizeOf(verPara), ref bytRv, 0)) { if (verPara.bIDEDeviceMap > 0) { byte bIDCmd = (byte)(((verPara.bIDEDeviceMap >> drive & 0x10) != 0) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY); SENDCMDINPARAMS scip = new SENDCMDINPARAMS(); SENDCMDOUTPARAMS scop = new SENDCMDOUTPARAMS(); scip.cBufferSize = IDENTIFY_BUFFER_SIZE; scip.irDriveRegs.bFeaturesReg = 0; scip.irDriveRegs.bSectorCountReg = 1; scip.irDriveRegs.bCylLowReg = 0; scip.irDriveRegs.bCylHighReg = 0; scip.irDriveRegs.bDriveHeadReg = (byte)(0xA0 | ((drive & 1) << 4)); scip.irDriveRegs.bCommandReg = bIDCmd; scip.bDriveNumber = drive; if (0 != DeviceIoControl(device, DFP_RECEIVE_DRIVE_DATA, ref scip, Marshal.SizeOf(scip), ref scop, Marshal.SizeOf(scop), ref bytRv, 0)) { StringBuilder s = new StringBuilder(); for (int i = 20; i < 40; i += 2) { s.Append((char)(scop.bBuffer[i + 1])); s.Append((char)scop.bBuffer[i]); } CloseHandle(device); return(s.ToString().Trim()); } } } CloseHandle(device); return(""); }