To change the drive letter without restarting machine

Hi!
I have written code to change name of the drive, for example drive 'D' to drive 'E' it runs successfully but to reflect the changes i need to restart my machine. So please provide any solution which will cause to reflect the changes without restarting my machine. I have made necessary changes in the registry by code but these changes are not reflected immediately for that i have to restart my machine plz if any suggestion.

Thanks in advance.
Last edited on
If by any solution you mean to include non C++ solutions then this is three lines of code in VBS:

1
2
3
Set objNetwork = CreateObject("WScript.Network")
objNetwork.RemoveNetworkDrive "E:"
objNetwork.MapNetworkDrive "F:" , "\\Server\Folder"


"E:" and "F:" Being the drive letters of your choice of course.

For simple things like this VBS is better suited, however if this is part of a larger program please say so and I will help you further.

EDIT: Doing this does NOT require a restart.
Last edited on
Thanks for your reply.

But when I am trying to rename drive letter from the computer management it works fine and we don't have to restart the machine. so i just want to know which are the windows API's are calling while renaming the drive letter. I need a solution which is totally part of C++.

Thanks in advance.
What does your code do, rename registry entries?
please refer the fallowing code for changing drive name 'E' to 'D'



#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
#include<iostream>
#include<atlstr.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CString newstr,oldstr;
long result;
HKEY hkey;
DWORD Type = REG_BINARY;
DWORD cbData;
char ch;

//Select the name of the drive to change.
ch=(char)'E';
oldstr.Format(L"%c",ch);

//Select the new name of the drive.
ch=(char)'D';
newstr.Format(L"%c",ch);

oldstr="\\DosDevices\\"+oldstr;
newstr="\\DosDevices\\"+newstr;
oldstr=oldstr +':';
newstr=newstr +':';

//Open the specified registry key.
result=RegOpenKeyEx(HKEY_LOCAL_MACHINE,L"System\\MountedDevices",0,
KEY_ALL_ACCESS|KEY_WRITE|KEY_SET_VALUE,&hkey);


result = RegQueryValueEx(hkey,oldstr.GetBuffer(oldstr.GetLength()),
NULL,&Type,NULL,&cbData);

LPBYTE dat;
dat = (LPBYTE) malloc( cbData );

cbData = cbData + 1;
result = RegQueryValueEx(hkey,oldstr.GetBuffer(oldstr.GetLength()),
NULL,&Type,dat,&cbData);

if(result!=ERROR_SUCCESS)
{
MessageBox(NULL,L"Failed to Query value",L"Error", MB_OK | MB_ICONERROR );
return 0;
}

// Set new value into the registry.
result = RegSetValueEx(hkey,newstr.GetBuffer(newstr.GetLength()),NULL,Type,dat,cbData);
if(result!=ERROR_SUCCESS)
{
MessageBox(NULL,L"Failed to set the value in Registry",L"Error", MB_OK | MB_ICONERROR );
return 0;
}

// Now the old drive name is deleted in the registry

result = RegDeleteValue(hkey,oldstr.GetBuffer(oldstr.GetLength()));
if(result!=ERROR_SUCCESS)
{
MessageBox(NULL,L"Failed in deleting the Value",L"Error", MB_OK | MB_ICONERROR );
return 0;
}

RegCloseKey(hkey);

return 0;
}


After execution of this code i will get corresponding changes in registry but those changes are not reflected in the explorer. For that i have to restart the system which i won't.
Yeah, that's changing registry entries.

You'll probably find the applet is going straight to the device driver in an undocumented way. Whatever that is, it's not an API call.

ya the program is changing only registry entries, but i want those changes reflected on the explorer means drive letter are getting changed but not shown in the explorer for that i have to restart the system. So plz tell me the further solution as i am beginner.

Thanks for your response.
Hi,

As we can change drive letter from control panel --> computer management --> Disk management by right click on volume header. same thing I want to do using c++. I am succeed for making changes in the registry but those are not reflecting in the explorer for that I have restart my system which I won't. I think as we can successfully change the drive letter using computer management we can have those changes using programming also. As I am new to this area please provide any solution regarding this. What is happening while making changes through computer management or what are API's are called or If I am doing completely wrong then also you can suggest me for the correct way.

Your help is really appreciable.
Here you go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define _WIN32_WINNT 0x500 

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
char *old_mountpoint="H:\\",
     *new_mountpoint="G:\\";
     
char unc_path[512]; // UNC path is needed to mount a device

GetVolumeNameForVolumeMountPoint(old_mountpoint,unc_path,512); // Get the UNC path of the volume
clog <<"UNC path of the disk: "<<unc_path<<endl;

DeleteVolumeMountPoint(old_mountpoint);

SetVolumeMountPoint(new_mountpoint,unc_path);

}


GetVolumeNameForVolumeMountPoint http://msdn.microsoft.com/en-us/library/aa364994(v=vs.85).aspx
DeleteVolumeMountPoint: http://msdn.microsoft.com/en-us/library/aa363927(v=vs.85).aspx
SetVolumeMountPoint: http://msdn.microsoft.com/en-us/library/aa365561(v=vs.85).aspx

EDIT: added #define _WIN32_WINNT 0x500
Last edited on

The function in the above code return error 123 I think the path specified in old_mountpoint parameter is wrong i have also tried path as \\.\\H:\\ but it is not working.
Thanks to everyone for your valuable time.
Topic archived. No new replies allowed.