We help IT Professionals succeed at work.

Convert Text to REG-BINARY to write to Registry

What is the easiest way to convert simple Text to REG-BINARY so I can write this value to a users registry HKCU  on Windows 7

I saw a nice script recently but can't find it again
Comment
Watch Question

iGottZFrontend Engineer

Commented:
how does your "simple text" looks like?
iGottZFrontend Engineer

Commented:
and to what registry path (as example) would you write into?
ARamptonIT Manager

Author

Commented:
I want to set HKCU\Software\Microsoft\Office\11.0\Common\UserInfo\UserName to "Harry Lion"

and HKCU\Software\Microsoft\Office\11.0\Common\UserInfo\UserInitials to "348"

Commented:


Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Office\10.0\Common\UserInfo]
"UserInitials"=hex:33,00,34,00,38,00
"UserName"=hex:48,2e,61,2e,72,2e,72,2e,79,2e,20,2e,4c,2e,69,2e,6f,2e,6e

Open in new window


 binary.reg

Double click binary.reg and click OK.
ARamptonIT Manager

Author

Commented:
Nice but I want a script to conver my Text to REG-BINARY
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, give this a try.

Regards,

Rob.
strText = "Harry Lion"
arrNewBinaryData = ConvertStringToMicrosoftBinary(strText)
WriteBinaryOctValue "HKCU\Software\Microsoft\Office\11.0\Common\UserInfo\UserName", arrNewBinaryData

strText = "348"
arrNewBinaryData = ConvertStringToMicrosoftBinary(strText)
WriteBinaryOctValue "HKCU\Software\Microsoft\Office\11.0\Common\UserInfo\UserInitials", arrNewBinaryData

WScript.Echo "Finished."

Function ConvertStringToMicrosoftBinary(strStringToConvert)
	For intChr = 1 To Len(strStringToConvert)
		strBinString = strBinString & Asc(Mid(strStringToConvert, intChr, 1)) & ",0,"
	Next
	strBinString = strBinString & "0,0"
	arrMicrosoftBinary = Split(strBinString, ",")
	ConvertStringToMicrosoftBinary = arrMicrosoftBinary
End Function

Sub WriteBinaryOctValue(strValueNamePath, uBinary)
	Const HKEY_CURRENT_USER = &H80000001
	Const HKEY_LOCAL_MACHINE = &H80000002
	Set objRegistry = GetObject("Winmgmts:root\default:StdRegProv")
	If Left(strValueNamePath, InStr(strValueNamePath, "\") - 1) = "HKCU" Or _
	Left(strValueNamePath, InStr(strValueNamePath, "\") - 1) = "HKEY_CURRENT_USER" Then
		strRoot = HKEY_CURRENT_USER
		strPath = Mid(strValueNamePath, InStr(strValueNamePath, "\") + 1)
	ElseIf Left(strValueNamePath, InStr(strValueNamePath, "\") - 1) = "HKLM" Or _
	Left(strValueNamePath, InStr(strValueNamePath, "\") - 1) = "HKEY_LOCAL_MACHINE" Then
		strRoot = HKEY_LOCAL_MACHINE
		strPath = Mid(strValueNamePath, InStr(strValueNamePath, "\") + 1)
	End If
	strValueToWrite = Mid(strPath, InStrRev(strPath, "\"))
	strPath = Left(strPath, InStrRev(strPath, "\"))
	If Len(strValueToWrite) > 1 Then
		strValueToWrite = Mid(strValueToWrite, 2)
	Else
		strValueToWrite = ""
	End If
	intReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, strPath, strValueToWrite, uBinary)
End Sub

Open in new window

ARamptonIT Manager

Author

Commented:
Fantastic, this does just what I want

I tried using a simplified Registry Writing used for setting other Variables but why won't this work for the binary data your routine creates?

Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite "HKCU\Software\Microsoft\Office\11.0\Common\UserInfo\UserName", arrNewBinaryData, "REG_BINARY"

Many Thanks

Andy
Most Valuable Expert 2012
Top Expert 2014
Commented:
Hi, from MSDN:
"RegWrite will write at most one DWORD to a REG_BINARY value. Larger values are not supported with this method"

You need to use SetBinaryValue.  You could shorten the whole code to this if you wanted to:
Const HKEY_CURRENT_USER = &H80000001
Set objRegistry = GetObject("Winmgmts:root\default:StdRegProv")

strText = "Harry Lion"
arrNewBinaryData = ConvertStringToMicrosoftBinary(strText)
intReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, "Software\Microsoft\Office\11.0\Common\UserInfo\", "UserName", arrNewBinaryData)

strText = "348"
arrNewBinaryData = ConvertStringToMicrosoftBinary(strText)
intReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, "Software\Microsoft\Office\11.0\Common\UserInfo\", "UserInitials", arrNewBinaryData)

WScript.Echo "Finished."

Function ConvertStringToMicrosoftBinary(strStringToConvert)
	For intChr = 1 To Len(strStringToConvert)
		strBinString = strBinString & Asc(Mid(strStringToConvert, intChr, 1)) & ",0,"
	Next
	strBinString = strBinString & "0,0"
	arrMicrosoftBinary = Split(strBinString, ",")
	ConvertStringToMicrosoftBinary = arrMicrosoftBinary
End Function

Open in new window


Regards,

Rob.
ARamptonIT Manager

Author

Commented:
Nice to have someone read the queation corectyly and provide a perfect solution
Most Valuable Expert 2012
Top Expert 2014

Commented:
No problem. Thanks for the grade.

Regards,

Rob.