硬盘低级格式化的研究

类别:VC语言 点击:0 评论:0 推荐:

file://硬盘低级格式化的研究
#include <winioctl.h>
#include <string.h>
#include <ctype.h>
#include <memory.h>
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<SupportedGeometryCount;i++) {
           LowLevelFormat(hDrive,&SupportedGeometry[i]);
           }
            DismountVolume(hDrive);
        UnlockVolume(hDrive);
        ExitProcess(0);

本文地址:http://com.8s8s.com/it/it2397.htm