Ir al contenido


Foto

Exportar claves del registro.


  • Por favor identifícate para responder
1 respuesta en este tema

#1 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 09 noviembre 2015 - 11:29

egostar hizo una pregunta sobre el truco dedicado a borrar claves del registro referente a como hacer una copia previa. Las APIs RegSaveKey y RegSaveKeyEx están pensadas para eso junto con RegRestoreKey.
 
Me pareció un ejercicio interesante lo que le insinué a egostar, realizar una función que las exporte en modo texto como lo hace regedit. Así que hoy me he puesto manos a la obra y este es el resultado:



delphi
  1. const
  2. HKeys: array [0..8-1] of PCHAR = (
  3. 'HKEY_CLASSES_ROOT',
  4. 'HKEY_CURRENT_USER',
  5. 'HKEY_LOCAL_MACHINE',
  6. 'HKEY_USERS',
  7. 'HKEY_PERFORMANCE_DATA',
  8. 'HKEY_CURRENT_CONFIG',
  9. 'HKEY_DYN_DATA',
  10. nil
  11. );
  12.  
  13. //---------------------------------------------------------------------------
  14. // Convierte un buffer binario en una cadena de números hexadecimales
  15. function BinToStr(Bin: PBYTE; BinSize: DWORD; Unicode: BOOL = false): String;
  16. var
  17. i: DWORD;
  18. Hi: CHAR;
  19. Lo: CHAR;
  20. begin
  21. for i:=0 to BinSize-1 do
  22. begin
  23. if i>0 then Result:= Result + ',';
  24. Hi:= CHAR(($0F and (Bin^ shr 4)) + 48);
  25. Lo:= CHAR(($0F and Bin^) + 48);
  26. if Hi > '9' then inc(Hi, 39);
  27. if Lo > '9' then inc(Lo, 39);
  28. inc(Bin);
  29. Result:= Result + Hi + Lo;
  30. if Unicode then Result:= Result + ',00';
  31. end;
  32. end;
  33.  
  34.  
  35. // Convierte un valor del registro en texto
  36. function TranslateRegValue(Name: String; dwType: DWORD; Data: PBYTE; dwData: DWORD): String;
  37. var
  38. i: integer;
  39. S: String;
  40. begin
  41. if Name <> '' then
  42. Result:= '"' + Name + '"='
  43. else
  44. Result:= Result + '@=';
  45.  
  46. case dwType of
  47. 1: Result:= Result + '"' + PCHAR(Data)+'"'; // REG_SZ
  48. 2: Result:= Result + 'hex(2):'+ BinToStr(Data, dwData, true); // REG_EXPAND_SZ
  49. 3: Result:= Result + 'hex:'+ BinToStr(Data, dwData); // REG_BINARY
  50. 4: Result:= Result + 'dword:'+ IntToHex(PDWORD(Data)^, 8); // REG_DWORD
  51. 5: Result:= Result + 'dword:'+ IntToHex(PDWORD(Data)^, 8); // REG_DWORD_BIG_ENDIAN
  52. 6: Result:= Result + 'hex(6):'+ BinToStr(Data, dwData, true); // REG_LINK
  53. 7: Result:= Result + 'hex(7):'+ BinToStr(Data, dwData, true); // REG_MULTI_SZ
  54. 8: Result:= Result + 'hex(8):'+ BinToStr(Data, dwData); // REG_RESOURCE_LIST
  55. 9: Result:= Result + 'hex(9):'+ BinToStr(Data, dwData); // REG_FULL_RESOURCE_DESCRIPTOR
  56. 10: Result:= Result + 'hex(a):'+ BinToStr(Data, dwData); // REG_RESOURCE_REQUIREMENTS_LIST
  57. 11: Result:= Result + 'hex(b):'+ BinToStr(Data, dwData); // REG_QWORD
  58. end;
  59. i:= 78;
  60. while i < Length(Result) do
  61. begin
  62. if (Result[i] = ',') and (i < Length(Result)) then
  63. begin
  64. S:= Copy(Result, 0, i) + '\'+#13+#10 + ' ';
  65. Result:= Copy(S, 0, Length(S)) + Copy(Result, i+1, Length(Result)-i);
  66. inc(i, 78)
  67. end
  68. else inc(i);
  69. end;
  70. end;
  71.  
  72. // Exporta recursivamene todos los valores y subclaves de una clave dada a una cadena de texto
  73. function ExportRegistryKeyToStr(const RootKey: HKEY; const Key: String): String;
  74. var
  75. Handle : HKEY;
  76. Name: array[0..255] of CHAR;
  77. dwName: DWORD;
  78. Data: PBYTE;
  79. dwData: DWORD;
  80. dwType: DWORD;
  81. PathKey: String;
  82. Index: integer;
  83. begin
  84. Result:= '';
  85. if RegOpenKeyEx(RootKey, PChar(Key), 0, KEY_ENUMERATE_SUB_KEYS or KEY_READ, Handle) = ERROR_SUCCESS then
  86. begin
  87. //Guardo nombre de clave
  88. Result:= Result + #13+#10+'['+HKeys[RootKey - $80000000]+'\'+Key+']';
  89. //Guardo valores
  90. Index:= 0;
  91. dwName:= sizeof(Name);
  92. Data:= nil;
  93. while ERROR_SUCCESS = RegEnumValue(Handle, Index, @Name[0], dwName, nil, @dwType, Data, @dwData) do
  94. begin
  95. // Si encontrado o un listado por nombre de valor
  96. Data:= VirtualAlloc(nil, dwData, MEM_COMMIT, PAGE_READWRITE);
  97. dwName:= sizeof(Name);
  98. RegEnumValue(Handle, Index, @Name[0], dwName, nil, @dwType, Data, @dwData);
  99. Result:= Result + #13+#10 + TranslateRegValue(Name, dwType, Data, dwData);
  100. VirtualFree(Data, 0, MEM_RELEASE);
  101. inc(Index);
  102. dwName:= sizeof(Name);
  103. Data:= nil;
  104. end;
  105.  
  106. // Busco subclaves
  107. Index:= 0;
  108. while ERROR_SUCCESS = RegEnumKey(Handle, Index, @Name[0], sizeof(Name)-1) do
  109. begin
  110. PathKey:= Key + '\' + String(Name);
  111. Result:= Result + #13+#10 + ExportRegistryKeyToStr(RootKey, PathKey);
  112. inc(Index);
  113. end;
  114. RegCloseKey(Handle);
  115. end;
  116. end;
  117.  
  118. // Exporta una clave, sus valores y sus subclaves a un archivo
  119. function ExportRegistryKey(const RootKey: HKEY; const Key: String; FileName: String): Boolean;
  120. var
  121. S: TStringList;
  122. begin
  123. S:= TStringList.Create;
  124. S.Add('Windows Registry Editor Version 5.00');
  125. S.Add(ExportRegistryKeyToStr(RootKey, Key));
  126. S.SaveToFile(FileName);
  127. S.Free;
  128. end;

Uso:


php
  1. ExportRegistryKey(HKEY_CURRENT_USER, Edit1.Text, 'C:\prueba.reg');

 

Podemos exportar a una cadena o a un archivo. Este último es importable desde el registro o desde el Shell como cualquier archivo.reg
 
Espero que os resulte interesante.
 
 
 
Saludos.


  • 2

#2 egostar

egostar

    missing my father, I love my mother.

  • Administrador
  • 14.446 mensajes
  • LocationMéxico

Escrito 09 noviembre 2015 - 01:13

Caramba,

 

Vaya que bien, me gusta como realizar la transformación a texto con la función BinToStr(), con eso ya da confianza plena el borrado de llaves del registro. (y)

 

Saludos 


  • 0




IP.Board spam blocked by CleanTalk.