Recent

Author Topic: Change the drive letter in hdd or usb  (Read 1541 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Change the drive letter in hdd or usb
« on: December 13, 2019, 05:32:13 am »
Hello friends, a question.
I want to change the letter of a hard disk or usb memory ( example J: D: K: ).
I found this code, it compiles well but when running it shows error.


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes,
  9.   SysUtils,
  10.   Forms,
  11.   Controls,
  12.   Graphics,
  13.   Dialogs,
  14.   ActiveX,
  15.   ComObj,
  16.   StdCtrls;
  17.  
  18. type
  19.  
  20.   { TForm1 }
  21.  
  22.   TForm1 = class(TForm)
  23.     Button1: TButton;
  24.     procedure Button1Click(Sender: TObject);
  25.   private
  26.  
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure  ChangeDriveLetter(OldDrive,NewDrive:Char);
  41. var
  42.   FSWbemLocator : OLEVariant;
  43.   FWMIService   : OLEVariant;
  44.   FWbemObjectSet: OLEVariant;
  45.   FWbemObject   : OLEVariant;
  46.   oEnum         : IEnumvariant;
  47.   iValue        : LongWord;
  48. begin;
  49.   FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  50.   FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  51.   FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_Volume Where DriveLetter=%s',[QuotedStr(OldDrive+':')]),'WQL',0);
  52.   oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  53.   if oEnum.Next(1, FWbemObject, iValue) = 0 then
  54.   begin
  55.     //Assign the New letter
  56.     FWbemObject.DriveLetter:=NewDrive+':';
  57.     //Apply the changes
  58.     FWbemObject.Put_();
  59.   end;
  60. end;
  61.  
  62. procedure TForm1.Button1Click(Sender: TObject);
  63. begin
  64.    try
  65.     CoInitialize(nil);
  66.     try
  67.       //This will change the letter of the drive E to Z
  68.       ChangeDriveLetter('E','Z');
  69.     finally
  70.       CoUninitialize;
  71.     end;
  72.  except
  73.     on E:Exception do
  74.     begin
  75.         ShowMessage(E.Classname+ ':'+ E.Message);
  76.  
  77.     end;
  78.   end;
  79. end;
  80.  
  81. end.
  82. // code of    https://theroadtodelphi.com/2011/02/25/change-the-drive-letter-using-wmi-and-delphi/
  83.  


help me please  :(

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Change the drive letter in hdd or usb
« Reply #1 on: December 13, 2019, 09:34:46 am »
What is the exception?
You should also check which step fails. E.g.:

Code: Pascal  [Select][+][-]
  1. FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  2. if VarIsNull(FSWbemLocator) then
  3.   raise Exception.Create('CreateOleObject failed');
  4. // and so on
  5.  

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Change the drive letter in hdd or usb
« Reply #2 on: December 13, 2019, 09:48:14 am »
I want to change the letter of a hard disk or usb memory ( example J: D: K: ).
I found this code, it compiles well but when running it shows error.
I wouldn't do it the way it's done in the code you found.  I don't have an example in Pascal but, MS has an example written in C which is fairly easy to understand and port to Pascal.  You can find it at https://docs.microsoft.com/en-us/windows/win32/fileio/editing-drive-letter-assignments

I'd use code like the one in that example.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Change the drive letter in hdd or usb
« Reply #3 on: December 13, 2019, 09:50:37 am »
I'd use code like the one in that example.
If there's a way not to use WMI that is indeed usually the better way. ;) (except one needs to be able to do that for a remote machine, then there's usually no way around...)

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: Change the drive letter in hdd or usb
« Reply #4 on: December 14, 2019, 02:42:42 am »
Hi friend, thanks for your answers, I will do some tests and I comment...

 

TinyPortal © 2005-2018