Inside Man
Inside Man

Reputation: 4372

Get Hex value of a Registry Key via VBScirpt

Think this is my target key in registry:

[HKEY_CURRENT_USER\System\Majid\0]
"GUID"=hex:60,de,2a,56,51,b2,e0,11,80,01,44,45,53,54,00,00

as you can see GUID has a hex value, I want to tell a vb-script to go to this key and stores its hex data into a variable.

For example if target variable is "Target" then its value should be "60,de,2a,56,51,b2,e0,11,80,01,44,45,53,54,00,00"

Any Help is Really Appreciated

Upvotes: 0

Views: 5549

Answers (4)

Mac
Mac

Reputation: 1

Here is one for DWORD

'##### READ A DWORD VALUE CONVERT TO HEX #####

strComputer = "."

Dim fso
Set fso = WScript.CreateObject("Scripting.Filesystemobject")

Dim oShell
Set oShell = WScript.CreateObject("Wscript.Shell")

strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")

regHive = "HKLM"
regKey = "Software\template\templateL2"
regValueName = "SAMPLE-Dword"
regPath = regHive & "\" & regKey & "\" & regValueName

regValue = oShell.RegRead(regPath) 'SPECIFY YOUR HIVE\KEY\VALUE HERE
res = "" 
If Len(regvalue) Mod 2 = 0 Then 
    res = "0x"
    leading0 = ""
Else
    res = "0x0"
    leading0 = "0"
End If

WScript.Echo "INPUT DATA IN GUI : " & Hex(regValue)
WScript.Echo "HEX REPRESENTATION: " & res & Hex(regvalue)
WScript.Echo "REG FILE VALUE    : " & "dword:" & leading0 & Hex(regvalue)

Upvotes: 0

Mac
Mac

Reputation: 1

One for REG_EXPAND_SZ

'############# READ REG_EXPAND_SZ ##########
strComputer = "."

Dim fso
Set fso = WScript.CreateObject("Scripting.Filesystemobject")

Dim oShell
Set oShell = WScript.CreateObject("Wscript.Shell")

strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")


regHive = "HKLM"
regKey = "Software\template\templateL2"
regValueName = "Expandable_String"
regPath = regHive & "\" & regKey & "\" & regValueName

regValue = oShell.RegRead(regPath) 
res = "" 
prefix = "hex(2):"

        For i=1 To Len(regValue)
            r = HexIt(Mid(regValue,i,1))
            res = res & r
        Next
        res = res & "00,00" 'NEW LINE / ENTRY



WScript.Echo "INPUT DATA IN GUI : " & regValue
WScript.Echo "HEX REPRESENTATION: " & res 
WScript.Echo "REG FILE VALUE    : " & prefix & res


'CONVERT EACH CHARACTER TO ASCII THEN TO HEX, ADD ",00," BETWEEN EACH VALUE
Function HexIt(data)
    a = Asc(data)
    h = Hex(a)
    HexIt = h & ",00,"
End Function

Upvotes: 0

Mac
Mac

Reputation: 1

Wrote these today. I still need to figure out where the ",\" is placed in the code, but this one will take a REG_MULTI_SZ value and convert it to the proper hex result as seen in a .reg file.

'##### READ A REG_MULTI_SZ VALUE CONVERT TO HEX #####

strComputer = "."

Dim fso
Set fso = WScript.CreateObject("Scripting.Filesystemobject")

Dim oShell
Set oShell = WScript.CreateObject("Wscript.Shell")

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")


regHive = "HKLM"
regKey = "Software\template\templateL2"
regValueName = "SAMPLE-Multi_String"
regPath = regHive & "\" & regKey & "\" & regValueName

regValue = oShell.RegRead(regPath)

res = "" 
prefix = "hex(7):"
For Each item In regValue
data = data & item & vbcrlf
        For i=1 To Len(item)
            r = HexIt(Mid(item,i,1))
            res = res & r
        Next
        res = res & "00,00," 'NEW LINE / ENTRY
    Next
        res = res & "00,00" 'FINAL COMPLETION 



'CONVERT EACH CHARACTER TO ASCII THEN TO HEX, ADD ",00," BETWEEN EACH VALUE
Function HexIt(data)
    a = Asc(data)
    h = Hex(a)
    HexIt = h & ",00,"
End Function


WScript.Echo "ACTUAL DATA IN GUI : " & vbCrLf & data & vbCrLf 
WScript.Echo "HEX REPRESENTATION : " & vbCrLf & res & vbCrLf 

WScript.Echo "REG FILE VALUE     : " & vbCrLf & prefix & res & vbCrLf 

Upvotes: 0

jqcAngel
jqcAngel

Reputation: 120

It's likely that the reason you don't yet have an answer is that the premise of your question is misleading if not flawed. The question seems to imply you're asking about a hex number but you're actually asking about binary data. When you export a binary value from the registry the resulting .reg file encodes the value in hex as per your example. You may or may not have realized this, but it's likely been a stumbling block to solving your issue.

So now to answer "How can I convert a binary value to a hex string representation?"

The following code did the job for me. I only use vbscript on occasion so forgive the sloppiness.

Dim objRegistry, target, output
Set objRegistry = CreateObject("Wscript.shell")
target = objRegistry.RegRead("HKCU\System\Majid\0\GUID")
output = ""
for k = LBound(target,1) To UBound(target,1)
    output = output & hex(target(k)) & ","
next
WScript.echo output

Does that work for you?

Upvotes: 2

Related Questions