DeviceIoControl problems...

I have 2 problems... first I am trying to format a disk using DeviceIoControl (*code below*) but it is only working on floppy disks.  I need to format CompactFlash memory cards in a memory card reader not floppy disks.  Does this method only support floppies??  If so ignore the second part of this question and if you have any ideas how to accomplish this please lend some help.  

Second problem is when formatting floppies it just makes like 200+ directories with no size and all named: "++++++++.+++" (but the plus signs are divide symbols).  

Here is the code I am using:

HANDLE hDrive;
DISK_GEOMETRY OutBuffer;
DWORD BytesReturned;
FORMAT_PARAMETERS FormatParameters;
PBAD_TRACK_NUMBER lpBadTrack;
UINT i;
BOOL b;
DWORD ReturnedByteCount;

 hDrive = CreateFile(
          "\\\\.\\a:",
          GENERIC_READ | GENERIC_WRITE,
          FILE_SHARE_READ|FILE_SHARE_WRITE,
          NULL,
          OPEN_EXISTING,
          0,
          NULL
          );

 if ( hDrive == INVALID_HANDLE_VALUE )
  {
    ExitProcess(1);
  }

 BOOL mybool = DeviceIoControl(
               hDrive,
               FSCTL_LOCK_VOLUME,
               NULL,
               0,
               NULL,
               0,
               &BytesReturned,
               NULL
               );

mybool = DeviceIoControl(
         hDrive,              
         IOCTL_DISK_GET_DRIVE_GEOMETRY,
         NULL,                          
         0,                            
         &OutBuffer,          
         sizeof(DISK_GEOMETRY),      
         &BytesReturned,    
         NULL
         );

  FormatParameters.MediaType = OutBuffer->MediaType;
  FormatParameters.StartHeadNumber = 0;
  FormatParameters.EndHeadNumber = OutBuffer->TracksPerCylinder - 1;
  lpBadTrack = (PBAD_TRACK_NUMBER) LocalAlloc(LMEM_ZEROINIT,OutBuffer->TracksPerCylinder*sizeof(*lpBadTrack));

  for (i = 0; i < OutBuffer->Cylinders.LowPart; i++) {

      FormatParameters.StartCylinderNumber = i;
      FormatParameters.EndCylinderNumber = i;

      b = DeviceIoControl(
              hDrive,
              IOCTL_DISK_FORMAT_TRACKS,
              &FormatParameters,
              sizeof(FormatParameters),
              lpBadTrack,
              NULL,
              &ReturnedByteCount,
              NULL
              );

      if (!b ) {
          LocalFree(lpBadTrack);
          return b;
          }
      }

  LocalFree(lpBadTrack);

  DWORD BytesReturned;

  BOOL mybool = DeviceIoControl(
              hDrive,
              FSCTL_UNLOCK_VOLUME,
              NULL,
              0,
              NULL,
              0,
              &BytesReturned,
              NULL
              );


JedimasterAsked:
Who is Participating?
 
NetminderConnect With a Mentor Commented:
PAQed, with points refunded (200)

Netminder
EE Admin
0
 
JedimasterAuthor Commented:
Figures... as soon as I post my question I find in the MSDN that the IOCTL_DISK_FORMAT_TRACKS is for floppy disks only.  I guess since it sees a CompactFlash memory card as a "Removable Media" instead of a floppy disk it just will not work.  Does anyone know how I can do this without using SHFormatDrive??

I would still like to know why I was getting those funky folders on a floppy disk when I tried this method though.
0
 
DanRollinsCommented:
Hi Jedimaster,
<Edited by SpideyMod>
An erroneous auto-delete was posted, we have corrected the problem.  Please indicate recommendations and these will not be scheduled for auto-delete, but will be finalized within the standard week notice.

A recommendation of "PAQ with refund" has already been proposed.

CleanupGuy -- EE database cleanup volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.