I need to format a disk under NT/2000/XP and I use this code, which obviously doesn't work!
DISK_GEOMETRY dg;
FORMAT_PARAMETERS fp;
HANDLE hDevice;
hDevice = CreateFile("\\\\.\\A:", GENERIC_READ, FILE_SHARE_READ |
FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &dg,
sizeof(DISK_GEOMETRY), &bytes, NULL);
DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
&bytes, NULL);
fp.MediaType = dg.MediaType;
fp.StartHeadNumber = 0;
fp.EndHeadNumber = dg.TracksPerCylinder - 1;
for (UINT i=0; i<dg.Cylinders.LowPart; i++)
{
fp.StartCylinderNumber = fp.EndCylinderNumber = i;
if (!DeviceIoControl(hDevice, IOCTL_DISK_FORMAT_TRACKS, &fp,
sizeof(FORMAT_PARAMETERS), NULL, 0, &bytes, NULL))
{
DeviceIoControl(hDevice, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL,
0, &bytes, NULL);
CloseHandle(hDevice);
Error("Format error"); // This is just a routine that pop-ups a msgbox
return -1;
}
DeviceIoControl(hDevice, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL,
0, &bytes, NULL);
CloseHandle(hDevice);
MessageBox(hWnd, "Format complete.", "Info", MB_OK | MB_ICONINFORMATION);
The error is given by DeviceIoControl, with IOCTL_FORMAT_TRACKS, and GetLastError returns 5, that is "Access denied"...
Where's the error???
Thanks in advance
nICO
How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.
JOHN KEATS