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:
-
Sub RegBinWrite (key, value, data)
-
-
Dim st_TEMPFILE
-
st_TEMPFILE = “wBin.reg”
-
-
Dim oFS
-
Dim txtStream
-
Dim WshShell
-
Dim valString
-
-
key = ”[“ & key & ”]”
-
-
If value <> ”@” then
-
value = chr(34) & value & chr(34)
-
End if
-
-
valString = value & ”=” & data
-
-
Set oFS = CreateObject(“Scripting.FileSystemObject”)
-
Set txtStream = oFS.CreateTextFile(st_TEMPFILE,True)
-
txtStream.WriteLine(“REGEDIT4”)
-
txtStream.WriteLine(key)
-
txtStream.WriteLine(valString)
-
txtStream.Close
-
-
Set WshShell = CreateObject(“Wscript.Shell”)
-
WshShell.Run “regedit /s “ & st_TEMPFILE, 1, True
-
Set WshShell = Nothing
-
-
oFS.DeleteFile st_TEMPFILE
-
Set oFS = Nothing
-
-
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:
-
Function stRegBinary (RegBinaryArray)
-
-
Dim item, st
-
-
If Not isarray(RegBinaryArray) Then
-
stRegBinary = ””
-
Else
-
For Each item In RegBinaryArray
-
st= st & CStr(Right(“00” & HEX(item),2)) & ” “
-
Next
-
stRegBinary = Trim(st)
-
End If
-
-
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)