Windows programming

Moderators: None (Apply to moderate this forum)
Number of threads: 3711
Number of posts: 9173

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Format under NT/2000/XP Posted by chick80 on 28 Feb 2003 at 10:35 AM
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

Report
Re: Format under NT/2000/XP Posted by velius on 2 Mar 2003 at 1:25 PM
Only the system can have access to such hardware for such a purpose. NT, 2000, and XP are all based on NT. They have a hareware abstraction layer that isolates the hardware from the user in a way that only the OS can access it. I don't know how to tell you to access such a service in these to work. I've never seen any other software able to format a HDD under these other than the OS itself.


When it came down to it I dropped everything and ran.
-Ramza Final Fantasy Tactics

Report
Re: Format under NT/2000/XP Posted by chick80 on 3 Mar 2003 at 10:08 AM
No, you can easily low-level access the disks for reading or writing, is not difficult.
I found the error, it was just the handle to the device. In fact it was only opened in reading mode and not also in writing.

nICO



How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.
JOHN KEATS





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.