Research on low-level formatting of hard disk

zhaozj2021-02-17  55

File: // Hard disk low-level formatted research #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 Lowometry) {Format_Parameters FormatParaseters; Pbad_Trameters FormatParaseters; 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 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: // simple call 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, // do not 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 attributesif (hdevice == invalid_handle_value) {// if we can't open the drive ... messagebox ("Open device error" } IF (Lockvo LuMe (HDRIVE) == false) {MessageBox ("Locking Volume Failed); EXITPROCESS (1);

if (GetDiskGeometry (hDrive, & Geometry)!) {MessageBox ( "GetDiskGeometry failed"); ExitProcess (1);} GetSupportedGeometrys (hDrive); for (i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-28808.html

New Post(0)