michael_madsen
asked on
Read binary value from registry
I am trying to read a binary value from the registry, like this:
var WshShell = new ActiveXObject("WScript.She ll");
var bKey = WshShell.RegRead ("HKCU\\Control Panel\\Desktop\\UserPrefer encesMask" );
WScript.Echo (bKey);
but i get an error in line 3. I don't know what kind of object is returned and how to get it to an integer value.
var WshShell = new ActiveXObject("WScript.She
var bKey = WshShell.RegRead ("HKCU\\Control Panel\\Desktop\\UserPrefer
WScript.Echo (bKey);
but i get an error in line 3. I don't know what kind of object is returned and how to get it to an integer value.
ASKER
i'd rather not
I hardly think it's a matter of prefrences at this point, but I'll be happy for you to get surprised.. :)
The problem is that REG_BINARY returns A VBArray of Integers
So you need VBS to read it.
Here an example (store it with extension *.vbs ):
Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScr ipt.Shell" )
bKey = WshShell.RegRead("HKCU\Con trol Panel\Desktop\UserPreferen cesMask")
'WScript.Echo WshShell.RegRead("HKCU\Con trol Panel\Desktop\UserPreferen cesMask")
Dim bVal
Dim i
For i = 0 To Ubound(bKey)
bVal = bVal + Hex(bKey(i))
Next
WScript.Echo bVal
Good luck,
Zvonko
So you need VBS to read it.
Here an example (store it with extension *.vbs ):
Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScr
bKey = WshShell.RegRead("HKCU\Con
'WScript.Echo WshShell.RegRead("HKCU\Con
Dim bVal
Dim i
For i = 0 To Ubound(bKey)
bVal = bVal + Hex(bKey(i))
Next
WScript.Echo bVal
Good luck,
Zvonko
ASKER
Ok , I've rewritten the script to VB. I can read the value but now I have another problem: I can't write the value back again.
The script now looks like this:
Option Explicit
Dim WshShell
Set WshShell = CreateObject("WScript.Shel l")
Dim bKey, I, myInt, multiplier
bKey = WshShell.RegRead ("HKCU\Control Panel\Desktop\UserPreferen cesMask")
'We will now convert to integer format (32 bit long)
myInt = 0
myInt = CInt(myInt)
multiplier = 1
If IsArray(bKey) Then
bKey(0) = bKey(0) AND 223 ' DF 'Clear 20
'bKey(0) = bKey(0) OR 32 ' 20 ' Set 20
For I = UBound(bKey) To LBound(bKey) Step -1
myInt = myInt + bKey(I)*multiplier
multiplier = multiplier * 256
Next
End If
WshShell.RegWrite "HKCU\Control Panel\Desktop\UserPreferen cesMask", myInt, "REG_BINARY"
I get an error in the last line!
The script now looks like this:
Option Explicit
Dim WshShell
Set WshShell = CreateObject("WScript.Shel
Dim bKey, I, myInt, multiplier
bKey = WshShell.RegRead ("HKCU\Control Panel\Desktop\UserPreferen
'We will now convert to integer format (32 bit long)
myInt = 0
myInt = CInt(myInt)
multiplier = 1
If IsArray(bKey) Then
bKey(0) = bKey(0) AND 223 ' DF 'Clear 20
'bKey(0) = bKey(0) OR 32 ' 20 ' Set 20
For I = UBound(bKey) To LBound(bKey) Step -1
myInt = myInt + bKey(I)*multiplier
multiplier = multiplier * 256
Next
End If
WshShell.RegWrite "HKCU\Control Panel\Desktop\UserPreferen
I get an error in the last line!
Why do you transfrom bKey to myInt?
Put bKey back as write parameter.
Put bKey back as write parameter.
Sorry, I confused with the Array :(
Your problem in shiffting the chars is, that you shifft EVERY char.
Better is this:
If IsArray(bKey) Then
bKey(0) = bKey(0) AND 223 ' DF 'Clear 20
'bKey(0) = bKey(0) OR 32 ' 20 ' Set 20
For I = UBound(bKey) To LBound(bKey) Step -1
myInt = myInt *multiplier
myInt = myInt + bKey(I)
multiplier = multiplier * 256
Next
End If
Better is this:
If IsArray(bKey) Then
bKey(0) = bKey(0) AND 223 ' DF 'Clear 20
'bKey(0) = bKey(0) OR 32 ' 20 ' Set 20
For I = UBound(bKey) To LBound(bKey) Step -1
myInt = myInt *multiplier
myInt = myInt + bKey(I)
multiplier = multiplier * 256
Next
End If
Sorry, again missreading. I give up...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Zvonko:
Thanx for hanging in there. Your last solution seems to work. I guess we ran into an overflow issue in the end.
And the reason I posted this here, is that I initially tried to do this in JavaScript. At the time I didn't know that I had to change over to VB!
/Michael
Thanx for hanging in there. Your last solution seems to work. I guess we ran into an overflow issue in the end.
And the reason I posted this here, is that I initially tried to do this in JavaScript. At the time I didn't know that I had to change over to VB!
/Michael
Yep, it was an overflow problem because type integer is signed. Therefore the negative integer correction above.
Thanks for the points.
See you,
Zvonko
Thanks for the points.
See you,
Zvonko
If you try :
var WshShell = new ActiveXObject("WScript.She
var bKey = WshShell.RegRead ("HKCU\\Control Panel\\Desktop\\UserPrefer
alert(typeof bKey)
You'll get "unknown" , I guess the answer is to use VBScript instead, although I haven't tried it.