A actual DeviceIocontrol one: Access the device driver via API

xiaoxiao2021-03-06  51

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, // device handle DWORD dwIoControlCode, // control code LPVOID lpInBuffer, // pointer to the input data buffer DWORD nInBufferSize, // input data buffer length LPVOID lpOutBuffer, // the output data buffer pointer DWORD nOutBufferSize, // Output Data Buffer Length LPDWORD LPBYTESRETURNED, / / ​​Output Data Real Length Unit LPoverLapped Lpoverlapped // Overlapping Operation 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 mode DWORD dwShareMode, // sharing LPSECURITY_ATTRIBUTES lpSecurityAttributes, // security descriptor pointer DWORD dwCreationDisposition, // create a way DWORD dwFlagsAndAttributes, // file attributes And the handle of the symbol Handle HTemplateFile // 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, the 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 partitions C:, D:, E:, ... Physical Drive PhysicalDriveX CD-ROM, DVD / ROM CDROMX 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 interest 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 bee exampled bool bresult; // results flag dword; // 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) // can not open The drive {return (false);} BRESULT = DeviceIocontrol (HDevice, // device to be queried IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform NULL, 0, // no input buffer pdg, 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 = getDriveGeometry (& PDG); if (BRESULT) {Printf ("

Cylinders =% i64d ", pdg.cylinders); Printf (" TRACKS Per Cylinder =% LD

", (Ulong) pdg.trackspercylinder; Printf (" Sectors Per TRACK =% LD

", (Ulong) pdg.sectorSpertrack; Printf (" Bytes per sector =% LD

", (ULONG) pdg.BytesPerSector); DiskSize = pdg.Cylinders.QuadPart * (ULONG) pdg.TracksPerCylinder * (ULONG) pdg.SectorsPerTrack * (ULONG) pdg.BytesPerSector; printf (" Disk size =% I64d (Bytes) =% I64D (MB)

", Disksize, Disksize / (1024 * 1024));} else {printf (" GetDriveGeometry Failed. Error% LD.

", 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.

转载请注明原文地址:https://www.9cbs.com/read-82853.html

New Post(0)