HOWTO: Getting Floppy Drive Information

ID: Q115828

3.10 3.50 WINDOWS NT

 kbhowto

The information in this article applies to:

SUMMARY

To get the media type(s) supported by a floppy drive, it is necessary to call CreateFile() to get a handle to the drive and then DeviceIoControl() to get the information. However, if there is no floppy disk in the floppy drive, the following message box may appear when CreateFile() is called for drive A (\\.\a:):

   There is no disk in the drive. Please insert a disk into drive A:

When calling CreateFile(), be sure to use 0 for the access mode and FILE_SHARE_READ for the share mode so that the user will not be prompted to insert a floppy disk:

   CreateFile(
      szFileName,
      0,
      FILE_SHARE_READ,
      NULL,
      OPEN_ALWAYS,
      0,
      NULL
   );

Another way to avoid the message box prompt is to put

   SetErrorMode( SEM_FAILCRITICALERRORS );

before the call to CreateFile().

MORE INFORMATION

The following sample code is based on the code in the FLOPPY SDK sample, but it simply displays the media type(s) supported by floppy drive A. The code demonstrates one way to retrieve the supported media without requiring you to insert a floppy disk in the drive.

Sample Code

   #include <windows.h>
   #include <stdio.h>
   #include <winioctl.h>

   DISK_GEOMETRY SupportedGeometry[20];
   DWORD SupportedGeometryCount;

   VOID
   GetSupportedGeometrys( HANDLE hDisk )
   {
      DWORD ReturnedByteCount;

      if( DeviceIoControl(
             hDisk,
             IOCTL_DISK_GET_MEDIA_TYPES,
             NULL,
             0,
             SupportedGeometry,
             sizeof(SupportedGeometry),
             &ReturnedByteCount,
             NULL
         ))
         SupportedGeometryCount = ReturnedByteCount / 
                                  sizeof(DISK_GEOMETRY);

      else SupportedGeometryCount = 0;
   }

   VOID
   PrintGeometry( PDISK_GEOMETRY lpGeometry )
   {
      LPSTR MediaType;

      switch ( lpGeometry->MediaType ) {
         case F5_1Pt2_512:
            MediaType = "5.25, 1.2MB,  512 bytes/sector";
            break;
         case F3_1Pt44_512:
            MediaType = "3.5,  1.44MB, 512 bytes/sector";
            break;
         case F3_2Pt88_512:
            MediaType = "3.5,  2.88MB, 512 bytes/sector";
            break;
         case F3_20Pt8_512:
            MediaType = "3.5,  20.8MB, 512   bytes/sector";
            break;
         case F3_720_512:
            MediaType = "3.5,  720KB,  512 bytes/sector";
            break;
         case F5_360_512:
            MediaType = "5.25, 360KB,  512 bytes/sector";
            break;
         case F5_320_512:
            MediaType = "5.25, 320KB,  512 bytes/sector";
            break;
         case F5_320_1024:
            MediaType = "5.25, 320KB,  1024 bytes/sector";
            break;
         case F5_180_512:
            MediaType = "5.25, 180KB,  512 bytes/sector";
            break;
         case F5_160_512:
            MediaType = "5.25, 160KB,  512  bytes/sector";
            break;
         case RemovableMedia:
            MediaType = "Removable media other than floppy";
            break;
         case FixedMedia:
            MediaType = "Fixed hard disk media";
            break;
         default:
            MediaType = "Unknown";
            break;
      }
      printf("    Media Type %s\n", MediaType );
      printf("    Cylinders %d, Tracks/Cylinder %d, Sectors/Track %d\n",
             lpGeometry->Cylinders.LowPart, lpGeometry->TracksPerCylinder,
             lpGeometry->SectorsPerTrack
            );
   }

   void main( int argc, char *argv[], char *envp[] )
   {
      HANDLE hDrive;
      UINT i;

      hDrive = CreateFile(
                      "\\\\.\\a:",
                      0,
                      FILE_SHARE_READ,
                      NULL,
                      OPEN_ALWAYS,
                      0,
                      NULL
                      );
      if ( hDrive == INVALID_HANDLE_VALUE )
      {
         printf( "Open failed: %d\n", GetLastError() );
         ExitProcess(1);
      }

      GetSupportedGeometrys( hDrive );

      printf( "\nDrive A supports the following disk geometries\n" );
      for( i=0; i<SupportedGeometryCount; i++ )
      {
          printf("\n");
          PrintGeometry( &SupportedGeometry[i] );
      }
      printf("\n");
   }

KBCategory: kbhowto KBSubcategory: BseMisc Additional reference words: 3.10 3.50
Keywords          : kbcode kbnokeyword kbKernBase kbGrpKernBase 
Version           : 3.10 3.50
Platform          : NT WINDOWS

Last Reviewed: May 23, 1998