Ir al contenido


Foto

Código para cambiar el ID del volumen de un Disco


  • Por favor identifícate para responder
2 respuestas en este tema

#1 azdin

azdin

    Member

  • Miembros
  • PipPip
  • 20 mensajes

Escrito 12 febrero 2016 - 03:18

Hola pedí ayuda para realizar un programa que cambie el ID del volumen de un Disco a un amigo, y él me brindo este código,

 

UDiskVolumeIDChanger.pas :::


delphi
  1. unit UDiskVolumeIDChanger;
  2.  
  3. interface
  4.  
  5. uses Windows, StrUtils, SysUtils, Dialogs;
  6.  
  7. type partial_boot_sector_info = record
  8. Fs: PAnsiChar;
  9. FsOffs: Integer;
  10. SerialOffs: Integer;
  11. end;
  12.  
  13. type
  14. TDiskVolumeIDChanger = class
  15. private
  16. m_hDisk: THandle;
  17. Unidad: AnsiString;
  18. public
  19. constructor Create;
  20. function AbrirDisco(Volumen: AnsiString): Boolean;
  21. procedure CerrarDisco();
  22. function LeerSector(Sector: DWORD; Buffer: AnsiString): Boolean;
  23. function EscribirSector(Sector: DWORD; Buffer: AnsiString): Boolean;
  24. function LeerDiscoID(): AnsiString;
  25. function SerialAStr(Serial: Integer): AnsiString;
  26. function StrASerial(Serial: AnsiString): Integer;
  27. function CambiarDiscoID(NuevoID: AnsiString): Boolean;
  28. end;
  29.  
  30. implementation
  31.  
  32. constructor TDiskVolumeIDChanger.Create;
  33. begin
  34. m_hDisk := INVALID_HANDLE_VALUE;
  35. end;
  36.  
  37. function TDiskVolumeIDChanger.AbrirDisco(Volumen: AnsiString): Boolean;
  38. var
  39. Disco: AnsiString;
  40. begin
  41. Disco := '\\.\' + Volumen + ':';
  42. m_hDisk := CreateFileA(
  43. PAnsiChar(Disco),
  44. GENERIC_READ or GENERIC_WRITE,
  45. FILE_SHARE_READ or FILE_SHARE_WRITE,
  46. nil,
  47. OPEN_EXISTING,
  48. 0,
  49. 0);
  50. Result := m_hDisk <> INVALID_HANDLE_VALUE;
  51. if Result then Unidad := Volumen + ':\';
  52. end;
  53.  
  54. procedure TDiskVolumeIDChanger.CerrarDisco();
  55. begin
  56. if m_hDisk <> INVALID_HANDLE_VALUE then begin
  57. CloseHandle(m_hDisk);
  58. m_hDisk := INVALID_HANDLE_VALUE;
  59. end;
  60. end;
  61.  
  62. function TDiskVolumeIDChanger.LeerSector(Sector: DWORD; Buffer: AnsiString): Boolean;
  63. var
  64. read: DWORD;
  65. begin
  66. if SetFilePointer(m_hDisk, sector, nil, FILE_BEGIN) = UINT(-1) then
  67. begin
  68. Result := False;
  69. Exit;
  70. end;
  71.  
  72. if ReadFile(m_hDisk, Buffer, 512, read, nil) = False then
  73. Result := False
  74. else
  75. Result := True;
  76. end;
  77.  
  78. function TDiskVolumeIDChanger.EscribirSector(Sector: DWORD; Buffer: AnsiString): Boolean;
  79. var
  80. write: DWORD;
  81. begin
  82. if SetFilePointer(m_hDisk, sector, nil, FILE_BEGIN) = UINT(-1) then
  83. begin
  84. Result := False;
  85. Exit;
  86. end;
  87.  
  88. if WriteFile(m_hDisk, Buffer, 512, write, nil) = False then
  89. Result := False
  90. else
  91. Result := True;
  92. end;
  93.  
  94. function TDiskVolumeIDChanger.LeerDiscoID(): AnsiString;
  95. var
  96. Serial: Integer;
  97. Max1, Max2: Cardinal;
  98. begin
  99. if not GetVolumeInformationA(PAnsiChar(Unidad), nil, 0, @Serial, Max1, Max2, nil, 0) then
  100. begin
  101. Result := '';
  102. Exit;
  103. end;
  104.  
  105. Result := SerialAStr(Serial);
  106. end;
  107.  
  108. function TDiskVolumeIDChanger.SerialAStr(Serial: Integer): AnsiString;
  109. begin
  110. Result := AnsiString(IntToHex(Serial shr 16, 4)) + '-' + AnsiString(IntToHex(Serial and $FFFF, 4));
  111. end;
  112.  
  113. function TDiskVolumeIDChanger.StrASerial(Serial: AnsiString): Integer;
  114. var
  115. S1, S2: AnsiString;
  116. begin
  117. SetLength(S1, 12);
  118.  
  119. try
  120. S1 := '$';
  121. S2 := Serial;
  122. S2[5] := S2[6];
  123. S2[6] := S2[7];
  124. S2[7] := S2[8];
  125. S2[8] := S2[9];
  126. S2[9] := #0;
  127. S1 := S1 + S2;
  128. Result := StrToInt(String(S1));
  129. except
  130. Result := 0;
  131. end;
  132. end;
  133.  
  134. function TDiskVolumeIDChanger.CambiarDiscoID(NuevoID: AnsiString): Boolean;
  135. var
  136. read: Cardinal;
  137. PActual, PSerial, POffs: ^Cardinal;
  138. PDiskHandle: ^THandle;
  139. Serial, i: Integer;
  140. Sector: AnsiString;
  141. VolType: PAnsiChar;
  142. pbsi: array [0..2] of partial_boot_sector_info;
  143. begin
  144. Result := False;
  145. Serial := StrASerial(NuevoID);
  146. SetLength(Sector, 512);
  147. PActual := @read;
  148. PSerial := @Serial;
  149. PDiskHandle := @m_hDisk;
  150.  
  151. if (Serial = 0) or (m_hDisk = INVALID_HANDLE_VALUE) then Exit;
  152.  
  153. pbsi[0].Fs := 'FAT32';
  154. pbsi[0].FsOffs := $52;
  155. pbsi[0].SerialOffs := $43;
  156. pbsi[1].Fs := 'FAT';
  157. pbsi[1].FsOffs := $36;
  158. pbsi[1].SerialOffs := $27;
  159. pbsi[2].Fs := 'NTFS';
  160. pbsi[2].FsOffs := $03;
  161. pbsi[2].SerialOffs := $48;
  162.  
  163. if SetFilePointer(m_hDisk, 0, nil, FILE_BEGIN) = INVALID_HANDLE_VALUE then Exit;
  164.  
  165. asm
  166. push 0
  167. push PActual
  168. push 512
  169. push Sector
  170. mov eax, PDiskHandle
  171. push [eax]
  172. call ReadFile
  173. end;
  174.  
  175. if SetFilePointer(m_hDisk, 0, nil, FILE_BEGIN) = INVALID_HANDLE_VALUE then Exit;
  176. //if WriteFile(m_hDisk, Sector, 512, write, nil) = False then Exit;
  177.  
  178. for i := 0 to 2 do
  179. begin
  180. POffs := @pbsi[i].FsOffs;
  181. asm
  182. mov eax, Sector
  183. mov ecx, POffs
  184. add eax, [ecx]
  185. push eax
  186. pop VolType
  187. end;
  188.  
  189. if AnsiStrLComp(VolType, pbsi[i].Fs, 3) = 0 then Break;
  190. end;
  191.  
  192. if i = 3 then Exit;
  193.  
  194. POffs := @pbsi[i].SerialOffs;
  195. asm
  196. mov eax, Sector
  197. mov ecx, PActual
  198. mov ebx, PSerial
  199. add eax, [ecx]
  200. mov ecx, [ebx]
  201. mov [eax], ecx
  202.  
  203. push 0
  204. push PActual
  205. push 512
  206. push Sector
  207. mov eax, PDiskHandle
  208. push [eax]
  209. call WriteFile
  210. end;
  211.  
  212. Result := True;
  213. end;
  214.  
  215. end.


