Escribir Tipos Binarios en el Registro

Después del comentario de Jack en mi última entrada sobre la lectura/escritura del registro de Windows usando VBScript, me puse a investigar como se podían introducir valores binarios en una clave.

Si tuvieramos que introducir un valor binario con formato corto usaríamos simplemente la instrucción RegWrite:

WSHShell.RegWrite("\HKey\KeyTest\", 0x1, "REG BINARY")

Pero, tras consultar unas cuantas páginas especializadas, he descubierto que no existe una forma automática de realizar este proceso para valores con formato largo [p.e: 00 00 10 01]. La única solución posible que tenemos que usar se basa en generar una archivo temporal con la información a grabar e importarlo en el registro [sí, es muy cutre].

Os presento las funciones necesarias para escribir y leer valores binarios en las claves del registro de Windows:

Procedimiento para Escribir en el Registro un Valor Binario

Visual Basic:
  1. Sub RegBinWrite (key, value, data)
  2.  
  3.   Dim st_TEMPFILE
  4.   st_TEMPFILE = "wBin.reg"
  5.  
  6.   Dim oFS
  7.   Dim txtStream
  8.   Dim WshShell
  9.   Dim valString
  10.  
  11.   key = "[" & key & "]"
  12.  
  13.   If value <> "@" then
  14.     value = chr(34) & value & chr(34)
  15.   End if
  16.  
  17.   valString = value & "=" & data
  18.  
  19.   Set oFS = CreateObject("Scripting.FileSystemObject")
  20.   Set txtStream = oFS.CreateTextFile(st_TEMPFILE,True)
  21.   txtStream.WriteLine("REGEDIT4")
  22.   txtStream.WriteLine(key)
  23.   txtStream.WriteLine(valString)
  24.   txtStream.Close
  25.  
  26.   Set WshShell = CreateObject("Wscript.Shell")
  27.   WshShell.Run "regedit /s " & st_TEMPFILE, 1, True
  28.   Set WshShell = Nothing
  29.  
  30.   oFS.DeleteFile st_TEMPFILE
  31.   Set oFS = Nothing
  32.  
  33. End Sub

Para realizar el proceso haríamos una llamada tal que así:

RegBinWrite "HKEY_CLASSES_ROOT\.000HNK", "Prueba", "hex:00,00,01,00"

Y si quisieramos que la clave binaria sea la predeterminada, usaríamos el caracter @:

RegBinWrite "HKEY_CLASSES_ROOT\.000HNK", "@", "hex:00,00,01,00"

Función para Leer un Valor Binario del Registro

Visual Basic:
  1. Function stRegBinary (RegBinaryArray)
  2.  
  3.   Dim item, st
  4.  
  5.   If Not isarray(RegBinaryArray) Then
  6.     stRegBinary = ""
  7.   Else
  8.     For Each item In RegBinaryArray
  9.       st= st & CStr(Right("00" & HEX(item),2)) & " "
  10.     Next
  11.     stRegBinary = Trim(st)
  12.   End If
  13.  
  14. End Function

Y para obtener el valor usaríamos la función del siguiente modo:
Set oReg = CreateObject("Wscript.Shell")
strClave = oReg.RegRead("HKEY_CLASSES_ROOT\.000HNK\Prueba")
MsgBox stRegBinary(strClave)

Otras entradas relacionadas

deja tu respuesta

NOTA: Para que tu comentario no sea eliminado intenta escribir correctamente, sin poner todo en mayúsculas, respetando las reglas de ortografía y sin insultar a nadie. ¡Gracias!