Deviceiocontrol 2: Obtain the disk/Hard Disk/disc parameters

Source: Internet
Author: User

QIn the demo of msdn, replace the device name with "A:" To obtain the parameters of disk A, read the disk with the resource manager, and then run thisProgramIt can be successful, but it will fail after changing a disk. If you change to "cdrom0", you will not be able to get the CDROM parameter in any case. How can this problem be solved?

AThe floppy disk is used to read formatted Information from a floppy disk, that is, the disk must be read. This is different from the hard disk. Change the access method in createfile to generic_read.

Ioctl_disk_get_drive_geometry is an I/O control code that is valid for floppy disks and hard disks. However, ioctl_disk_get_drive_geometry is useless for removable media such as CD/DVD-ROM and tape. To obtain the CDROM parameter, you must find another way. Ioctl_storage_get_media_types_ex can help us solve the problem.

QWhat input/output data format is required for using these I/O control codes?

AWhen deviceiocontrol uses these two control codes, no data is required.

Ioctl_disk_get_drive_geometry directly outputs a disk_geometry structure:

Typedef struct_ Disk_geometry {large_integer cylinders;// Number of cylindersMedia_type mediatype;// Media typeDWORD trackspercylinder;// Number of tracks per cylinderDWORD sectorspertrack;// Number of sectors per trackDWORD bytespersector;// The number of bytes per slice} Disk_geometry;

Ioctl_storage_get_media_types_ex outputs a get_media_types structure:

 
Typedef struct_ Get_media_types {DWORD devicetype;// Device typeDWORD mediainfocount;// Number of media information itemsDevice_media_info mediainfo [1];// Media information} Get_media_types;

Let's take a look at the definition of the device_media_info structure:

Typedef struct _ Device_media_info { Union { Struct {Large_integer cylinders; // Number of cylinders Storage_media_type mediatype; // Media type DWORD trackspercylinder; // Number of tracks per cylinder DWORD sectorspertrack; // Number of sectors per track DWORD bytespersector; // The number of bytes per slice DWORD numbermediasides; // Number of media faces DWORD mediacharacteristics; // Media features } Diskinfo;// Hard disk Information          Struct {Large_integer cylinders; // Number of cylinders Storage_media_type mediatype; // Media type DWORD trackspercylinder; // Number of tracks per cylinder DWORD sectorspertrack; // Number of sectors per track DWORD bytespersector; // The number of bytes per slice DWORD numbermediasides; // Number of media faces DWORD mediacharacteristics; // Media features } Removablediskinfo; // "Removable disk" Information          Struct {Storage_media_type mediatype; // Media type DWORD mediacharacteristics; // Media features DWORD currentblocksize; // Block size } Tapeinfo; // Tape Information } Devicespecific;} device_media_info;

The CD-ROM is within the range of removable disks. Note that the get_media_types structure only defines one device_media_info, and the additional device_media_info needs to be followed by another space in this structure.

QI have learned how to call the method. Please use VC as an example to implement the functions I have been waiting for a long time?

ANow, we will demonstrate how to obtain the parameters of a floppy disk, hard disk, or disc. During the test, remember to have a floppy disk/CD in the drive!

First, use MFC Appwizard to generate a single-document application named diskgeometry so that its view is based on ceditview.

Then, add the following. h and. cpp files.

//////////////////////////////////////// //////////////////////////////////////// Getdiskgeometry. h /////////////////////////////////////// ///////////////////////////////////////    # If ! Defined (Get_disk_geometry_h __) # Define Get_disk_geometry_h __ # If _ Msc_ver> 1000  # Pragma once  # Endif   // _ Msc_ver> 1000    # Include   <Winioctl. h> Bool getdrivegeometry ( Const char * Filename, disk_geometry * PDG ); # Endif  //! Defined (get_disk_geometry_h __)    //////////////////////////////////////// //////////////////////////////////////// Getdiskgeometry. CPP /////////////////////////////////////// ///////////////////////////////////////    # Include   "Stdafx. H"  # Include   "Getdiskgeometry. H"    // Ioctl_storage_get_media_types_ex may return more than one device_media_info, so sufficient space is defined.  # Define Media_info_size Sizeof (Get_media_types) + 15 * Sizeof (Device_media_info) // Filename -- used for device file name // PDG -- parameter buffer pointer Bool getdrivegeometry ( Const char * Filename, disk_geometry * PDG) {handle hdevice; // Device handle Bool bresult; // Result returned by deviceiocontrol Get_media_types * PMT; // Internal output buffer DWORD dwoutbytes; // Output Data Length        // Open the device Hdevice =: createfile (filename, // File name Generic_read, // The disk needs to be read for the soft drive. File_pai_read | file_pai_write, // Share mode Null, // Default Security Descriptor Open_existing, // Creation Method          0 , // You do not need to set file attributes. Null ); // You do not need to refer to the template file        If (Hdevice = invalid_handle_value ){ // The device cannot be opened...          Return False ;} // Use ioctl_disk_get_drive_geometry to retrieve disk Parameters Bresult =: deviceiocontrol (hdevice, // Device handle Ioctl_disk_get_drive_geometry, // Obtain disk Parameters Null, 0 , // No data input PDG, sizeof (disk_geometry ), // Output data buffer & Dwoutbytes, // Output Data Length (Lpoverlapped) null ); // Use synchronous I/O        // If the request fails, use ioctl_storage_get_media_types_ex to obtain the media type parameter.      If (! Bresult) {PMT = (get_media_types *) New Byte [media_info_size]; bresult =: deviceiocontrol (hdevice, // Device handle Ioctl_storage_get_media_types_ex, // Obtain the media type parameter Null, 0 , // No data input PMT, media_info_size, // Output data buffer & Dwoutbytes, // Output Data Length (Lpoverlapped) null ); // Use synchronous I/O            If (Bresult ){ // Note that the device_media_info structure is extended based on the disk_geometry structure. // for the simplified program, use memcpy to replace the following assignment statements: // PDG-> mediatype = (media_type) PMT-> mediainfo [0]. devicespecific. diskinfo. mediatype; // PDG-> cylinders = PMT-> mediainfo [0]. devicespecific. diskinfo. cylinders; // PDG-> trackspercylinder = PMT-> mediainfo [0]. devicespecific. diskinfo. trackspercylinder ;//...... : Memcpy (PDG, PMT-> mediainfo, Sizeof (Disk_geometry ));} Delete PMT ;} // Close the device handle : Closehandle (hdevice ); Return (Bresult );}

Then, add a button on idr_mainframe of the toolbar with the ID id_get_disk_geometry. Open classwizard, in diskgeometryview

Add the ing function ongetdiskgeometry of id_get_disk_geometry. Open diskgeometryview. cpp, including the header file getdiskgeometry. h.

In ongetdiskgeometry, add the followingCode

     Const char * Szdevname [] = { "\\\\. \ :" , "\\\\. \ B :" ,"\\\\. \ Physicaldrive0" , "\\\\. \ Physicaldrive1" , "\\\\. \ Physicaldrive2" , "\\\\. \ Physicaldrive3" , "\\\\. \ Cdrom0" , "\\\\. \ Cdrom1" ,}; Disk_geometry DG; ulonglong disksize; bool bresult; cstring strmsg; cstring strtmp; For ( Int I = 0 ; I < Sizeof (Szdevname )/ Sizeof ( Char *); I ++) {bresult = getdrivegeometry (szdevname [I], & DG); strtmp. Format ( "\ R \ n % s result = % s \ r \ n" , Szdevname [I], bresult? "Success" : "Failure" ); Strmsg + = strtmp; If (! Bresult) Continue ; Strtmp. Format ( "Media type = % d \ r \ n" , Dg. mediatype); strmsg + = strtmp; strtmp. Format ( "Cylinders = % i64d \ r \ n" , Dg. cylinders); strmsg + = strtmp; strtmp. Format ( "Tracks per cylinder = % LD \ r \ n" , (Ulong) DG. trackspercylinder); strmsg + = strtmp; strtmp. Format ("Sectors per track = % LD \ r \ n" , (Ulong) DG. sectorspertrack); strmsg + = strtmp; strtmp. Format ( "Bytes per sector = % LD \ r \ n" , (Ulong) DG. bytespersector); strmsg + = strtmp; disksize = DG. cylinders. quadpart * (ulong) DG. trackspercylinder * (ulong) DG. sectorspertrack * (ulong) DG. bytespersector; strtmp. format ( "Disk size = % i64d (bytes) = % i64d (MB) \ r \ n" , Disksize, disksize /( 1024 * 1024 ); Strmsg + = strtmp;} cedit & edit = geteditctrl (); Edit. setwindowtext (strmsg );

Finally, what should I do? Compile and run ......

[Related resources]
  • Demo source code: diskgeometry.zip (21kb)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.