硬盘低级格式化的研究 (转)

本文介绍了一种硬盘低级格式化的实现方法,通过Windows API函数进行操作,包括获取磁盘几何信息、锁定磁盘、低级格式化等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

硬盘低级格式化的研究 (转)[@more@]

file://硬盘低级格式化的研究
#include
#include
#include
#include
BOOL GetDiskGeometry(HANDLE hDisk,PDISK_GEOMETRY lpGeometry )
{
  Dword ReturnedbyteCount;

  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
  );
}
file://简调用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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-991591/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值