What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » reading boolean from registry

reading boolean from registry
Author: Message:
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. reading boolean from registry
I need a little help, i'm trying to read a boolean value from the registry, but everytime it gets converted to true.

I tried lots of things like implicit conversion but even that didn't help, only thing I havn't tried is Matty's Registry Module

Here's a piece of my code
code:
//First some classes so we can easily create objects later

function preferences()
{
  this.percentage = new Boolean(true);
  this.cols = 3;
}

//Then we create the objects

var preferences = new preferences();

//The actual reaing of the variable in the registry

preferences.percentage = ReadRegistry("\preferences\\percentage");

//The actual saving of the variable in the registry

WriteRegistry("\preferences\\percentage", preferences.percentage);

//Functions to do the saving and reading

function WriteRegistry(key, value)
{
  var Shell = new ActiveXObject("WScript.Shell");
  return Shell.RegWrite(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key, value);
  delete Shell;
}

function ReadRegistry(key)
{
  var Shell = new ActiveXObject("WScript.Shell");
  return Shell.RegRead(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key);
  delete Shell;
}


I also tried this when reading:

code:
preferences.percentage = Boolean(ReadRegistry("\preferences\\percentage"));


and

code:
preferences.percentage = new Boolean(ReadRegistry("\preferences\\percentage"));


If anyone knows what I'm doing wrong, all help is appreciated.




This post was edited on 07-21-2006 at 08:59 PM by Ezra.
[Image: 1-0.png]
             
07-21-2006 08:48 PM
Profile PM Web Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: reading boolean from registry
If the Preferences object is called a "Preferences", don't name your variable that  (don't do what I crossed out, instead do what's underneath):
code:
function preferences()
{
  this.percentage = new Boolean(true);
  this.cols = 3;
}
var preferences = new preferences();
var mypreferences = new preferences();

(the bold is what I changed)

I believe it is because of the intialization where the boolean is set to true. Because they have the same name, it's initializing again each time, setting it to true.

This post was edited on 07-21-2006 at 09:22 PM by deAd.
07-21-2006 09:20 PM
Profile PM Find Quote Report
TheGeek
Full Member
***

Avatar
Excuse my geekyness.

Posts: 179
Reputation: 15
34 / Male / –
Joined: Feb 2005
RE: reading boolean from registry
Can't you just read/write it as a string?
code:
Shell.RegWrite(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key, bMyVal?"true":"false");
code:
bMyVal = (Shell.RegRead(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key) == "true");

also, your shell object won't be deleted, because the function returns before your delete statement.

This post was edited on 07-21-2006 at 09:24 PM by TheGeek.
[Image: 468x60banner.png]
07-21-2006 09:22 PM
Profile E-Mail PM Web Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: reading boolean from registry
First I tried your id deAd, but that didn't work, but I left it in anyway, cause it's better this way, although did it the other way around, renamed the class :-)

Also tried your idea TheGeek and it workes perfectly :-)

I added both your usenames to my Thanks section in my code :P

The script i'm working on is a newer version of Letter Count btw.
[Image: 1-0.png]
             
07-21-2006 10:28 PM
Profile PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: reading boolean from registry
quote:
Originally posted by deAd
I believe it is because of the intialization where the boolean is set to true. Because they have the same name, it's initializing again each time, setting it to true.
No... As it is, it works perfectly. JScript knows what the object is and what the variable name is.

It's not initialized each time. And the method used by Ezra can perfectly be used as a means to provide a default value.


quote:
Originally posted by TheGeek
Can't you just read/write it as a string?
code:
Shell.RegWrite(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key, bMyVal?"true":"false");

bMyVal = (Shell.RegRead(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key) == "true");

That is actually besides the point and not what the 'problem' is, nor does it explain anything in relation to what is happening. It is also unneeded code and makes it more complicated than it is.
quote:
Originally posted by TheGeek
also, your shell object won't be deleted, because the function returns before your delete statement.
In regards to that, that's only partially true. It is true that the delete statement isn't executed, but the object will still be deleted anyways. The important thing is that he even doesn't need the delete statement as the shell object is created as a local variable. Local variables are not valid /dont exist outside the function they're created in, they are automatically deleted when the function ends.


quote:
Originally posted by Ezra
I need a little help, i'm trying to read a boolean value from the registry, but everytime it gets converted to true.
In short:

