Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert registry binary to string or text

Status
Not open for further replies.

cluM09

Technical User
May 15, 2004
127
US
I would like to know if there is a way to convert the registry binary data to the string or text using VBScript.

Any input or direction on how I can do it would be very much appreciated!

Thanks in advance.
 
Hello cluM09,

When you read reg_binary either via wshshell regread method or via wmi stdregprov, you get as a return an array of variant with entries being the successive bytes of data. You can then chr() each entry, if they are all printable character and join the array to form a string for display purpose. Or, if you know the particular specification of how info is stored, you pick the entries is that specific order and read the info.

regards - tsuji
 
Thanks tsuji for your help!

What I am trying to do is to read the registry binary value from the registry that stores the SMTP server and dial-up information used by Microsoft OutLook so that I can automate OutLook to use two types of connections - one is LAN and one is modem dial-up, each of which using a different STMP server.

I am able to just do a string query of the registry with MS Office 2000, but with MS Office XP or 2003, these data are stored in binary.

Therefore, I want to be able to query the registry binary value, convert it to string to determine the for the current setting so that I can have the options to change back and fort between LAN and dialup connection without having to make these changes within OutLook. I would appreciate it if you could show me what can be done to my code I already have or advise a new one as shown below:

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003

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

strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows Messaging SubSystem\Profiles\OutLook\9375CFF0413111d3B88A00104B2A6676\00000002"
strValueName = "SMTP Server"
oReg.GetBinaryValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

For i = lBound(strValue) to uBound(strValue)
StdOut.WriteLine strValue(i)
Next

The output from this code when run is a set of numbers as shown below:

115
0
109
0
116
0
112
0
46
0
120
0
115
0
112
0
101
0
100
0
105
0
117
0
115
0
46
0
110
0
101
0
116
0
0
0

Please advise.

Thanks.


 
cluM09,

As you know the out-param is an array, so if you want to practice strick discipline, strValue should just better be named arrValue---this is a side-note.

To read the info, do this.
[tt]
oReg.GetBinaryValue HKEY_CURRENT_USER,strKeyPath,strValueName,arrValue
strInfo=""
for i=0 to ubound(arrValue)
if arrValue(i)<>0 then strInfo=strInfo & chr(arrValue(i))
next
wscript.echo strInfo 'strInfo is the info you need
[/tt]
- tsuji
 
tsuji,

Thank you so much for your help! It works!

With your help, I can query the registry binary data and convert to string.

Now I have another problem where I have to convert string data, convert to binary, and write it back to the registry. As I mentioned above, I need to update the registry values to reflect the new SMTP server and dial-up information. Therefore, I need to convert a string data to binary, and then write this binary data back to the registry.

I also need to delete the registry binary value in order to complete the required tasks.

Do you have any idea how these can be done?

Thanks again.
 
cluM09,

I have not checked the specification for the key and value. But to set new value, the principle is the same in reverse. If it represents zero-terminated string, then it is something like this.
Code:
s="smtp.xspedius.net"

dim arrValue()
redim arrValue(2*len(s)))
for i=0 to len(s)-1
	arrValue(2*i)=ascb(mid(s,i+1,1))
	arrValue(2*i+1)=cbyte(0)
next

'zero terminated string
'I isolated this part. It should be checked against spec.
x=ubound(arrValue)
redim preserve arrValue(x+2)
arrValue(x+1)=cbyte(0) : arrValue(x+2)=cbyte(0)

'you have to make necessary modifications to other params
oReg.SetBinaryValue HKEY_CURRENT_USER,strKeyPath,strValueName,arrValue
- tsuji
 
correction:

The first redim should be read:
[tt]
redim arrValue(2*len(s)[red]-1)[/red]
[/tt]
- tsuji
 
Further notes:

As to the other part of the question, you have deletekey and deletevalue methods to handle the named operations.

- tsuji
 
tsuji,

Thank you so much for your help!!!

It works with the last portion of the code that you added to treat the "zero terminated string."

I am quite new to VBScript, and I still don’t understand how to use ascb, cbyte, dim, redim, and redim preserve of the array functions.

I don't quite understand when I should use the last portion to treat the terminated string. I know that without this last portion of the code, it leaves some trailing strange characters at the end of the string when I tried to use it.

Do you have any recommendation on what book or resources I can get to learn to understand on how to use ascb, cbyte, dim, redim, and redim preserve of the array functions as you did in the code?

Thanks again!

clum09


 
cluM09,

The basic reference is the ms wsh 5.6 documentation, free download:

It being largely a glue, you have to look for respective documentations for the technologies, like wmi etc. Msdn and kb are irreplacable sources of official info you've to refer back to constantly.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top