INFO: Direct Drive Access Under Win32

This article was previously published under Q100027
To open a physical hard drive for direct disk access (raw I/O) in aWin32-based application, use a device name of the form
where N is 0, 1, 2, and so forth, representing each of the physical drivesin the system.

To open a logical drive, direct access is of the form
where X: is a hard-drive partition letter, floppy disk drive, or CD-ROMdrive.
You can open a physical or logical drive using the CreateFile() applicationprogramming interface (API) with these device names provided that you havethe appropriate access rights to the drive (that is, you must be anadministrator). You must use both the CreateFile() FILE_SHARE_READ andFILE_SHARE_WRITE flags to gain access to the drive.

Once the logical or physical drive has been opened, you can then performdirect I/O to the data on the entire drive. When performing direct diskI/O, you must seek, read, and write in multiples of sector sizes of thedevice and on sector boundaries. Call DeviceIoControl() usingIOCTL_DISK_GET_DRIVE_GEOMETRY to get the bytes per sector, number ofsectors, sectors per track, and so forth, so that you can compute the sizeof the buffer that you will need.

Note that a Win32-based application cannot open a file by using internalWindows NT object names; for example, attempting to open a CD-ROM drive byopening
does not work because this is not a valid Win32 device name. An applicationcan use the QueryDosDevice() API to get a list of all valid Win32 devicenames and see the mapping between a particular Win32 device name and aninternal Windows NT object name. An application running at a sufficientprivilege level can define, redefine, or delete Win32 device mappings bycalling the DefineDosDevice() API.

Article ID: 100027 - Last Review: 05/06/2003 18:12:00 - Revision: 2.0

Microsoft Win32 Application Programming Interface

  • kbinfo kbapi kbkernbase kbfileio KB100027