Because the value in the registry is actually true... There is nothing wrong with your script. Change the registy value manually and read it out using your script, you'll see it will reflect the value perfectly.



Long explanation:

Note that there is no such thing as an explicit boolean value in the registry.

REG_BOOLEAN (or whatever) doesn't exist. If a program saved a boolean value to the registry it is useually done as a REG_DWORD holding a value of 1 (or -1) or 0.

----------------------

code:
preferences.percentage = ReadRegistry("\preferences\\percentage");
Note that if you use this, the property percentage will become a string, eventhough you declared it as being a boolean object. This is because JScript automatically converts properties to other types if it is needed. It is as if it doesn't care what you declared it as. And the string type is used because the ReadRegistry function returns a string.

code:
preferences.percentage = Boolean(ReadRegistry("\preferences\\percentage"));
If you use this, the property percentage will become a boolean, just as you have declared it with. The default value which you used to initialize it with will be overwritten.

code:
preferences.percentage = new Boolean(ReadRegistry("\preferences\\percentage"));
Same as above actually. Only you again declare percentage again. But makes actually no difference otherwise.

----------------------

Also, when working with boolean objects in JScript, JSCript will automatically convert the boolean object to the needed type.
(this is the essence of this whole post and the essence of your whole issue)

eg: If B is a boolean object. Then S will be a string and B will be converted automatically to a string ("false" or "true"):

S = "This is " + B

N will be a number and JScript will therefore automatically convert B to the equivalent number (0 or -1):

N = 500 + B

In an IF THEN ELSE statement a boolean object is treated as a real boolean. Thus this is perfectly valid, eventhough if you output B with Debug.Trace it will output a string:

If ( B ) {}



In other words, when you say it gets converted to "true", it is exactly what it should be doing and it works correctly.

If you output the to a boolean converted read value with the Debug.Trace statement to check its contents, then it indeed gets converted to the string "true" since Debug.Trace expects a string and outputs a string. JScript detects this and thus converts the boolean value to a string; "true" or "false" are the string equivalents of the boolean type, thus that is outputted.

In other words, there is nothing wrong with your code (except for the few unrelated pointers posted before).

code:
Shell.RegWrite(keypath, value);
Note that this is the equivalent of
code:
Shell.RegWrite(keypath, value, "REG_SZ");
And thus the value variable will be written as a string to the registry. Thus if the value variable is a boolean object, it will again be converted to a string and the string "true" or "false" will be written. If you want to write the boolean object as a number you need to specify the type to write to the registry as a REG_DWORD:
code:
Shell.RegWrite(keypath, value, "REG_DWORD");
Again, the value variable will be converted to the equivalent type because it is a boolean object. Thus this time being a number and thus the number 0 (0x0) or -1 (0xFFFFFF) will be written to the registry.



See MSDN Library:
* RegRead method
* Boolean Object


-----------------

PS: Since you use
code:
MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key
you do not need to specify yet another slash in your key value:
code:
ReadRegistry("\preferences\\percentage");

Doing that will make your final string actually be:
"HKEY_CURRENT_USER\\Software\\Patchou\\Messenger Plus! Live\\GlobalSettings\\Scripts\\yourscript\\my@email\\\preferences\\percentage"

This isn't technically wrong as luckally the extra slash is ignored, but it is still wrong though.

-----------------

;)

This post was edited on 07-21-2006 at 11:40 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
07-21-2006 10:31 PM
Profile PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: reading boolean from registry
Thanks very much Cookie, I used REG_DWORD now and it works without any dodgy conversions.

Right now in the script im also using REG_SZ to save an array, and it simply converts it to a csv string, and that's fine I just use split(",") when reading, but I thought maybe it would be better if I used REG_BINARY, but I don't know how, i've been looking around, but can't find any anwers.

Maybe you know this too. TIA
[Image: 1-0.png]
             
07-22-2006 03:56 PM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: reading boolean from registry
See J-Thread's reply to [HELP] Reading Reg Keys
07-22-2006 06:04 PM
Profile E-Mail PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: reading boolean from registry
For reading a REG_BINARY I knew it would return a VBArray, but I don't know how to write a REG_BINARY
[Image: 1-0.png]
             
07-22-2006 06:08 PM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: reading boolean from registry
Hmm, quote from the MSDN:

quote:
Tip 
RegWrite will write at most one DWORD to a REG_BINARY value. Larger values are not supported with this method.
07-22-2006 07:19 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On