Link to home
Start Free TrialLog in
Avatar of michael_madsen
michael_madsenFlag for Denmark

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.Shell");
var bKey = WshShell.RegRead ("HKCU\\Control Panel\\Desktop\\UserPreferencesMask");
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.
Avatar of avner
avner

The returned Object is not seems to be understood in JavaScript.
If you try :
var WshShell = new ActiveXObject("WScript.Shell");
var bKey = WshShell.RegRead ("HKCU\\Control Panel\\Desktop\\UserPreferencesMask");
alert(typeof bKey)


You'll get "unknown" , I guess the answer is to use VBScript instead, although I haven't tried it.

Avatar of michael_madsen

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.. :)
Avatar of Zvonko
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("WScript.Shell")
bKey = WshShell.RegRead("HKCU\Control Panel\Desktop\UserPreferencesMask")
'WScript.Echo WshShell.RegRead("HKCU\Control Panel\Desktop\UserPreferencesMask")
Dim bVal
Dim i
For i = 0 To Ubound(bKey)
  bVal = bVal + Hex(bKey(i))
Next
WScript.Echo bVal


Good luck,
Zvonko

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.Shell")


Dim bKey, I, myInt, multiplier

bKey = WshShell.RegRead ("HKCU\Control Panel\Desktop\UserPreferencesMask")

'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\UserPreferencesMask", myInt, "REG_BINARY"

I get an error in the last line!
Why do you transfrom bKey to myInt?

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

Sorry, again missreading. I give up...
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Yep, it was an overflow problem because type integer is signed. Therefore the negative integer correction above.

Thanks for the points.

See you,
Zvonko