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("Drive {0} may not exists.", driveIndex + 1)); } // If IDE identify command not supported, fails if (0 == (vers.fCapabilities & 1)) { CloseHandle(hDevice); throw new Exception("Error: IDE identify command not supported."); } // 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("Drive {0} is a ATAPI device, we don't detect it.", 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("DeviceIoControl failed: 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("Open smartvsd.vxd failed."); } 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("DeviceIoControl failed:DFP_GET_VERSION"); } // If IDE identify command not supported, fails if (0 == (vers.fCapabilities & 1)) { CloseHandle(hDevice); throw new Exception("Error: IDE identify command not supported."); } 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("Drive {0} is a ATAPI device, we don't detect it", 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("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA"); } CloseHandle(hDevice); return(GetHardDiskInfo(outParam.bBuffer)); }