Q In NT / 2000 / XP, I want to write an application to access the hardware device with the VC, such as get disk parameters, read and write absolute sector data, test the actual speed of the optical drive, etc., where to start?
A In NT / 2000 / XP, the application can achieve access to the device through the API function deviceioControl, send commands, exchange data, etc. Use the interface function to drive the correct control code and data to the specified device, then analyze its response, you can achieve our goal.
DeviceIocontrol's function prototype
Bool DeviceIocontrol
Handle HDevice, // Equipment handle
DWORD DWIOCONTROLCODE, // Control code
LPVOID LPINBUFFER, // Enter the data buffer pointer
DWORD NINBUFFERSIZE, / / Enter the data buffer length
LPVOID LPOUTBUFFER, / / Output Data Buffer Pointer
DWORD NOUTBUFFERSIZE, / / Output Data Buffer Length
LPDWORD LPBYTESRETURNED, / / Output Data actual length unit length
LPOVERLAPPED LPOVERLAPPED / / Operating Structure Pointer
);
The device handle is used to identify the device you visit.
Send different controls, you can call different types of features of the device driver. In the header file WiniocTl.h, a predefined standard device control code is starting with IOCTL or FSCTL. For example, IOCTL_DISK_GET_DRIVE_GEOMETRY is a control code for the physical drive to take structural parameters (media type, cylindrical number, number of squares per column area, number per track sector, etc.), FSCTL_LOCK_VOLUME is a control code that locks the logical drive.
Input Output Data buffer is required, what structure, and how many bytes are determined by different operation types of different devices. In the header file WiniocTl.h, some input output data structures have been predefined for standard devices. The overlapping operation structure pointer is set to null, and DeviceIoControl will block the blocking call; otherwise, it should be designed according to asynchronous operation when programming.
Q Where did the Q device handle get?
The A device handle can be obtained with the API function CreateFile. Its prototype
Handle Createfile
LPCTSTR LPFILENAME, / / File Name / Device Path
DWord dwdesiredAccess, // Access method
DWORD dwsharemode, // shared method
LPSecurity_attributes LPSecurityAttributes, // Security Descriptor Pointer
DWORD dwcreationdisposition, // creation method
DWORD dwflagsandattributes, // file properties and logos
Handle HTemplateFile // Handle of Template file
);
Createfile This function is much more, here we use it "Open" device driver to get the handle of the device. After the operation is complete, turn off the device handle with CloseHandle.
Unlike normal file names, device-driven "file name" (often referred to as "device path") is fixed to "//./ deviceName" (note that the string is written in the C program ".//Devicename "), DeviceName must match the device name defined within the device driver. Generally, when calling createfile obtains a device handle, the access mode parameter is set to 0 or generic_read | generic_write, the shared mode parameter is set to file_share_read | file_share_write, the creation mode parameter is set to Open_EXISTING, and the other parameters are set to 0 or NULL.
Q, how do I know what the device name is?
A Some storage devices are known to be Microsoft definition, and it is impossible to change. Generally listed as follows
Soft disk drive A:, b: hard disk logic partition C:, d:, e:, ... Physical Drive PhysicalDriveX CD-ROM, DVD / ROMCDROMX Telt machine TAPEX
Among them, physical drivers do not include floppy drives and optical drives. The logical drive can be a hard disk partition (volume), optical drive, Mo, CF card, etc. of the IDE / SCSI / PCMCIA / USB interface, or even a virtual disk. X = 0, 1, 2 ...
Other equipment names need to be used by the GUID calling device management function of the drive interface, which is not discussed here.
Q Please give a simple example to explain how to access the device driver via DeviceIocontrol.
A here has a DEMO program from the MSDN, demonstrating how the basic parameters of the hard drive through DeviceioControl in NT / 2000 / XP.
/ * The code of interterest is in the subroutine getDriveGeometry. The
Code in main shows how to interpret the results of the ioctl call. * /
#include
#include
Bool getDriveGeometry (Disk_geometry * PDG)
{
Handle Hdevice; // Handle to the Drive To Be EXAMINED
Bool Bresult; // Results Flag
DWORD JUNK; // Discard RESULTS
HDevice = Createfile (".// PhysicalDrive0", // Drive To Open
0, // no access to the drive
File_share_read | // Share Mode
File_share_write,
Null, // Default Security Attributes
Open_existing, // disposition
0, // File Attributes
Null); // Do Not Copy File Attributes
IF (hdevice == invalid_handle_value) // canNot Open the Drive
{
Return (False);
}
BRESULT = Deviceiocontrol (HDevice, // device to be queried
IOCTL_DISK_GET_DRIVE_GEOMETRY, / / OPERATION TO PERFORM
NULL, 0, // no Input Bufferpdg, SizeOf (* PDG), // Output BUFFER
& junk, // # bytes returned
(LPOVERLAPPED) NULL); // Synchronous I / O
CloseHandle (HDEvice);
Return (BRESULT);
}
Int main (int Argc, char * argv [])
{
Disk_geometry PDG; // Disk Drive Geometry Structure
Bool Bresult; // Generic Results Flag
Ulonglong disksize; // size of the drive, in bytes
BRESULT = GETDRIVEMETRY (& PDG);
IF (BRESULT)
{
Printf ("cylinders =% i64D / n", pdg.cylinders;
Printf ("Tracks Per Cylinder =% LD / N", (Ulong) pdg.trackspercylinder
Printf ("Sectors Per TRACK =% LD / N", (Ulong) PDG.sectorspertrack);
Printf ("Bytes Per Sector =% LD / N", (Ulong) pdg.bytespector;
Disksize = pdg.cylinders.quadpart * (ulong) pdg.trackspercylinder *
(Ulong) pdg.sectorspertrack * (ulong) PDG.BYTESPERSECTOR;
Printf ("Disk size =% i64d (bytes) =% i64D (MB) / N", Disksize,
Disksize / (1024 * 1024));
}
Else
{
Printf ("GetDriveGeometry Failed. Error% LD. / N", getLastError ());
}
Return (Int) BRESULT
}
Q If you use the device name to "A:", you can take the A disk parameters, replace it with "CDROM0" to take the CDROM parameters, is this?
A This problem does not answer. Try it back.
Now we summarize the "three steps" to access the device driver via Deviceiocontrol: First obtain the device handle with CreateFile, then use DeviceioControl and the device I / O, and finally don't forget to close the device handle with CloseHandle.
[related resources]
BHW98 column: http://www.9cbs.net/develop/author/netauthor/bhw98/
First release: 2003-02-16 Last revision: 2003-05-20