RegistryWriteKey (write hex value)

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 1023
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

RegistryWriteKey (write hex value)

Post by Grovkillen » Thu Feb 06, 2020 7:15 am

I want to have my startup script automatically set the taskbar to hidden but the registrywritekey isn't working since the value must be hex. Am I missing something obvious here?

/*
hide:
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3]
"Settings"=hex:30,00,00,00,fe,ff,ff,ff,03,00,00,00,03,00,00,00,4e,00,00,00,26,\
00,00,00,00,00,00,00,12,04,00,00,80,07,00,00,38,04,00,00,78,00,00,00,01,00,\
00,00
*/

/*
unhide:
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3]
"Settings"=hex:30,00,00,00,fe,ff,ff,ff,02,00,00,00,03,00,00,00,4e,00,00,00,26,\
00,00,00,00,00,00,00,12,04,00,00,80,07,00,00,38,04,00,00,78,00,00,00,01,00,\
00,00
*/
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: RegistryWriteKey (write hex value)

Post by Marcus Tettmar » Thu Feb 06, 2020 8:41 am

Hi,

RegistryWriteKey doesn't support binary values. You can do it with a bit of VBScript like this:

Code: Select all

VBSTART
Set WshShell = CreateObject("WScript.Shell")
WshShell.RegWrite "HKEY_CURRENT_USER\MJTNET\TEST", &H00239856, "REG_BINARY"
VBEND
You would need to convert your binary dump to a hex &h value though.

I did find some VBScript functions someone had written which use the comma separated binary values here:
https://rcmtech.wordpress.com/2012/03/0 ... ry-values/

Using this we end up with:

Code: Select all

