How to use Volinfo to get logical drives

Source: Internet
Author: User
Tags bitmask file system

1. How do I get a logical drive with Volinfo?

2. How to modify the system menu with C # programming?

I'm trying to write an MFC program that lists the drives on the system (C:,D: etc.), and I also need to know if the drive is a hard drive or an optical drive. Is there such a class to get this information?

There is no MFC class that provides this information, but Microsoft®windows® has a set of volume management functions to do this. This includes functions for obtaining logical drives and volume-hanging contacts (mount points) information, which is not included in the Advanced NTFS feature. For your request, you can just process the logical drive information. Figure 1 shows the correlation function.

There are four basic functions: GetLogicalDrives, GetLogicalDriveStrings, GetDriveType and GetVolumeInformation. The fifth one is Setvolumelabel, and if you want, you can use it to set the volume label. These functions are fairly straightforward and, for ease of use, I encapsulate them in a friendly MFC class: Cvolumemaster, (see Figure 2), which allows you to process CString instead of TCHAR arrays. I also wrote an example program: VolInfo.exe, demonstrating how to use this MFC class. You can download the source code from the hyperlink at the beginning of this article. Figure 3 is the detailed information that the Volinfo example program displays when it runs on my own computer.

Figure 3 Detailed drive information

The first function, GetLogicalDrives, returns a DWORD bitmask (bitmask) value to tell the driver letter. "0" means drive A, "1" means drive B, and so on. As you know, there are 26 letters in the English alphabet, and there are 32 in the DWORD, and those of you who are good at math will quickly deduce that a DWORD can provide enough space to hold all, or a combination of some possible drive letters. Fortunately, Microsoft (Redmond) is not in northwest Leah (the ancient Slavic alphabet has 33 letters), Cvolumemaster has a static method Formatbitmask, Its bitmask information is formatted as an ASCII----the Volinfo program applies it to display a message like the following:

10110 10001 11000 00000 00000 00000 00

This message indicates that there are drives in My computer: A, C, D, F, J, K, and L. Yo! If your cerebral cortex is programmed in binary form, sooner or later there will be a function of getlogicaldrivestrings, which returns an important string representing all the letters of the drive. Each drive letter has the form d:\ (trailing a ' \ "), where D represents the drive letter, each string has an empty (null) non-terminal, and the end has two null. Now that you know how to use tchars processing very restrained, then write a handy cvolumemaster package, with CStringArray to save the drive letter. After all, this is a C + + column. You just write the following code:

CVolumeMaster vm;
  CStringArray arDrives;
  int n = vm.GetLogicalDriveStrings(arDrives);

Now Ardrives is the drive letter string and n is the number of logical drives. You get it?

What drives do you know, but how do you know what the different drives are? This is what GetDriveType is designed to do. GetDriveType returns a code, such as Drive_fixed on behalf of the hard disk, or Drive_cdrom represents the CD-ROM drive. Cvolumemaster has a static function that formats the code as an identifiable string; Volinfo uses it as output. For more information, see Source code.

Finally, if you want to learn more about a logical drive, such as its volume label, the file system it uses or whether the drive supports named streams (named streams) and encryption, call the GetVolumeInformation function. This Swiss saber function gets the volume label, file system name (for example, NTFS or FAT), volume serial number, file system identity, and maximum component length.

You ask, "What the hell is the maximum component length?" "That means a file system representation of the length of the path name between the backslash characters." In other words, if the path name is C:\mumble\bletch\oops, then "mumble", "Bletch" and "oops" are components, and each component has a limit of length. With Volinfo, you can find that the maximum length of the NTFS support component is 255, while the CD-ROM is usually only 127. This explains why when you save all your MP3 to a CD, you often get a message telling you that some filename or something else is too long and asking if it's truncated.

Cvolumemaster has a getvolumeinformation version of it-it uses CString instead of LPTSTR.

CString volname,filesys;
DWORD serno, maxcomplen, flags;
vm.GetVolumeInformation("C:\", volname, serno, maxcomplen, flags, filesys);

At the same time, the reason I insist on using CString is not because it is easier, it is also safe. Even Arnold Schwarzenegger knows what a buffer overflow is today when it's focused on security and the ravages of malicious viruses. Using CString is a good way to avoid it.

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.