In practice, deviceiocontrol Series II: Obtain the parameters of a floppy disk, hard disk, or optical disk.

Source: Internet
Author: User

Q: In the msdn demo, replace the device name with "A:" and take the disk parameter. Read the disk with the resource manager, and then run the program, however, after changing the disk, it will fail. If you change to "cdrom0", the CDROM parameter will not work in any case. How can this problem be solved?

A:

The 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
Generic_read.

Ioctl_disk_get_drive_geometry I/O
The control code is valid for floppy disks and hard disks, but it is useless for some 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.

 

Q:
What input/output data format is required for using these I/O control codes?

A:

When 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 cylinders
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
} Disk_geometry;
Ioctl_storage_get_media_types_ex
Output A get_media_types structure:
Typedef struct _ get_media_types {
DWORD devicetype; // device type
DWORD mediainfocount ;//
Number of media information entries
Device_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 ;//
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; // number of bytes per sector
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;

Where
CD-ROM is in the range of removable disks. Note that the get_media_types structure only defines one
Device_media_info. The additional device_media_info needs to be added to another space in this structure.

 

Q: I 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?

A:

Now, 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 generates 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,
Therefore, sufficient space is defined.
# Define
Media_info_size
Sizeof (get_media_types) + 15 * sizeof (device_media_info)
  
// Filename -- Used for the device file name
// PDG -- parameter buffer pointer
Bool
Getdrivegeometry (const char * filename, disk_geometry * PDG)
{
Handle hdevice; // device handle
Bool bresult; // 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 by the drive.
File_pai_read
| File_pai_write, // share mode
Null, // Default Security Descriptor
 
Pen_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 it fails, use it again
Ioctl_storage_get_media_types_ex
If (! Bresult)
{
  
PMT = (get_media_types *) New byte [media_info_size];
 
    
Bresult =: deviceiocontrol (hdevice, // device handle
      
Ioctl_storage_get_media_types_ex, // gets 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.
//
To simplify the program, replace memcpy with 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, ID
Id_get_disk_geometry. Open classwizard and add
The ing function ongetdiskgeometry of id_get_disk_geometry. Open diskgeometryview. cpp, including the header file getdiskgeometry. h.

In
In ongetdiskgeometry, add the following code

     const char *szDevName[]=
  
{
    ".A:",
    ".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("rn%s
 result = %srn", szDevName[i], bResult ? "success" : "failure");
 
   strMsg+=strTmp;
 
    if (!bResult) continue;
 
    strTmp.Format("  Media Type = %drn", dg.MediaType);
    strMsg+=strTmp;
 
    strTmp.Format("  Cylinders = %I64drn",
dg.Cylinders);
    strMsg+=strTmp;
 
    strTmp.Format("  
Tracks per cylinder = %ldrn", (ULONG)dg.TracksPerCylinder);
    strMsg+=strTmp;
 
    strTmp.Format("  Sectors per track = %ldrn",
(ULONG)dg.SectorsPerTrack);
    strMsg+=strTmp;
 
    strTmp.Format("  Bytes per sector = %ldrn", (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)rn",DiskSize, DiskSize / (1024 * 1024));
    strMsg+=strTmp;
  }
 
  CEdit& Edit = GetEditCtrl();
 
   Edit.SetWindowText(strMsg);

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

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.