Obtain the path, category, and information of each drive under VC.

Source: Internet
Author: User

1. Get all the drives
Use Functions
Getlogicaldrivestrings

The getlogicaldrivestrings function fills a buffer with strings that specify valid drives in the system.

DWORD getlogicaldrivestrings (
DWORD nbufferlength, // size of Buffer
Lptstr lpbuffer // drive Strings Buffer
);

A very simple function. msdn provides a detailed description.
Note that

The last data obtained in lpbuffer is C:/<null> D:/<null>. Every two paths are separated by a null-terminated,
Therefore, if you directly cout <lpbuffer, the resulting result is C:/, which is depressing. Therefore, you need to obtain these paths one by one.

Therefore, with the following code
Tchar szbuf [100];
Memset (szbuf, 0,100 );
DWORD Len = getlogicaldrivestrings (sizeof (szbuf)/sizeof (tchar), szbuf );

For (tchar * s = szbuf; * s; S + = _ tcslen (s) + 1)
{
Lpctstr sdrivepath = s;

Cout <sdrivepath
}

Therefore, this sdrivepath is a string that is similar to C:/and D :/.

 

2. Get the drive type
Now we have obtained the drive path, such as C:/, D :/
How can we differentiate them,
This function is available

Getdrivetype
The getdrivetype function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.

Uint getdrivetype (
Lpctstr lprootpathname // root directory
);

Uint udrivetype = getdrivetype (sdrivepath );
After the function is called, the return value of this function is

Value Meaning
Drive_unknown the drive type cannot be determined.
Drive_no_root_dir the root path is invalid. For example, no volume is mounted at the path.
Drive_removable the disk can be removed from the drive.
Drive_fixed the disk cannot be removed from the drive.
Drive_remote the drive is a remote (network) Drive.
Drive_cdrom the drive is an CD-ROM drive.
Drive_ramdisk the drive is a RAM disk.

However, many things cannot be distinguished by this function. For example, the drive and the inserted USB flash drive are drive_removable, while the hard drive and the inserted mobile hard drive are drive_fixed.
Rely on :(

Let's try it one by one.

3. Get the optical drive
Pinch soft persimmon first :)

Uint udrivetype = getdrivetype (sdrivepath );

If (udrivetype = drive_cdrom)
{
That's it.
}

It should be noted that, although drive_cdrom is written
But the DVD drive can also be obtained (this is not nonsense). In addition, the virtual drive can also be obtained. For example, alcohol 120% is installed on the machine, and the virtual drive can also be obtained.

4. differentiate between a soft drive and a USB flash drive

Paste the code first
# Define media_info_size sizeof (get_media_types) + 15 * sizeof (device_media_info)

Bool getdrivegeometry (const tchar * filename, disk_geometry * PDG)
{
Handle hdevice; // device handle
Bool bresult; // return result of 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, // Sharing Method
Null, // Default Security Descriptor
Open_existing, // Creation Method
0, // file attributes do not need to be set
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 input data
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, // gets the media type parameter
Null, 0, // no input data
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
Disk_geometry DG;
Tchar szpath [100] = _ T ("////.//");
: _ Tcscat (szpath, sdrivepath );
Int nsize =: _ tcslen (szpath );
Szpath [nSize-1] = '/0 ';

Bool bretval = getdrivegeometryszpath, & DG );

If (DG. mediatype = removablemedia)
{
This is the USB flash drive.
}

This Code, wow, it's so troublesome. I don't know much about it.
First look at the structure of disk_geometry.

Media_type is an enumeration type.
Quoa

The details are not listed. You can view all

A very important function is: deviceiocontrol, which can obtain many attributes.

The first parameter is a handle.
Call: createfile. I am dizzy. Isn't this a file :)
In fact, this function does not have to create only one file in the traditional sense as we imagined.
Here we use it to "open" the device driver to get the device handle. After the operation is complete, use closehandle to close the device handle.

Here are the following small changes. If the path is selected as a drive, the path format is
//./Devicename
For example
//./C:
It's really BT,
So
Tchar szpath [100] = _ T ("////.//");
: _ Tcscat (szpath, sdrivepath );
Int nsize =: _ tcslen (szpath );
Szpath [nSize-1] = '/0 ';
These codes
Set C:/=> //./C:
For more information about the function: deviceiocontrol, see
Http://dev.csdn.net/article/55/55510.shtmthis series, I also refer to this series ^ + ^

