Jump to content
Mbee

Any code to Add/Remove drive letters like Disk Manager?

Recommended Posts

Mbee

Occasionally I need to make more drive letters available for other disks I will be using.  To do this using the Disk Manager, one selects a currently mounted drive that has a letter assigned, right-click, and choose "Change Drive Letter and Paths...", then click the Add or Remove buttons and so on. Note that I'm not trying to do anything even the slightest bit complex, such as mapping drives or folders or working over a network.  I just want to remove and later re-add drive letters to currently mounted disks/partitions.  I'd prefer to do this directly inside AutoIt rather than via the shell/explorer.

I see there are very promising _WinAPI functions that I could put together to do this, such as "_WinAPI_GetVolumeNameForVolumeMountPoint", "_WinAPI_SetVolumeMountPoint", and "_WinAPI_DeleteVolumeMountPoint".  I'm sure I could accomplish what I need with these.  But I don't want to re-invent the wheel if this work has already been done.  Does anyone know if it has?

If not, I'll code it up and submit it as a UDF, so let me know and I'll do that..

Thanks!

Share this post


Link to post
Share on other sites
BrewManNH

Have you looked at DriveMapAdd and DriveMapDel in the help file?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Mbee
On 4/18/2018 at 9:56 AM, BrewManNH said:

Have you looked at DriveMapAdd and DriveMapDel in the help file?

Hi, BrewManNH! Yes, I looked at those, but with the second parameter being a network path string, it didn't look like the best way to go.

Edited by Mbee

Share this post


Link to post
Share on other sites
BrewManNH

What exactly are you trying to do then? If they're not network drives, then what are they?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Mbee
2 hours ago, BrewManNH said:

What exactly are you trying to do then? If they're not network drives, then what are they?

First, I've made very good progress so far, but my next post will be a request for assistance in figuring out how to handle a parameter returned from a particular DLL call...

But to answer your question, I'm developing a desktop tool that will operate on hard disks, with or without drive letters (e.g., mounted or unmounted) currently on the system. I've got that part working. The primary purpose of the application is to allow the user to easily free up drive letters and later return those drive letters to the disks they were previously assigned to (if that drive letter is available).  I doubt anyone else will ever want or use it, but I'm writing it for myself.

To perform the necessary operations, I needed WinAPI calls that, to the best of my knowledge, have not had an AutoIt interface function written for them,  so what I'm doing now is preparing a UDF with those function interfaces. I have completed three of the probable 5 I'll need, which are: _WinAPI_FindFirstVolume(), _WinAPI_FindNextVolume(), and _WinAPI_FindVolumeClose(). These were needed because, since not all drives will currently have letters, I had to find a way to get that info some other way.

Share this post


Link to post
Share on other sites
Mbee

I need some help coding up a WinAPI DLL call to GetVolumeNameForVolumeMountPoint(). The problem is that one of the returns to the WinAPI function is a null-terminated string that contains one or more null-terminated strings.  Here's the MSDN page: 

https://msdn.microsoft.com/en-us/library/windows/desktop/aa364994(v=vs.85).aspx

I checked the AutoIt help file looking for how to specify a nested string in a DLL call, but either my comprehension is terrible (a very distinct possibility!) or it offered no guidance.

 

.. NEVER MIND.  I think I know how to do it..

Share this post


Link to post
Share on other sites
BrewManNH

Try with Mountvol <Driveletter:> /d to remove the name, but keep the drive mounted, or /p to remove the letter and unmount the drive.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Mbee
19 minutes ago, BrewManNH said:

Try with Mountvol <Driveletter:> /d to remove the name, but keep the drive mounted, or /p to remove the letter and unmount the drive.

Aww, you're no fun (you fell right over! - Firesign ref?).  Yeah, but I really want to do this internally in AutoIt rather than invoke external commands...

Share this post


Link to post
Share on other sites
Bilgus

https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_GetVolumeNameForVolumeMountPoint.htm

and the include file..

;;WinAPIFiles.au3
Func _WinAPI_GetVolumeNameForVolumeMountPoint($sMountedPath)
    Local $aRet = DllCall('kernel32.dll', 'bool', 'GetVolumeNameForVolumeMountPointW', 'wstr', $sMountedPath, 'wstr', '', 'dword', 80)
    If @error Or Not $aRet[0] Then Return SetError(@error + 10, @extended, '')

    Return $aRet[2]
EndFunc   ;==>_WinAPI_GetVolumeNameForVolumeMountPoint

 

Share this post


Link to post
Share on other sites
Mbee
36 minutes ago, Bilgus said:

https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_GetVolumeNameForVolumeMountPoint.htm

and the include file..

;;WinAPIFiles.au3
Func _WinAPI_GetVolumeNameForVolumeMountPoint($sMountedPath)
    Local $aRet = DllCall('kernel32.dll', 'bool', 'GetVolumeNameForVolumeMountPointW', 'wstr', $sMountedPath, 'wstr', '', 'dword', 80)
    If @error Or Not $aRet[0] Then Return SetError(@error + 10, @extended, '')

    Return $aRet[2]
EndFunc   ;==>_WinAPI_GetVolumeNameForVolumeMountPoint

 

Hi, Bilgus, and thanks!  Nice to hear from you...

Yeah, sorry about that.  I entered the wrong function name.  I needed _WinAPI_GetVolumePathNamesForVolumeName(), which I still can't find, so I started coding it, but I ran into a puzzle.  I'd certainly appreciate the help!

You see, the main return from that function is not purely an AutoIt-compatible (i.e., null-terminated) string. It's a MULTI_SZ, I guess, of zero-terminated strings, with a double zero terminating the entire string.  Now I figure that 98% of the time, it will only consist of a single ZTS -- namely the drive letter path.  But since MSDN states that it might well return multiple substrings, I don't know the best way of handling that return.

At first I was planning on dividing  it myself within _WinAPI_GetVolumePathNamesForVolumeName() such that it would return an array of strings, but I'm not at all sure that the multi-SZ string can properly be handled by native AutoIt functions.

How would you do it, wise sir?

Edited by Mbee

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×