Calling DeviceIoControl on Windows NT
On Windows NT, an application can use the DeviceIoControl function to perform direct input and output operations on, or retrieve information about, a floppy disk drive, hard disk drive, tape drive, or CD-ROM drive. The following example demonstrates how to retrieve information about the first physical drive in the system. It uses the CreateFile function to obtain the device handle of the first physical drive, and then uses the DeviceIoControl function with the IOCTL_DISK_GET_DRIVE_GEOMETRY control code to fill a DISK_GEOMETRY structure with information about the drive.
BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)
{
HANDLE hDevice;
BOOL fResult;
DWORD cb;
hDevice = CreateFile("\\\\.\\PhysicalDrive0",
0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == NULL)
return FALSE;
fResult = DeviceIoControl(hDevice,
IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
pdg, sizeof(*pdg), &cb, (LPOVERLAPPED) NULL);
if (!fResult)
return FALSE;
CloseHandle(hDevice);
}
This example succeeds only when it runs on Windows NT, for two reasons:
·The standard device input/output control codes are available only on Windows NT, and
·On Windows 95, an application must specify a virtual device driver in the CreateFile function ¾ not a specific device.