VBSTART
  Sub regWriteBinary(sRegKey, sRegValue, sBinaryData)
      ' Expects sBinaryData to be a comma separated string of hex values
      ' sRegKey needs to start with full registry root, e.g. HKEY_CURRENT_USER and not VBScript short form HKCU
      Dim oShell, oFSO, oFile, oExec
      Dim sTempFile
      Set oShell = CreateObject("WScript.Shell")
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      sTempFile = oShell.ExpandEnvironmentStrings("%temp%\RegWriteBinary.reg")
      Set oFile = oFSO.CreateTextFile(sTempFile,True)
      oFile.WriteLine("Windows Registry Editor Version 5.00")
      oFile.WriteLine("")
      oFile.WriteLine("[" & sRegKey & "]")
      oFile.WriteLine(Chr(34) & sRegValue & Chr(34) & "=" & "hex:" & sBinaryData)
      oFile.Close
      Set oExec = oShell.Exec("reg.exe import """ & sTempFile & """")
      If InStr(1,oExec.StdErr.ReadAll,"operation completed successfully",vbTextCompare) Then
             ' Yes the success text IS sent out via StdErr and NOT StdOut
             'WScript.Echo "Registry updated sucessfully"
             oFSO.DeleteFile sTempFile
      Else
             WScript.Echo "regWriteBinary: Registry import of " & sTempFile & " failed: " & oExec.StdErr.ReadAll
      End If
End Sub
VBEND

Let>value=30,00,00,00,fe,ff,ff,ff,03,00,00,00,03,00,00,00,4e,00,00,00,26,00,00,00,00,00,00,00,12,04,00,00,80,07,00,00,38,04,00,00,78,00,00,00,01,00,00,00
VBRun>regWriteBinary,HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3,Settings,value
Which does what you want.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1023
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: RegistryWriteKey (write hex value)

Post by Grovkillen » Thu Feb 06, 2020 12:40 pm

Marcus Tettmar wrote:
Thu Feb 06, 2020 8:41 am

Code: Select all

VBSTART
  Sub regWriteBinary(sRegKey, sRegValue, sBinaryData)
      ' Expects sBinaryData to be a comma separated string of hex values
      ' sRegKey needs to start with full registry root, e.g. HKEY_CURRENT_USER and not VBScript short form HKCU
      Dim oShell, oFSO, oFile, oExec
      Dim sTempFile
      Set oShell = CreateObject("WScript.Shell")
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      sTempFile = oShell.ExpandEnvironmentStrings("%temp%\RegWriteBinary.reg")
      Set oFile = oFSO.CreateTextFile(sTempFile,True)
      oFile.WriteLine("Windows Registry Editor Version 5.00")
      oFile.WriteLine("")
      oFile.WriteLine("[" & sRegKey & "]")
      oFile.WriteLine(Chr(34) & sRegValue & Chr(34) & "=" & "hex:" & sBinaryData)
      oFile.Close
      Set oExec = oShell.Exec("reg.exe import """ & sTempFile & """")
      If InStr(1,oExec.StdErr.ReadAll,"operation completed successfully",vbTextCompare) Then
             ' Yes the success text IS sent out via StdErr and NOT StdOut
             'WScript.Echo "Registry updated sucessfully"
             oFSO.DeleteFile sTempFile
      Else
             WScript.Echo "regWriteBinary: Registry import of " & sTempFile & " failed: " & oExec.StdErr.ReadAll
      End If
End Sub
VBEND

Let>value=30,00,00,00,fe,ff,ff,ff,03,00,00,00,03,00,00,00,4e,00,00,00,26,00,00,00,00,00,00,00,12,04,00,00,80,07,00,00,38,04,00,00,78,00,00,00,01,00,00,00
VBRun>regWriteBinary,HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3,Settings,value
Which does what you want.
I didn't see why I needed to use VBScript for this... but thanks to your input I got this:

Code: Select all

SRT>SET_REGISTRY_FOR_TASKBAR
  If>SET_REGISTRY_FOR_TASKBAR_Var_1=hide
    Let>SET_REGISTRY_FOR_TASKBAR_STATE=03
  Else>
    Let>SET_REGISTRY_FOR_TASKBAR_STATE=02
  Endif>
  Let>HEX_CODE=30,00,00,00,fe,ff,ff,ff,%SET_REGISTRY_FOR_TASKBAR_STATE%,00,00,00,03,00,00,00,4e,00,00,00,26,00,00,00,00,00,00,00,12,04,00,00,80,07,00,00,38,04,00,00,78,00,00,00,01,00,00,00
  Let>FILE_CONTENTS=Windows Registry Editor Version 5.00%CRLF%
  ConCat>FILE_CONTENTS,[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3]%CRLF%
  ConCat>FILE_CONTENTS,"Settings"=hex:
  ConCat>FILE_CONTENTS,HEX_CODE
  DeleteFile>%SCRIPT_DIR%\temp.reg
  WriteLn>%SCRIPT_DIR%\temp.reg,,FILE_CONTENTS
  RunProgram>cmd /k reg.exe import "%SCRIPT_DIR%\temp.reg"
  Wait>1
  DeleteFile>%SCRIPT_DIR%\temp.reg
END>SET_REGISTRY_FOR_TASKBAR
Thanks for the support! :)
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: RegistryWriteKey (write hex value)

Post by Marcus Tettmar » Thu Feb 06, 2020 2:27 pm

Grovkillen wrote:
Thu Feb 06, 2020 12:40 pm
I didn't see why I needed to use VBScript for this... but thanks to your input I got this:
Because as I said:
RegistryWriteKey doesn't support binary values
So a different method was needed.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1023
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: RegistryWriteKey (write hex value)

Post by Grovkillen » Thu Feb 06, 2020 2:36 pm

Marcus Tettmar wrote:
Thu Feb 06, 2020 2:27 pm
Grovkillen wrote:
Thu Feb 06, 2020 12:40 pm
I didn't see why I needed to use VBScript for this... but thanks to your input I got this:
Because as I said:
RegistryWriteKey doesn't support binary values
So a different method was needed.
Yes I realized that but as you see I did it all in MS.. ;)
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: RegistryWriteKey (write hex value)

Post by Marcus Tettmar » Thu Feb 06, 2020 2:49 pm

Fair enough
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts