Delete and restore the drive letter of the specified volume label

Source: Internet
Author: User

I encountered a problem when I recently completed the configuration. The previous idea was too naive. It may be a bit difficult to implement the pre-defined function. I have to write a filter driver, but I have never touched on the driver before, I don't have the confidence to figure it out in a short time, so it's just like other methods. Later I thought about it. I only had to change my mind, I thought of hiding the icon of the USB flash drive in the resource manager. I didn't think much about it at the time. Later, I changed my mind to delete the USB flash drive at the prompt of a netizen, when it is used, it will be restored, so we will follow this idea, but how can we do it?

 

After a Google review, I found that my predecessors have already studied this problem. I am very pleased to solve this problem according to the methods of netizens. The Code is as follows:Bool hidevolume (maid, maid) <br/>{< br/> bool Bret = false; <br/> tchar szdriveletterandslash [4] = {0 }; <br/> tchar szdriveletter [3] = {0}; <br/> tchar szuniquevolumename [max_path] = {0 }; <br/> // If (lpdriveletter & lpdevice) <br/> // {<br/> szdriveletter [0] = lpdriveletter [0]; <br/> szdriveletter [1] = text (':'); <br/> szdriveletter [2] = text ('/0'); </P> <p> Szdriveletterandslash [0] = lpdriveletter [0]; <br/> szdriveletterandslash [1] = text (':'); <br/> szdriveletterandslash [2] = text ('//'); <br/> szdriveletterandslash [3] = text ('/0 '); <br/>/*} */<br/> If (baddmountpoint) <br/>{< br/> // try to attach lpdevice to lpdriveletter <br/> Bret = definedosdevice (ddd_raw_target_path, szdriveletter, <br/> lpdevice ); </P> <p> If (BRET) <br/>{< br/> If (! Getvolumenameforvolumemountpoint (szdriveletterandslash, <br/> szuniquevolumename, <br/> max_path )) <br/> {<br/> // can't find attached lpdevice's volumename <br/> szuniquevolumename [0] = '/0 '; <br/>}</P> <p> Bret = definedosdevice (<br/> ddd_raw_target_path | ddd_remove_definition | <br/> delimiter, szdriveletter, <br/> lpdevice ); </P> <p> If (! Bret) <br/> return Bret; </P> <p> Bret = setvolumemountpoint (szdriveletterandslash, <br/> szuniquevolumename ); <br/>}< br/> else <br/>{< br/> Bret = deletevolumemountpoint (szdriveletterandslash ); <br/>}< br/> return Bret; </P> <p>}

This function has three parameters:

The first parameter is the drive volume label, such as "C :/";

The second parameter is a string pointing to a DOS device name. It has a special naming method called Universal Naming Convention (UNC). The naming rule is/device/xxx, xxx represents the device number (in my personal understanding), which can be divided into disks and optical drives. Disks (including mobile hard disks and USB disks) are generally harddiskvolumex, and X represents your number on the disk, for example, my C drive device name is "/device/harddiskvolume1", see the msdn: http://msdn.microsoft.com/en-us/library/aa365247 (V = vs.85 ). aspx # maxpath

The third parameter is a hidden flag.

 

The function usage is as follows:

Tchar dospath [max_path]; <br/> querydosdevice (_ T ("D:"), dospath, max_path ); </P> <p> // hide disk D <br/> hidevolume (_ T ("D: //"), null, false ); </P> <p> // recover disk D <br/> hidevolume (_ T ("D: //"), dospath, true); <br/>

 

Note that the device name of the drive must be used when the drive letter is restored. Therefore, before hiding the drive letter, we use the querydosdevice function to save the device name to the dospath, note that the drive letter in this function does not require a backslash. For details, see msdn: http://msdn.microsoft.com/en-us/library/aa365461 (vs.85 ). aspx

 

In this way, the specified drive is hidden and cannot be accessed in command line mode. The specified path cannot be found, but this is an indicator that is not permanent, you only need to re-allocate a drive letter to the disk manager to restore access.

 

There is another method, but this is just a blind way, that is, to use the shchangenoworkflow function that was not publicly available in Windows to fool the system. The specific usage is as follows:

// Hide the D disk <br/> shchangenotify (shcn_delete, _ T ("D: //"), null );

This function is used to notify the system of file changes. We do not want the D disk to be displayed. We only need to notify the system that the disk has been "deleted", but we only need to refresh it, the disk is shown again, which is why I say it is a blind eye.

 

References:

1. http://www.cnitblog.com/wangk/archive/2006/01/10/6106.html

2. http://topic.csdn.net/u/20100819/09/002a1f13-0dfc-466f-9f61-73f4b7f684ee.html

3. http://msdn.microsoft.com/en-us/library/aa363904 (V = vs.85). aspx

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.