votar

Escribir Tipos Binarios en el Registro

Publicada el 21/01/2008 a las 11:01 y leída 1440 veces. Traducir ao galego Traducir ao galego.

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
[vb]
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
[/vb]

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
[vb]
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
[/vb]

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)

Programación registro script VB

dolcebita

  • Xibor
    Gracias!!! capo!!!tanto tiempo.
  • Mijail
    busque esto em todo sitio lo encontre aqui muchas graciasss!!
blog comments powered by Disqus