Of course, there are still a lot to be discussed about how to distinguish between a soft drive and a USB flash drive. For example, some people have suggested that a soft drive is all in a:, and the path below is not enough, or the size is 1.44 MB.
In addition, if the USB flash drive and USB flash drive are differentiated, how can we differentiate them if something is inserted on the USB port, such as a card reader or camera ??
There are still many things worth learning about ~~~~

4. differentiate between a mobile hard disk and a hard disk
I said, the mobile hard drive is drive_fixed, which is really enough for Bt. If you haven't done it before, it's hard to imagine. It's too BT.
How to differentiate

Use deviceiocontrol to obtain the ioctl_storage_query_property under the volume
Return the storage_bus_type in the storage_device_descriptor structure.

Code
# Include <DBT. h>
# Include <winioctl. h>
// IOCTL control code
# Define ioctl_storage_query_property ctl_code (ioctl_storage_base, 0x0500, method_buffered, file_any_access)

Typedef Enum _ storage_property_id {
Storagedeviceproperty = 0,
Storageadapterproperty,
Storagedeviceidproperty
} Storage_property_id, * pstorage_property_id;

Typedef Enum _ storage_query_type {
Propertystandardquery = 0,
Propertyexistsquery,
Propertymaskquery,
Propertyquerymaxdefined
} Storage_query_type, * pstorage_query_type;

Typedef struct _ storage_property_query {
Storage_property_id propertyid;
Storage_query_type querytype;
Uchar additionalparameters [1];
} Storage_property_query, * pstorage_property_query;

 

Typedef struct _ storage_device_descriptor {
Ulong version;
Ulong size;
Uchar devicetype;
Uchar devicetypemodifier;
Boolean removablemedia;
Boolean commandqueueing;
Ulong vendoridoffset;
Ulong productidoffset;
Ulong productrevisionoffset;
Ulong serialnumberoffset;
Storage_bus_type bustype;
Ulong rawpropertieslength;
Uchar rawdeviceproperties [1];
} Storage_device_descriptor, * pstorage_device_descriptor;

 

Handle hdevice; // device handle
Bool bresult; // return result of deviceiocontrol

// Open the device
Hdevice =: createfile (szpath, // file name
Generic_read, // the disk needs to be read by the drive.
File_pai_read | file_pai_write, // Sharing Method
Null, // Default Security Descriptor
Open_existing, // Creation Method
0, // file attributes do not need to be set
Null );
 

If (hdevice = invalid_handle_value)
{
Return false;
}

Storage_property_query query; // input Param for query
DWORD dwoutbytes; // IOCTL output Length

Query. propertyid = storagedeviceproperty;
Query. querytype = propertystandardquery;

Storage_device_descriptor pdevdesc;

Pdevdesc. size = sizeof (storage_device_descriptor );

// Use ioctl_storage_query_property

Bresult =: deviceiocontrol (hdevice, // device handle
Ioctl_storage_query_property, // info of device property
& Query, sizeof (storage_property_query), // input data buffer
& Pdevdesc, pdevdesc. Size, // output data buffer
& Dwoutbytes, // out's length
(Lpoverlapped) null );

Uint type = pdevdesc. bustype;

// Unknown 0x00
// SCSI 0x01
// Atapi 0x02
// Ata 0x03
// Ieee1394 0x04
// SSA (Serial Storage Architecture) 0x05
// Fibre channel, 0x06
// USB, 0x07
// Raid, 0x08

In this way, the USB hard disk and general hard disk can be distinguished.

References
Http://www.codeproject.com/w2k/usbdisks.asp

The article was written here, and I went back and tried it again. In 3. to tell the truth, I am not very satisfied with the method when distinguishing between a USB flash drive and a soft drive. I feel a little confused. It is too complicated and I can't help it. My younger brother is really stupid.
In fact, the query in Method 4 should also be differentiated, because the USB flash drive bustype is USB, and I cannot try it because there is a soft drive on the machine.
But I believe it won't be USB, hey

Okay. Write it here first.

Postscript:
In fact, writing this thing is actually a learning process: the deviceiocontrol function is used too much. Sometimes, to implement a function, you can use different parameters to call this function, to get the desired results
In addition, there are still many things worth further study. For example, how to distinguish other devices? There are too many devices that can be connected to the computer now. Isn't there any legendary USB that is hot ?? Haha, starting with YY ~~~ ^ + ^

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.