delphi
  1. unit UPruebaDelphi;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7. Dialogs, UDiskVolumeIDChanger, StdCtrls, FileCtrl, ExtCtrls, Mask;
  8.  
  9. type
  10. TForm1 = class(TForm)
  11. Button1: TButton;
  12. Label1: TLabel;
  13. Label2: TLabel;
  14. MaskEdit1: TMaskEdit;
  15. Label3: TLabel;
  16. procedure Button1Click(Sender: TObject);
  17. procedure FormCreate(Sender: TObject);
  18. private
  19. UDVID: TDiskVolumeIDChanger;
  20. public
  21. { Public declarations }
  22. end;
  23.  
  24. var
  25. Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.dfm}
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. begin
  33. if MessageBox(0, PWideChar('Estás a punto de cambiar el ID del volumen de la unidad C' + #13#13#13 + 'Esto puede ser peligroso. ¿Desea continuar?'), PWideChar('AVISO'), MB_YESNO or
  34. MB_ICONQUESTION) = IDYES then
  35. begin
  36. UDVID.AbrirDisco(AnsiString('C'));
  37. if not UDVID.CambiarDiscoID(AnsiString(MaskEdit1.Text)) then
  38. ShowMessage('ID DE 32 BITS NO VÁLIDO O UNIDAD PROTEGIDA CONTRA R / W')
  39. else
  40. ShowMessage('DE DE VOLUMEN CAMBIADO EXITOSAMENTE. REINICIA LA PC');
  41. UDVID.CerrarDisco();
  42. end;
  43. end;
  44. (*
  45. procedure TForm1.dcbChange(Sender: TObject);
  46. begin
  47. UDVID.AbrirDisco(AnsiString(dcb.Drive));
  48.  
  49.   if UDVID.LeerDiscoID() = '' then
  50.   begin
  51.   Label1.Caption := 'Volumen no válido';
  52.   Button1.Enabled := False;
  53.   MaskEdit1.Enabled := False;
  54.   end else
  55.   begin
  56. Label1.Caption := String(UDVID.LeerDiscoID());
  57.   Button1.Enabled := True;
  58.   MaskEdit1.Enabled := True;
  59.   end;
  60.  
  61. UDVID.CerrarDisco();
  62. end; *)
  63.  
  64. procedure TForm1.FormCreate(Sender: TObject);
  65. begin
  66. UDVID := TDiskVolumeIDChanger.Create;
  67. UDVID.AbrirDisco(AnsiString('C'));
  68.  
  69. if UDVID.LeerDiscoID() = '' then
  70. begin
  71. Label1.Caption := 'Volumen no válido';
  72. Button1.Enabled := False;
  73. MaskEdit1.Enabled := False;
  74. end else
  75. begin
  76. Label1.Caption := String(UDVID.LeerDiscoID());
  77. Button1.Enabled := True;
  78. MaskEdit1.Enabled := True;
  79. end;
  80.  
  81. UDVID.CerrarDisco();
  82. end;
  83.  
  84. end.

Archivo adjunto  tick.png   13,89KB   0 descargas
 
[ESTE CÓDIGO SOLO CAMBIA EL ID DEL DISCO C]
 
Siendo sinceros, yo me perdí un poco en el código, Aunque él me dijo que todo le andaba bien en su PC y le funcionaba con normalidad.
 
Pero al parecer en mi PC y Laptop no funciona, alguien sabe a que se debe? O me pueden decir sí si les funciona el código
 
Muchas Gracias

  • 0

#2 enecumene

enecumene

    Webmaster

  • Administrador
  • 7.419 mensajes
  • LocationRepública Dominicana

Escrito 13 febrero 2016 - 09:17

Pues debes preguntarle en qué sistema operativo trabaja, es posible que versiones recientes de Windows haya cambios en la API.


  • 0

#3 egostar

egostar

    missing my father, I love my mother.

  • Administrador
  • 14.448 mensajes
  • LocationMéxico

Escrito 13 febrero 2016 - 12:37

Hola

 

Yo pregunto

 

¿Es posible cambiar el ID de volumen de un disco en uso? hasta donde sé se puede en discos no maestros, pero no estoy seguro, nunca he tenido que hacer eso.

 

Saludos


  • 0




IP.Board spam blocked by CleanTalk.