硬碟低階格式化的研究 (轉)

worldblog發表於2007-12-12
硬碟低階格式化的研究 (轉)[@more@]

盤低階格式化的研究
#include
#include
#include
#include
BOOL GetDiskGeometry(HANDLE hDisk,PDISK_GEOMETRY lpGeometry )
{
  D ReturneyteCount;

  return DeviceIoControl(
  hDisk,
  IOCTL_DISK_GET_DRIVE_GEOMETRY,
  NULL,
  0,
  lpGeometry,
  sizeof(*lpGeometry),
  &ReturnedByteCount,
  NULL
  );
}
DWORD GetSupportedGeometrys(
  HANDLE hDisk
  )
{
  DWORD ReturnedByteCount;
  BOOL b;
  DWORD NumberSupported;

  b = DeviceIoControl(
  hDisk,
  IOCTL_DISK_GET_MEDIA_TYPES,
  NULL,
  0,
  SupportedGeometry,
  sizeof(SupportedGeometry),
  &ReturnedByteCount,
  NULL
  );
  if ( b ) {
  NumberSupported = ReturnedByteCount / sizeof(DISK_GEOMETRY);
  }
  else {
  NumberSupported = 0;
  }
  SupportedGeometryCount = NumberSupported;

  return NumberSupported;
}
BOOL  LowLevelFormat(HANDLE hDisk,PDISK_GEOMETRY lpGeometry )
{
  FORMAT_PARAMETERS FormatParameters;
  PBAD_TRACK_NUMBER lpBadTrack;
  UINT i;
  BOOL b;
  DWORD ReturnedByteCount;

  FormatParameters.MediaType = lpGeometry->MediaType;
  FormatParameters.StartHeadNumber = 0;
  FormatParameters.EndHeadNumber = lpGeometry->TracksPerCylinder - 1;
  lpBadTrack = (PBAD_TRACK_NUMBER) LocalAlloc(LMEM_ZEROINIT,lpGeometry->TracksPerCylinder*sizeof(*lpBadTrack));

  for (i = 0; i < lpGeometry->Cylinders.LowPart; i++) {

  FormatParameters.StartCylinderNumber = i;
  FormatParameters.EndCylinderNumber = i;

  b = DeviceIoControl(
  hDisk,
  IOCTL_DISK_FORMAT_TRACKS,
  &FormatParameters,
  sizeof(FormatParameters),
  lpBadTrack,
  lpGeometry->TracksPerCylinder*sizeof(*lpBadTrack),
  &ReturnedByteCount,
  NULL
  );

  if (!b ) {
  LocalFree(lpBadTrack);
  return b;
  }
  }

  LocalFree(lpBadTrack);

  return TRUE;
}

BOOL LockVolume( HANDLE hDisk  )
{
  DWORD ReturnedByteCount;

  return DeviceIoControl(
  hDisk,
  FSCTL_LOCK_VOLUME,
  NULL,
  0,
  NULL,
  0,
  &ReturnedByteCount,
  NULL
  );
}

BOOL UnlockVolume(  HANDLE hDisk  )
{
  DWORD ReturnedByteCount;

  return DeviceIoControl(
  hDisk,
  FSCTL_UNLOCK_VOLUME,
  NULL,
  0,
  NULL,
  0,
  &ReturnedByteCount,
  NULL
  );
}

BOOL DismountVolume( HANDLE hDisk  )
{
  DWORD ReturnedByteCount;

  return DeviceIoControl(
  hDisk,
  FSCTL_DISMOUNT_VOLUME,
  NULL,
  0,
  NULL,
  0,
  &ReturnedByteCount,
  NULL
  );
}
單sample.
  HANDLE hDiskImage;
  DISK_GEOMETRY Geometry;
  HANDLE hDevice; // handle to the drive to be examined
  DISK_GEOMETRY SupportedGeometry[20];
DWORD SupportedGeometryCount;
hDevice = CreateFile(".PhysicalDrive0", // drive to open
  0,  // don't need any access to the drive
  FILE_SHARE_READ | FILE_SHARE_WRITE,  // share mode
  NULL,  // default security attributes
  OPEN_EXISTING,  // disposition
  0,  // file attributes
  NULL);  // don't copy any file's attributes

 if (hDevice == INVALID_HANDLE_VALUE) {  // if we can't open the drive...
  MessageBox("開啟裝置錯誤");
  } 
  if ( LockVolume(hDrive) == FALSE ) {
  MessageBox("Locking volume failed );
  ExitProcess(1);
  }

  if ( !GetDiskGeometry(hDrive,&Geometry) ) {
  MessageBox("GetDiskGeometry failed");
  ExitProcess(1);
  }
  GetSupportedGeometrys(hDrive);
  for(i=0;i  LowLevelFormat(hDrive,&SupportedGeometry[i]);
  }
  DismountVolume(hDrive);
  UnlockVolume(hDrive);
  ExitProcess(0);


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-991591/,如需轉載,請註明出處,否則將追究法律責任。

相關文章